蒟蒻刚学OI,整体二分求助
查看原帖
蒟蒻刚学OI,整体二分求助
578628
Undead2008楼主2023/1/30 21:55
#include "iostream"
#include "cstdio"
#include "algorithm"
#include "cstring"
#include "vector"
using namespace std;

template<typename T>inline bool read(T &x_){
	int x=0,f=1;char ch=getchar();
	while (ch<'0'||ch>'9') {if(ch=='-')f=-1; ch=getchar();}
	while (ch>='0'&&ch<='9'){x=(x<<3)+(x<<1)+ch-'0'; ch=getchar();}
	x_=x*f;return 1;
}
template<typename T,typename ...Args>inline bool read(T& a,Args& ...args){
	return read(a)&&read(args...);
}
char getch(){
	char c=' ';
	while(c==' '||c=='\n'||c=='\r')
		c=getchar();
	return c;
}
namespace Undead{
	
	const int maxn = 200010;
	const int maxb = 200010;
	
	struct BITree{int ind[maxb];int lowbit(int x){return x&(-x);}
        void update(int p,int v){for(int i=p;i<=maxb;i+=lowbit(i))ind[i]+=v;}
        int query(int p){int ret=0;for(int i=p;i;i-=lowbit(i))ret+=ind[i];return ret;}
    }tr;
	
	struct opt{
		int l,r,k,id;//For Queries
		bool ty;//For Dividing Modifiations
		//0->Query,1->Modification
	}q[maxn],p1[maxn],p2[maxn];
	int n,m,top,idtop,ans[maxn],maxv;
	void two_div(int l,int r,int L,int R){
		if(l>r||L>R)return;
		if(l==r){
			for(int i=L;i<=R;i++)
				if(q[i].ty==0){
					ans[q[i].id]=l;
				}
			return;
		}
		int p1t=0,p2t=0;
		int mid=(l+r)>>1;
		for(int i=L;i<=R;i++){
			if(q[i].ty==0){
				int val=tr.query(q[i].r)-tr.query(q[i].l-1);
				if(q[i].k<=val){
					p1[++p1t]=q[i];
				}else{
					q[i].k-=val;
					p2[++p2t]=q[i];
				}
			}else{
				if(q[i].r<=mid){
					tr.update(q[i].l,q[i].r);
					p1[++p1t]=q[i];
				}else{
					p2[++p2t]=q[i];
				}
			}
		}
		for(int i=1;i<=p1t;i++)
			if(p1[i].ty==1)
				tr.update(p1[i].l,-p1[i].r);
		for(int i=L;i<=L+p1t-1;i++)
			q[i]=p1[i-(L-1)];
		for(int i=L+p1t;i<=R;i++)
			q[i]=p2[i-(L+p1t-1)];
		two_div(l,mid,L,L+p1t-1);
		two_div(mid+1,r,L+p1t,R);
	}
	void Reanimates(){
		cin.tie(0);
		read(n,m);
		for(int i=1;i<=n;i++){
			++top; 
			read(q[top].r);
			q[top].l=i;
			maxv=max(maxv,q[top].r);
			q[top].ty=1;
		}
		for(int i=1;i<=m;i++){
			char ch=getch();
			if(ch=='C'){
				++top;
				read(q[top].l,q[top].r);
				q[top].ty=1;
			}else{
				++top;
				read(q[top].l,q[top].r,q[top].k);
				q[top].id=++idtop;
				maxv=max(maxv,q[top].k);
				q[top].ty=0;
			}
		}
		two_div(0,maxv,1,top);
		for(int i=1;i<=idtop;i++)
			printf("%lld\n",ans[i]);
	}
}
int main(){
	Undead::Reanimates();
}

2023/1/30 21:55
加载中...