https://www.luogu.org/problemnew/show/2617
整体二分比树套树高级多了
#include<cstdio> #include<algorithm> #define FOR(i,s,t) for(register int i=s;i<=t;++i) const int N=1000011; struct question{ int t,p,x,y,k,cnt,cur; }q[N],s[N]; int ans[N]; int tr[N],a[N],tmp[N]; int n,m,tot,cnt,x,y,inf; char c; inline int max(int a,int b){return a>b?a:b;} inline void add(int p,int v){for(;p<=n;p+=p&-p)tr[p]+=v;} inline int query(int p){int ret=0;for(;p;p-=p&-p)ret+=tr[p];return ret;} inline void solve(int h,int t,int l,int r){ if(h>t)return; if(l==r){ FOR(i,h,t)ans[q[i].cnt]=l; return; } int mid=(l+r)>>1; FOR(i,h,t){ if(q[i].t!=3&&q[i].x<=mid)q[i].t==1?add(q[i].p,1):add(q[i].p,-1); if(q[i].t==3)tmp[i]=query(q[i].y)-query(q[i].x-1); } FOR(i,h,t)if(q[i].t!=3&&q[i].x<=mid)q[i].t==1?add(q[i].p,-1):add(q[i].p,1); int tail=h,d; FOR(i,h,t){ if(q[i].t==3&&q[i].cur+tmp[i]>=q[i].k)s[tail++]=q[i]; if(q[i].t!=3&&q[i].x<=mid)s[tail++]=q[i]; } d=tail-1; FOR(i,h,t){ if(q[i].t==3&&q[i].cur+tmp[i]<q[i].k)q[i].cur+=tmp[i],s[tail++]=q[i]; if(q[i].t!=3&&q[i].x>mid)s[tail++]=q[i]; } FOR(i,h,t)q[i]=s[i]; solve(h,d,l,mid); solve(d+1,t,mid+1,r); } int main(){ scanf("%d%d",&n,&m); FOR(i,1,n){ scanf("%d",a+i); inf=max(inf,a[i]); q[++tot].t=1;q[tot].p=i;q[tot].x=a[i];q[tot].cnt=0; } FOR(i,1,m){ scanf(" %c",&c); if(c=='Q'){ q[++tot].t=3; scanf("%d%d%d",&q[tot].x,&q[tot].y,&q[tot].k); q[tot].cnt=++cnt; } else{ scanf("%d%d",&x,&y); q[++tot].t=2;q[tot].p=x;q[tot].x=a[x];q[tot].cnt=0; a[x]=y;inf=max(inf,y); q[++tot].t=1;q[tot].p=x;q[tot].x=a[x];q[tot].cnt=0; } } solve(1,tot,0,inf); FOR(i,1,cnt)printf("%d ",ans[i]); return 0; }