BZOJ2674 : Attack
整体二分+树状数组套Treap,时间复杂度$O(n\log^3n)$。
#include<cstdio> #include<cstdlib> #include<algorithm> #define N 100010 using namespace std; int n,m,tot,i,X0,Y0,X1,Y1,cq,bx[N],by[N],bz[N],ans[N],tmp,C,D;char op[8]; struct P{ int a,b,c,d,k,p; P(){} P(int _a,int _b,int _c,int _d,int _k,int _p){a=_a,b=_b,c=_c,d=_d,k=_k,p=_p;} }a[N],b[N],ql[N],qr[N]; inline int findl(int*a,int x){ int l=1,r=n,mid,t; while(l<=r)if(a[mid=(l+r)>>1]>=x)r=(t=mid)-1;else l=mid+1; return t; } inline int findr(int*a,int x){ int l=1,r=n,mid,t; while(l<=r)if(a[mid=(l+r)>>1]<=x)l=(t=mid)+1;else r=mid-1; return t; } struct node{ int p,val,v,sum;node*l,*r; node(){val=v=sum=p=0;l=r=NULL;} inline void up(){sum=v+l->sum+r->sum;} }*blank=new(node),*bit[N],pool[6000000],*cur=pool;int pos[N],T; inline void Rotatel(node*&x){node*y=x->r;x->r=y->l;x->up();y->l=x;y->up();x=y;} inline void Rotater(node*&x){node*y=x->l;x->l=y->r;x->up();y->r=x;y->up();x=y;} void Ins(node*&x){ if(x==blank){ x=cur++;x->val=C;x->l=x->r=blank;x->v=x->sum=D;x->p=std::rand(); return; } x->sum+=D; if(C==x->val){x->v+=D;return;} if(C<x->val){ Ins(x->l); if(x->l->p>x->p)Rotater(x); }else{ Ins(x->r); if(x->r->p>x->p)Rotatel(x); } } void Ask(node*&x,int a,int b){ if(x==blank)return; if(C<=a&&b<=D){tmp+=x->sum;return;} if(C<=x->val&&x->val<=D)tmp+=x->v; if(C<x->val)Ask(x->l,a,x->val-1); if(D>x->val)Ask(x->r,x->val+1,b); } inline void add(int x,int y,int p){for(C=y,D=p;x<=n;Ins(bit[x]),x+=x&-x)if(pos[x]<T)pos[x]=T,bit[x]=blank;} inline int sum(int x,int l,int r){for(C=l,D=r,tmp=0;x;x-=x&-x)if(pos[x]==T)Ask(bit[x],0,n);return tmp;} void solve(int l,int r,int L,int R){ if(L>R)return; if(l==r){ for(int i=L;i<=R;i++)if(b[i].p)ans[b[i].p]=l; return; } int mid=(l+r)>>1,i,cl=0,cr=0; for(T++,i=L;i<=R;i++)if(!b[i].p){ if(b[i].k<=mid)add(b[i].a,b[i].b,b[i].c),ql[cl++]=b[i];else qr[cr++]=b[i]; }else{ int t=sum(b[i].b,b[i].c,b[i].d)-sum(b[i].a-1,b[i].c,b[i].d); if(t>=b[i].k)ql[cl++]=b[i];else b[i].k-=t,qr[cr++]=b[i]; } for(i=0;i<cl;i++)b[L+i]=ql[i]; for(i=0;i<cr;i++)b[L+cl+i]=qr[i]; solve(l,mid,L,L+cl-1),solve(mid+1,r,L+cl,R); } int main(){ scanf("%d%d",&n,&m); for(i=1;i<=n;i++){ scanf("%d%d%d",&a[i].a,&a[i].b,&a[i].k); bx[i]=a[i].a,by[i]=a[i].b,bz[i]=a[i].k; } sort(bx+1,bx+n+1),sort(by+1,by+n+1),sort(bz+1,bz+n+1); for(i=1;i<=n;i++){ a[i].a=findl(bx,a[i].a); a[i].b=findl(by,a[i].b); a[i].k=findl(bz,a[i].k); a[i].c=1; b[i]=a[i]; } tot=n; while(m--){ scanf("%s%d%d",op,&X0,&Y0); if(op[0]=='S'){ X0++,Y0++; if(X0==Y0)continue; b[++tot]=P(a[X0].a,a[X0].b,-1,0,a[X0].k,0); b[++tot]=P(a[X0].a,a[X0].b,1,0,a[Y0].k,0); b[++tot]=P(a[Y0].a,a[Y0].b,1,0,a[X0].k,0); b[++tot]=P(a[Y0].a,a[Y0].b,-1,0,a[Y0].k,0); swap(a[X0].k,a[Y0].k); }else{ scanf("%d%d%d",&X1,&Y1,&i); if(X0>X1)swap(X0,X1); if(Y0>Y1)swap(Y0,Y1); if(X0>bx[n]||X1<bx[1]||Y0>by[n]||Y1<by[1])X0=1,X1=Y0=Y1=0; else{ X0=findl(bx,X0); X1=findr(bx,X1); Y0=findl(by,Y0); Y1=findr(by,Y1); } b[++tot]=P(X0,X1,Y0,Y1,i,++cq); } } solve(1,n+1,1,tot); for(i=1;i<=cq;i++)if(ans[i]>n)puts("It doesn't exist.");else printf("%d\n",bz[ans[i]]); return 0; }