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 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
| #include<bits/stdc++.h> using namespace std; #define gc getchar #define pc putchar #define F(i,x,y) for(int i=x;i<=y;++i) #define R(i,x,y) for(int i=x;i>=y;--i) inline int read(){ int x=0;char ch=gc(); while (!isdigit(ch)) ch=gc(); while (isdigit(ch)) x=(x<<1)+(x<<3)+(ch^48),ch=gc(); return x; } inline void put(int x){ if (x) put(x/10),pc('0'+x%10); } inline void print(int x){ if (x>0) put(x); else if (x<0) pc('-'),put(-x); else pc('0'); pc('\n'); } const int N=5e4+11; struct Splay{ int val,fa,son[2],sz,cnt; } none; vector<Splay> G[N<<2];int rt[N<<2],A[N],idx[N<<2],n,m; #define val(y,x) G[y][x].val #define fa(y,x) G[y][x].fa #define sz(y,x) G[y][x].sz #define son(z,x,y) G[z][x].son[y] #define cnt(y,x) G[y][x].cnt const int INF=2147483647; inline void OUT(int pos){ F(i,0,(int)(G[pos].size()-1)){ printf("%d :: %d :: val=%d fa=%d sz=%d son=%d,%d cnt=%d rt=%d idx=%d\n" ,pos,i,val(pos,i),fa(pos,i),sz(pos,i),son(pos,i,0),son(pos,i,1),cnt(pos,i),rt[pos],idx[pos]); } } inline void maintain(int pos,int x){ if (!x) return; sz(pos,x)=sz(pos,son(pos,x,0))+sz(pos,son(pos,x,1))+cnt(pos,x); } inline bool get(int pos,int x){ return x==son(pos,fa(pos,x),1); } inline void clear(int pos,int x){ val(pos,x)=fa(pos,x)=sz(pos,x)=son(pos,x,0)=son(pos,x,1)=cnt(pos,x)=0; } inline void rotate(int pos,int x){ int y=fa(pos,x),z=fa(pos,y);bool p=get(pos,x); son(pos,y,p)=son(pos,x,p^1);if (son(pos,y,p)) fa(pos,son(pos,y,p))=y; son(pos,x,p^1)=y,fa(pos,y)=x; if (z) son(pos,z,y==son(pos,z,1))=x;fa(pos,x)=z; maintain(pos,y),maintain(pos,x); } inline void splay(int pos,int x){ if (!x) return; for (int f;(f=fa(pos,x));rotate(pos,x)){ if (fa(pos,f)) rotate(pos,get(pos,x)==get(pos,f)?f:x); } rt[pos]=x; } inline int findk(int pos,int x){ if (x<=0) return -INF; if (x>sz(pos,rt[pos])) return INF; int cur=rt[pos]; while (sz(pos,son(pos,cur,0))>=x||sz(pos,son(pos,cur,0))+cnt(pos,cur)<x){ if (sz(pos,son(pos,cur,0))>=x) cur=son(pos,cur,0); else x-=sz(pos,son(pos,cur,0))+cnt(pos,cur),cur=son(pos,cur,1); } return splay(pos,cur),cur; } inline int getrank(int pos,int x){ int cur=rt[pos],res=0; while (cur){ if (val(pos,cur)<x) res+=sz(pos,son(pos,cur,0))+cnt(pos,cur),cur=son(pos,cur,1); else if (val(pos,cur)==x) return res+=sz(pos,son(pos,cur,0)),splay(pos,cur),res; else cur=son(pos,cur,0); } return splay(pos,fa(pos,cur)),res; } inline void ins(int pos,int x){ if (!rt[pos]){ G[pos].push_back(none);++idx[pos]; cnt(pos,idx[pos])=1,val(pos,idx[pos])=x,fa(pos,idx[pos])=0,sz(pos,idx[pos])=1,rt[pos]=idx[pos]; return; } int cur=rt[pos],f=0; while (cur){ if (val(pos,cur)==x){ ++cnt(pos,cur),++sz(pos,cur),maintain(pos,f),splay(pos,cur); return; } f=cur,cur=son(pos,cur,val(pos,cur)<x); } G[pos].push_back(none);++idx[pos]; cnt(pos,idx[pos])=1,val(pos,idx[pos])=x,fa(pos,idx[pos])=f,sz(pos,idx[pos])=1; if (f) son(pos,f,val(pos,f)<x)=idx[pos]; maintain(pos,f),splay(pos,idx[pos]); } inline void rtpre(int pos){ int cur=son(pos,rt[pos],0); while (son(pos,cur,1)) cur=son(pos,cur,1); splay(pos,cur); } inline void del(int pos,int x){ x=findk(pos,getrank(pos,x)+1); if (cnt(pos,x)>1) return --cnt(pos,x),--sz(pos,x),void(); if (!son(pos,x,0)||!son(pos,x,1)){ fa(pos,son(pos,x,0)+son(pos,x,1))=0,rt[pos]=son(pos,x,0)+son(pos,x,1),clear(pos,x); return; } rtpre(pos); fa(pos,son(pos,x,1))=rt[pos],son(pos,rt[pos],1)=son(pos,x,1),clear(pos,x),maintain(pos,rt[pos]); } void Build(int k,int l,int r){ G[k].push_back(none); F(i,l,r) ins(k,A[i]); if (l==r) return; int mid=l+r>>1; Build(k<<1,l,mid),Build(k<<1|1,mid+1,r); } int Queryrank(int k,int l,int r,int le,int ri,int v){ if (le<=l&&ri>=r) return getrank(k,v); int mid=l+r>>1,res=0; if (mid>=le) res+=Queryrank(k<<1,l,mid,le,ri,v); if (mid<ri) res+=Queryrank(k<<1|1,mid+1,r,le,ri,v); return res; } inline void tomax(int& x,int y){ if (y>x) x=y; } inline void tomin(int& x,int y){ if (y<x) x=y; } int Querypre(int k,int l,int r,int le,int ri,int v){ if (le<=l&&ri>=r){ int d=findk(k,getrank(k,v)); return d==-INF?d:val(k,d); } int mid=l+r>>1,res=-INF; if (mid>=le) tomax(res,Querypre(k<<1,l,mid,le,ri,v)); if (mid<ri) tomax(res,Querypre(k<<1|1,mid+1,r,le,ri,v)); return res; } int Querynxt(int k,int l,int r,int le,int ri,int v){ if (le<=l&&ri>=r){ int d=findk(k,getrank(k,v+1)+1); return d==INF?d:val(k,d); } int mid=l+r>>1,res=INF; if (mid>=le) tomin(res,Querynxt(k<<1,l,mid,le,ri,v)); if (mid<ri) tomin(res,Querynxt(k<<1|1,mid+1,r,le,ri,v)); return res; } int Queryk(int le,int ri,int v){ int a=0,b=1e8; while (a<b){ int mid=a+b+1>>1; int d=Queryrank(1,1,n,le,ri,mid); if (d<v) a=mid; else b=mid-1; } return b; } void Modify(int k,int l,int r,int p,int v){ del(k,A[p]); ins(k,v); if (l==r) return A[p]=v,void(); int mid=l+r>>1; if (mid>=p) Modify(k<<1,l,mid,p,v); else Modify(k<<1|1,mid+1,r,p,v); } signed main(){ n=read(),m=read(); F(i,1,n) A[i]=read(); Build(1,1,n); F(i,1,m){ int op=read(),x=read(),y=read(),z; if (op==3) Modify(1,1,n,x,y); else{ z=read(); if (op==1) print(Queryrank(1,1,n,x,y,z)+1);else if (op==2) print(Queryk(x,y,z));else if (op==4) print(Querypre(1,1,n,x,y,z));else if (op==5) print(Querynxt(1,1,n,x,y,z)); } } return 0; }
|