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
| #include <cstdio> #include <iostream> #include <cctype> #include <algorithm> using namespace std; const int Mn(100500); void qrd(int& x) { x = 0; char c=getchar(); while(!isdigit(c)) { c = getchar(); } while(isdigit(c)) { x = x*10 + c-'0'; c = getchar(); } } inline int lb(int x) { return x&(-x); }
int a[Mn],shf[Mn*2];
struct Query { int t,x,y,z; }q[Mn];
struct Sgn { int v; Sgn *ls,*rs; }*bitr[Mn]; Sgn *nil = new Sgn(); void bud(int l,int r,Sgn* p) { p->v = 0; if(l==r) { p->ls = p->rs = nil; return; } int mid((l+r)/2); p->ls = new Sgn(); p->rs = new Sgn(); bud(l,mid,p->ls); bud(mid+1,r,p->rs); } void mdf(int l,int r,int mp,int mv,Sgn*& p) { if(p==nil) { p = new Sgn(); p->ls = p->rs = nil; } if(l==r) { p->v += mv; return; } int mid((l+r)/2); if(mid>=mp) { mdf(l,mid,mp,mv,p->ls); } else { mdf(mid+1,r,mp,mv,p->rs); } p->v = p->ls->v + p->rs->v; } Sgn *tmpq[2][Mn]; int qcn[2]; int qry(int l,int r,int k) { if(l==r) { return shf[l]; } int sum(0); for(int i(1);i<=qcn[1];++i) { sum += tmpq[1][i]->ls->v; } for(int i(1);i<=qcn[0];++i) { sum -= tmpq[0][i]->ls->v; } int mid((l+r)/2); if(k <= sum) { for(int i(1);i<=qcn[1];++i) { tmpq[1][i] = tmpq[1][i]->ls; } for(int i(1);i<=qcn[0];++i) { tmpq[0][i] = tmpq[0][i]->ls; } return qry(l,mid,k); } else { for(int i(1);i<=qcn[1];++i) { tmpq[1][i] = tmpq[1][i]->rs; } for(int i(1);i<=qcn[0];++i) { tmpq[0][i] = tmpq[0][i]->rs; } return qry(mid+1,r,k-sum); } }
int main() { nil->ls = nil->rs = nil; int n,m; qrd(n), qrd(m); for(int i(1);i<=n;++i) { qrd(a[i]); shf[i] = a[i]; } int sn(n); for(int i(1);i<=m;++i) { char op; cin >> op; if(op=='Q') { q[i].t = 0; qrd(q[i].x), qrd(q[i].y), qrd(q[i].z); } else { q[i].t = 1; qrd(q[i].x), qrd(q[i].y); shf[++sn] = q[i].y; } }
sort(shf+1,shf+sn+1); sn = unique(shf+1,shf+sn+1)-shf-1; for(int i(1);i<=n+1;++i) { bitr[i] = nil; } for(int i(1);i<=n;++i) { int t(i); int mp = lower_bound(shf+1,shf+sn+1,a[i])-shf; while(t<=n) { mdf(1,sn,mp,1,bitr[t]); t += lb(t); } }
for(int i(1);i<=m;++i) { Query& d(q[i]); if(d.t==0) { if(d.y==1) { printf("%d\n",a[1]); continue; } qcn[0] = qcn[1] = 0; d.x -= 1; while(d.x) { tmpq[0][++qcn[0]] = bitr[d.x]; d.x -= lb(d.x); } while(d.y) { tmpq[1][++qcn[1]] = bitr[d.y]; d.y -= lb(d.y); } int ans = qry(1,sn,d.z); printf("%d\n",ans); } else { int tmp(d.x); int mp = lower_bound(shf+1,shf+sn+1,a[d.x])-shf; while(tmp<=n) { mdf(1,sn,mp,-1,bitr[tmp]); tmp += lb(tmp); } a[d.x] = d.y; tmp = d.x; mp = lower_bound(shf+1,shf+sn+1,d.y)-shf; while(tmp<=n) { mdf(1,sn,mp,1,bitr[tmp]); tmp += lb(tmp); } } } return 0; }
|