抽象分块求区间前 k 大和求调
查看原帖
抽象分块求区间前 k 大和求调
826774
Little_Fox_Fairy楼主2025/1/18 17:33

TLE 我能理解,但是 WA 是为什么啊(恼

#include<bits/stdc++.h>
#define max(a,b) Max(a,b)
using namespace std;
namespace fast_IO {
#define IOSIZE 100000
	char ibuf[IOSIZE], obuf[IOSIZE], *p1 = ibuf, *p2 = ibuf, *p3 = obuf;
#define getchar() ((p1==p2)and(p2=(p1=ibuf)+fread(ibuf,1,IOSIZE,stdin),p1==p2)?(EOF):(*p1++))
#define putchar(x) ((p3==obuf+IOSIZE)&&(fwrite(obuf,p3-obuf,1,stdout),p3=obuf),*p3++=x)
#define isdigit(ch) (ch>47&&ch<58)
#define isspace(ch) (ch<33)
	template<typename T> inline T read() { T s = 0; int w = 1; char ch; while (ch = getchar(), !isdigit(ch) and (ch != EOF)) if (ch == '-') w = -1; if (ch == EOF) return false; while (isdigit(ch)) s = s * 10 + ch - 48, ch = getchar(); return s * w; }
	template<typename T> inline bool read(T &s) { s = 0; int w = 1; char ch; while (ch = getchar(), !isdigit(ch) and (ch != EOF)) if (ch == '-') w = -1; if (ch == EOF) return false; while (isdigit(ch)) s = s * 10 + ch - 48, ch = getchar(); return s *= w, true; }
	template<typename T> inline void print(T x) { if (x < 0) putchar('-'), x = -x; if (x > 9) print(x / 10); putchar(x % 10 + 48); }
	inline bool read(char &s) { while (s = getchar(), isspace(s)); return true; }
	inline bool read(char *s) { char ch; while (ch = getchar(), isspace(ch)); if (ch == EOF) return false; while (!isspace(ch)) *s++ = ch, ch = getchar(); *s = '\000'; return true; }
	inline void print(char x) { putchar(x); }
	inline void print(char *x) { while (*x) putchar(*x++); }
	inline void print(const char *x) { for (int i = 0; x[i]; i++) putchar(x[i]); }
	inline bool read(std::string& s) { s = ""; char ch; while (ch = getchar(), isspace(ch)); if (ch == EOF) return false; while (!isspace(ch)) s += ch, ch = getchar(); return true; }
	inline void print(std::string x) { for (int i = 0, n = x.size(); i < n; i++) putchar(x[i]); }
	inline bool read(bool &b) { char ch; while(ch=getchar(), isspace(ch)); b=ch^48; return true; }
	inline void print(bool b) { putchar(b+48); }
	template<typename T, typename... T1> inline int read(T& a, T1&... other) { return read(a) + read(other...); }
	template<typename T, typename... T1> inline void print(T a, T1... other) { print(a), print(other...); }
	struct Fast_IO { ~Fast_IO() { fwrite(obuf, p3 - obuf, 1, stdout); } } io;
	template<typename T> Fast_IO& operator >> (Fast_IO &io, T &b) { return read(b), io; }
	template<typename T> Fast_IO& operator << (Fast_IO &io, T b) { return print(b), io; }
#define cout io
#define cin io
#define endl '\n'
} using namespace fast_IO;
const int N=1e5+10;
const int C=110;

int n,m,st,d,e[N];
long long b[N];
int sum2[C][C],sum3[C][N];
int bel[N],loc,L[N],R[N],S=1000,LOC;
long long sum1[C][C];
long long f1[N],f2[N],f3[N],f4[N];
inline long long Max(const long long &x,const long long &y) {return x>y?x:y;}
namespace BLOCK {
	inline void calc() {
		for (int i=1;i<=n;i++) b[i]=e[i];
		sort(b+1,b+n+1);
		m=unique(b+1,b+n+1)-b-1;
		for (int i=1;i<=n;i++) e[i]=lower_bound(b+1,b+m+1,e[i])-b;
		return ;
	}
	inline void init() {
		loc=(n-1)/S+1,LOC=(m-1)/S+1;
		for (int i=1;i<=n;i++) bel[i]=(i-1)/S+1;
		for (int i=1;i<=loc;i++) {
			L[i]=(i-1)*S+1,R[i]=i*S;
			if (i==loc) R[i]=n;
			for (int j=L[i];j<=R[i];j++) for (int k=i;k<=loc;k++) sum1[k][bel[e[j]]]+=b[e[j]],sum2[k][bel[e[j]]]++,sum3[k][e[j]]++;
		}
		return ;
	}
	long long s[C];
	int c[C],t[N];
	inline void add(int l,int r) {
		for (int i=l;i<=r;i++) c[bel[e[i]]]++,s[bel[e[i]]]+=b[e[i]],t[e[i]]++;
		return ;
	}
	inline void del(int l,int r) {
		for (int i=l;i<=r;i++) c[bel[e[i]]]--,s[bel[e[i]]]-=b[e[i]],t[e[i]]--;
		return ;
	}
	inline long long query(int l,int r,int k) {
		if (l>r or k<=0) return 0;
		int bll=bel[l],blr=bel[r];
		long long res=0;
		if (bll==blr) {
			int pos=0;
			add(l,r);
			for (int i=LOC;i>=1;i--) {
				if (!k) break;
				if (k>=c[i]) {
					k-=c[i];
					res+=s[i];
				}
				else {
					pos=i;
					break;
				}
			}
			for (int i=R[pos];i>=L[pos];i--) {
				if (!t[i]) continue;
				if (k>=t[i]) {
					k-=t[i];
					res+=t[i]*b[i];
				}
				else {
					res+=k*b[i];
					break;
				}
			}
			del(l,r);
			return res;
		}
		add(l,R[bll]),add(L[blr],r);
		int pos=0;
		for (int i=LOC;i>=1;i--) {
			if (!k) break;
			if (k>=c[i]+sum2[blr-1]-sum2[bll]) {
				k-=c[i]+sum2[blr-1]-sum2[bll];
				res+=s[i]+sum1[blr-1]-sum1[bll];
			}
			else {
				pos=i;
				break;
			}
		}
		for (int i=R[pos];i>=L[pos];i--) {
			if (!(t[i]+sum3[blr-1][i]-sum3[bll][i])) continue;
			if (k>=t[i]+sum3[blr-1][i]-sum3[bll][i]) {
				k-=t[i]+sum3[blr-1][i]-sum3[bll][i];
				res+=(t[i]+sum3[blr-1][i]-sum3[bll][i])*b[i];
			}
			else {
				res+=k*b[i];
				break;
			}
		}
		del(l,R[bll]),del(L[blr],r);
		return res;
	}
}using namespace BLOCK;
inline void solve1(int l,int r,int lft,int rght) {
	if (l>r) return ;
	int mid=l+r>>1,pos=rght;
	long long maxx=0;
	for (int i=lft;i<=rght;i++) {
		long long tmp=query(i,st-1,mid-st+i);
		if (tmp>maxx) {
			maxx=tmp;
			pos=i;
		}
	}
	f1[mid]=maxx;
	solve1(l,mid-1,pos,rght);
	solve1(mid+1,r,lft,pos);
	return ;
}
inline void solve2(int l,int r,int lft,int rght) {
	if (l>r) return ;
	int mid=l+r>>1,pos=lft;
	long long maxx=0;
	for (int i=lft;i<=rght;i++) {
		long long tmp=query(st+1,i,mid-i+st);
		if (tmp>maxx) {
			maxx=tmp;
			pos=i;
		}
	}
	f2[mid]=maxx;
	solve2(l,mid-1,lft,pos);
	solve2(mid+1,r,pos,rght);
	return ;
}
inline void solve3(int l,int r,int lft,int rght) {
	if (l>r) return ;
	int mid=l+r>>1,pos=rght;
	long long maxx=0;
	for (int i=lft;i<=rght;i++) {
		long long tmp=query(i,st-1,mid-(st-i)*2);
		if (tmp>maxx) {
			maxx=tmp;
			pos=i;
		}
	}
	f3[mid]=maxx;
	solve3(l,mid-1,pos,rght);
	solve3(mid+1,r,lft,pos);
	return ;
}
inline void solve4(int l,int r,int lft,int rght) {
	if (l>r) return ;
	int mid=l+r>>1,pos=lft;
	long long maxx=0;
	for (int i=lft;i<=rght;i++) {
		long long tmp=query(st+1,i,mid-(i-st)*2);
		if (tmp>maxx) {
			maxx=tmp;
			pos=i;
		}
	}
	f4[mid]=maxx;
	solve4(l,mid-1,lft,pos);
	solve4(mid+1,r,pos,rght);
	return ;
}
signed main() {
	cin>>n>>st>>d;st++;
	for (int i=1;i<=n;i++) cin>>e[i];
	calc();init();
	solve1(0,d,1,st-1),solve2(0,d,st+1,n),solve3(0,d,1,st-1),solve4(0,d,st+1,n);
	long long res=0;
	for (int i=0;i<=d;i++) res=max(max(res,f1[d-i]+f4[i]),f2[d-i]+f3[i]);
	for (int i=1;i<=d;i++) res=max(max(res,f1[d-i]+f4[i-1]+b[e[st]]),f2[d-i]+f3[i-1]+b[e[st]]);
	cout<<res<<endl;
	return (0-0);
}
2025/1/18 17:33
加载中...