把博客园图标替换成自己的图标
把博客园图标替换成自己的图标end

BZOJ #2122. 工作评估

题面传送门
首先我们发现如果对于一段固定的序列,出来的值随着塞进去的值变大而不降。、
\(f(l,r,x)\)表示将\(x\)塞进\([l,r]\)中,那么如果\(x_1<x_2\)\(f(l,r,x1)\leq f(l,r,x2)\)
然后就可以写出\(50\)分的类似最大子段和的东西。
然后仔细观察又可以发现\(f_(l,r,x)=\min(f(l,r,INF),\sum\limits_{i=l}^{r}{b_i}+x)\)
然后分块以后块内二分就可以得到答案。
时间复杂度\(O(n\sqrt nlogn)\)
code:

#include<bits/stdc++.h>
#define I inline
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
#define abs(x) ((x)>0?(x):-(x))
#define re register
#define RI re int
#define ll long long
#define db double
#define lb long db
#define N 50000
#define K 330
#define mod 2333
#define Mod 998244352
#define eps (1e-4)
#define U unsigned int
#define it iterator
#define Gc() getchar() 
#define Me(x,y) memset(x,y,sizeof(x))
#define d(x,y) (n*(x-1)+(y))
#define R(n) (rand()*rand()%(n)+1)
using namespace std;
int n,m,k,Maxn,l,r,mid,x,y,z,X,Ans,B[N+5],C[N+5],A[N+5],Fr[K+5],En[K+5],H[K+5],G1H[K+5],G2H[K+5],Sh,Q[N+5],G[K+5];
struct Pa{int s,g;}F[K+5][N+5],G1[K+5][K+5],G2[K+5][K+5],Sf[N+5];I bool cmp(Pa x,Pa y){return x.s>y.s;}
I void MD(Pa *F,int &H){RI i;sort(F+1,F+H+1,cmp);Maxn=-1e9;for(Sh=0,i=1;i<=H;i++) F[i].g>Maxn&&(Sf[++Sh]=F[i],Maxn=F[i].g);H=Sh;for(i=1;i<=H;i++) F[i]=Sf[H-i+1];}
int main(){
	freopen("1.in","r",stdin);freopen("1.out","w",stdout);
	RI i,j,h;scanf("%d%d",&n,&m);k=sqrt(n/2);for(i=1;i<=n;i++) scanf("%d",&B[i]),Q[i]=Q[i-1]+B[i];for(i=1;i<=n;i++) scanf("%d",&C[i]);
	for(i=0;i<=n/k;i++){
		Fr[i]=max(1,i*k),En[i]=min(n,i*k+k-1);for(j=Fr[i];j<=En[i];j++){
			for(x=1e9+7,h=j;h<=En[i];h++)x=min(x+B[h],C[h]),F[i][++H[i]]=(Pa){Q[h]-Q[j-1],x},j==Fr[i]&&(G1[i][++G1H[i]]=F[i][H[i]],0),h==En[i]&&(G2[i][++G2H[i]]=F[i][H[i]],0),j==Fr[i]&&h==En[i]&&(G[i]=x,0);
		}MD(F[i],H[i]);MD(G1[i],G1H[i]);MD(G2[i],G2H[i]);
	}while(m--){
		scanf("%d%d%d",&x,&y,&z);Ans=z;if(x/k==y/k){for(X=z,i=x;i<=y;i++)X=min(X+B[i],C[i]),Ans=max(Ans,X),X<z&&(X=z);printf("%d\n",Ans);continue;}
		for(X=z,i=x;i<=En[x/k];i++) X=min(X+B[i],C[i]),Ans=max(Ans,X),X=max(X,z);for(i=x/k+1;i<y/k;i++){
			l=1;r=H[i];while(l+1<r) mid=l+r>>1,(F[i][mid].s+z<=F[i][mid].g?l:r)=mid;Ans=max(Ans,max(min(F[i][r].s+z,F[i][r].g),min(F[i][l].s+z,F[i][l].g)));
			l=1;r=G1H[i];while(l+1<r) mid=l+r>>1,(G1[i][mid].s+X<=G1[i][mid].g?l:r)=mid;Ans=max(Ans,max(min(G1[i][r].s+X,G1[i][r].g),min(G1[i][l].s+X,G1[i][l].g)));X=max(z,min(X+Q[En[i]]-Q[Fr[i]-1],G[i]));
			l=1;r=G2H[i];while(l+1<r) mid=l+r>>1,(G2[i][mid].s+z<=G2[i][mid].g?l:r)=mid;X=max(X,max(min(G2[i][r].s+z,G2[i][r].g),min(G2[i][l].s+z,G2[i][l].g)));;Ans=max(Ans,X);
		}for(i=Fr[y/k];i<=y;i++) X=min(X+B[i],C[i]),Ans=max(Ans,X),X=max(X,z);printf("%d\n",Ans);
	}
}
posted @ 2021-11-12 09:04  275307894a  阅读(39)  评论(0编辑  收藏  举报
浏览器标题切换
浏览器标题切换end