线段树+二分0pts求条,悬棺
查看原帖
线段树+二分0pts求条,悬棺
1052499
Em0ty楼主2024/10/22 15:04
#include<bits/stdc++.h>
#define N 200010
#define lc (p<<1)
#define rc (p<<1|1)
#define mid ((l+r)>>1)
using namespace std;
int a[N],sum[N<<2],add[N<<2],cnt,tms;
/*¿ì¶Á¿ìд*/ 
inline int rd(){
	char c=getchar();
	int base=0;
	while(c>='0'&&c<='9'){
		base=base*10+c-'0';
		c=getchar();
	}
	return base;
}
int buf[10];
inline void wt(int i){
	int p=0;
	if(i==0) p++;
	else{
		while(i){
			buf[p++]=i%10;
			i/=10;
		}
	}
	for(int j=p-1;j>=0;j--){
		putchar('0'+buf[j]);
	}
	putchar('\n');
}
/*¿ì¶Á¿ìд*/
inline void pushup(int p){
	sum[p]=sum[lc]+sum[rc];
}
inline void f(int p,int l,int r,int k){
	add[p] = add[p]+k;
	sum[p] = sum[p]+k*(r-l+1);
}
inline void build(int p,int l,int r){
	if(l==r){
		sum[p]=a[l];
		return ;
	} 
	build(lc,l,mid);
	build(rc,mid+1,r);
	pushup(p);
}
inline void pushdown(int p,int l,int r){
	f(lc,l,mid,add[p]);
	f(rc,mid+1,r,add[p]);
	add[p]=0;
}
inline void updata(int p,int l,int r,int L,int R,int k){
	if(L<=l&&r<=R){
		sum[p]+=k*(r-l+1);
		add[p]+=k;
		return ;
	}
	pushdown(p,l,r);
	if(L<=mid) updata(lc,l,mid,L,R,k);
	if(mid+1<=R) updata(rc,mid+1,r,L,R,k);
	pushup(p);
}
inline int query(int p,int l,int r,int L,int R){
	int ans=0;
	if(L<=l&&r<=R){
		return sum[p];
	}
	pushdown(p,l,r);
	if(L<=mid) ans+=query(lc,l,mid,L,R);
	if(mid+1<=R) ans+=query(rc,mid+1,r,L,R);
	return ans;
}
int main(){
	int n,q,w,ans;
	n=rd();
	q=rd();
	w=rd();
	wt(n);wt(q);wt(w);
	for(int i=1;i<=n;i++){
		a[i]=rd();
	}
	build(1,1,n);
	for(int i=1;i<=q;i++){
		cnt=1;
		tms=0;
		ans=0;
		int W=w;
		int l=rd();
		int r=rd();
		int d=rd();
		updata(1,1,n,l,r,d);
		int x=1,y=n,md=n,res=1;
		while(W>=query(1,1,n,1,n)*cnt){
			W-=query(1,1,n,1,n)*cnt;
			cnt<<=1;
			tms++;
		}
		if(W==0){
			wt(tms*n-1);
			continue;
		}
		while(x+1<y){
			md=(x+y)>>1;
			if(query(1,1,n,1,md)*cnt<W){
				x=md;
			}
			else{
				y=md;
			}
		}
		ans=tms*n+x;
		wt(ans);
	}
	return 0;
}

2024/10/22 15:04
加载中...