1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
| #include<iostream> #include<cstdio> #include<cstring> #include<cctype> #include<algorithm> #include<cmath> using namespace std; inline int read(){ int w=0,x=0;char c=getchar(); while(!isdigit(c))w|=c=='-',c=getchar(); while(isdigit(c))x=(x<<3)+(x<<1)+(c^48),c=getchar(); return w?-x:x; } namespace star { const int maxn=5e4+10,maxm=1e7+10,maxl=1e8; int n,q,rt[maxm],ls[maxm*3],rs[maxm*3],Ls[maxm],Rs[maxm],val[maxm*3],tot,Tot,root; int a[maxn]; void update(int &ro,int l,int r,int p,int k){ if(k==1 and !ro)ro=++tot; if(ro) val[ro]+=k; if(!ro or l==r)return; int mid=l+r>>1; (p<=mid)?update(ls[ro],l,mid,p,k):update(rs[ro],mid+1,r,p,k); } void update(int &ro,int l,int r,int v,int p,int k){ if(k==1 and !ro)ro=++Tot; if(ro) update(rt[ro],1,n,p,k); if(!ro or l==r)return; int mid=l+r>>1; (v<=mid)?update(Ls[ro],l,mid,v,p,k):update(Rs[ro],mid+1,r,v,p,k); } int query(int ro,int l,int r,int x,int y){ if(!ro)return 0; if(x<=l and y>=r)return val[ro]; int mid=l+r>>1; return (x<=mid?query(ls[ro],l,mid,x,y):0)+(y>mid?query(rs[ro],mid+1,r,x,y):0); } int rank(int ro,int l,int r,int x,int y,int L,int R){ if(!ro)return 0; if(x<=l and y>=r)return query(rt[ro],1,n,L,R); int mid=l+r>>1; return (x<=mid?rank(Ls[ro],l,mid,x,y,L,R):0)+(y>mid?rank(Rs[ro],mid+1,r,x,y,L,R):0); } int kth(int ro,int l,int r,int k,int L,int R){ if(l==r)return l; int mid=l+r>>1,tmp=query(rt[Ls[ro]],1,n,L,R); return (k<=tmp)?kth(Ls[ro],l,mid,k,L,R):kth(Rs[ro],mid+1,r,k-tmp,L,R); } inline int pre(int x,int y,int k){ int zp; if(!(zp=rank(root,0,maxl,0,k-1,x,y)))return -2147483647; return kth(root,0,maxl,zp,x,y); } inline int nxt(int x,int y,int k){ int zp; if((zp=rank(root,0,maxl,0,k,x,y))==rank(root,0,maxl,0,maxl,x,y))return 2147483647; return kth(root,0,maxl,zp+1,x,y); } inline void work(){ n=read(),q=read(); for(int i=1;i<=n;i++) update(root,0,maxl,a[i]=read(),i,1); while(q--){ int op=read(),x=read(),y=read(); switch(op){ case 1:printf("%d\n",rank(root,0,maxl,0,read()-1,x,y)+1);break; case 2:printf("%d\n",kth(root,0,maxl,read(),x,y));break; case 3:update(root,0,maxl,a[x],x,-1),update(root,0,maxl,a[x]=y,x,1);break; case 4:printf("%d\n",pre(x,y,read()));break; case 5:printf("%d\n",nxt(x,y,read()));break; } } } } signed main(){ star::work(); return 0; }
|