1 #include <iostream>
2 #include <cstdio>
3 #include <algorithm>
4 using namespace std;
5 int sum[2500010],ls[2500010],rs[2500010],root[10010],A[10010],B[10010],C[10010],num[20010],hash[20010],v[10010],L[30],R[30],flag[10010];
6 int n,m,nt,numt,sz,a,b;
7 int lowbit(int x){return x&(-x);}
8
9 int find(int x)
10 {
11 int l=1,r=nt,mid;
12 while (l<=r)
13 {
14 mid=(l+r)>>1;
15 if (hash[mid]<x) l=mid+1;
16 else r=mid-1;
17 }
18 return l;
19 }
20
21 void updata(int l,int r,int x,int &y,int p,int w)
22 {
23 y=++sz;
24 sum[y]=sum[x]+w;
25 ls[y]=ls[x]; rs[y]=rs[x];
26 if (l==r) return;
27 int mid=(l+r)>>1;
28 if (p<=mid) updata(l,mid,ls[x],ls[y],p,w);
29 else updata(mid+1,r,rs[x],rs[y],p,w);
30 }
31
32 int query(int l,int r,int k)
33 {
34 if (l==r) return l;
35 int suml=0,sumr=0;
36 for (int i=1;i<=a;i++) suml+=sum[ls[L[i]]];
37 for (int i=1;i<=b;i++) sumr+=sum[ls[R[i]]];
38 int mid=(l+r)>>1;
39 if (sumr-suml>=k)
40 {
41 for (int i=1;i<=a;i++) L[i]=ls[L[i]];
42 for (int i=1;i<=b;i++) R[i]=ls[R[i]];
43 return query(l,mid,k);
44 }
45 else
46 {
47 for (int i=1;i<=a;i++) L[i]=rs[L[i]];
48 for (int i=1;i<=b;i++) R[i]=rs[R[i]];
49 return query(mid+1,r,k-(sumr-suml));
50 }
51 }
52
53 int main()
54 {
55 scanf("%d%d",&n,&m);
56 for (int i=1;i<=n;i++)
57 {
58 scanf("%d",&v[i]);
59 num[++numt]=v[i];
60 }
61 for (int i=1;i<=m;i++)
62 {
63 char s[6];
64 scanf("%s",s);
65 scanf("%d%d",&A[i],&B[i]);
66 if (s[0]=='Q') {scanf("%d",&C[i]);flag[i]=1;}
67 else num[++numt]=B[i];
68 }
69 sort(num+1,num+numt+1);
70 hash[++nt]=num[1];
71 for (int i=2;i<=numt;i++)
72 if (num[i]!=num[i-1])
73 hash[++nt]=num[i];
74 for (int i=1;i<=n;i++)
75 {
76 int t=find(v[i]);
77 for (int j=i;j<=n;j+=lowbit(j)) updata(1,nt,root[j],root[j],t,1);
78 }
79 for (int i=1;i<=m;i++)
80 {
81 if (flag[i])
82 {
83 a=0,b=0;
84 int xx;
85 A[i]--;
86 for (int j=A[i];j>=1;j-=lowbit(j)) L[++a]=root[j];
87 for (int j=B[i];j>=1;j-=lowbit(j)) R[++b]=root[j];
88 xx=query(1,nt,C[i]);
89 printf("%d\n",hash[xx]);
90 }
91 else
92 {
93 int t=find(v[A[i]]);
94 for (int j=A[i];j<=n;j+=lowbit(j)) updata(1,nt,root[j],root[j],t,-1);
95 v[A[i]]=B[i];
96 t=find(B[i]);
97 for (int j=A[i];j<=n;j+=lowbit(j)) updata(1,nt,root[j],root[j],t,1);
98 }
99 }
100 return 0;
101 }