1 const maxn=50000+1000;
2 type node=record
3 l,r,lch,rch,rt,mid:longint;
4 end;
5 var t:array[0..4*maxn] of node;
6 l,r,s,w,rnd,v,a:array[0..50*maxn] of longint;
7 i,n,m,x,y,z,tot:longint;
8 ch:char;
9 procedure pushup(k:longint);
10 begin
11 s[k]:=s[l[k]]+s[r[k]]+w[k];
12 end;
13 procedure rturn(var k:longint);
14 var t:longint;
15 begin
16 t:=l[k];l[k]:=r[t];r[t]:=k;s[t]:=s[k];pushup(k);k:=t;
17 end;
18 procedure lturn(var k:longint);
19 var t:longint;
20 begin
21 t:=r[k];r[k]:=l[t];l[t]:=k;s[t]:=s[k];pushup(k);k:=t;
22 end;
23 procedure ins(var k,num:longint);
24 begin
25 if k=0 then
26 begin
27 inc(tot);k:=tot;v[k]:=num;s[k]:=1;w[k]:=1;l[k]:=0;r[k]:=0;
28 rnd[k]:=random(maxlongint);exit;
29 end;
30 inc(s[k]);
31 if num=v[k] then inc(w[k])
32 else if num<v[k] then begin ins(l[k],num);if rnd[l[k]]<rnd[k] then rturn(k);end
33 else begin ins(r[k],num);if rnd[r[k]]<rnd[k] then lturn(k);end;
34 end;
35 procedure del(var k,num:longint);
36 begin
37 if v[k]=num then
38 begin
39 if w[k]>1 then begin dec(w[k]);dec(s[k]);exit;end;
40 // writeln(l[100],‘ ‘,r[100],‘ ‘,rnd[100],‘ ‘,num);
41 // writeln(l[93],‘ ‘,r[93],‘ ‘,rnd[93],‘ ‘,num);
42 if l[k]*r[k]=0 then k:=l[k]+r[k]
43 else if rnd[l[k]]<rnd[r[k]] then begin rturn(k);del(k,num);end
44 else begin lturn(k);del(k,num);end;
45 exit;
46 end;
47 dec(s[k]);
48 if num<v[k] then del(l[k],num) else del(r[k],num);
49 end;
50 procedure build(k,x,y:longint);
51 var i:longint;
52 begin
53 with t[k] do
54 begin
55 l:=x;r:=y;mid:=(l+r)>>1;
56 for i:=l to r do ins(rt,a[i]);
57 if l=r then exit;
58 lch:=k<<1;rch:=k<<1+1;
59 build(lch,l,mid);build(rch,mid+1,r);
60 end;
61 end;
62 procedure print(x:longint);
63 begin
64 if x=0 then exit;
65 print(l[x]);
66 write(x,‘ ‘,v[x],‘!‘);
67 print(r[x]);
68 end;
69 procedure change(k,x,y:longint);
70 begin
71 with t[k] do
72 begin
73 // writeln(k,‘ ‘,l,‘ ‘,r,‘ ‘,rt);
74 // print(rt);writeln;
75 del(rt,a[x]);ins(rt,y);
76 if l=r then exit;
77 if x<=mid then change(lch,x,y)
78 else change(rch,x,y);
79 end;
80 end;
81 function rank(k,num:longint):longint;
82 begin
83 if k=0 then exit(0);
84 if num<v[k] then exit(rank(l[k],num))
85 else exit(s[l[k]]+w[k]+rank(r[k],num));
86 end;
87 function query(k,x,y,num:longint):longint;
88 begin
89 with t[k] do
90 begin
91 //writeln(k,‘ ‘,l,‘ ‘,r,‘ ‘,x,‘ ‘,y,‘ ‘,num);
92 if (l=x) and (r=y) then exit(rank(rt,num));
93 if y<=mid then exit(query(lch,x,y,num))
94 else if x>mid then exit(query(rch,x,y,num))
95 else exit(query(lch,x,mid,num)+query(rch,mid+1,y,num));
96 end;
97 end;
98 procedure solvechange;
99 begin
100 readln(x,y);
101 change(1,x,y);
102 a[x]:=y;
103 end;
104 procedure solveask;
105 var l,r,mid:longint;
106 begin
107 readln(x,y,z);
108 l:=0;r:=1000000000;
109 while l<r do
110 begin
111 mid:=(l+r)>>1;
112 if query(1,x,y,mid)>=z then r:=mid else l:=mid+1;
113 end;
114 writeln(l);
115 end;
116 procedure init;
117 begin
118 readln(n,m);
119 for i:=1 to n do read(a[i]);readln;
120 build(1,1,n);
121 end;
122 procedure main;
123 begin
124 for i:=1 to m do
125 begin
126 read(ch);//writeln(i,‘ ‘,ch);
127 case ch of
128 ‘C‘:solvechange;
129 ‘Q‘:solveask;
130 end;
131 end;
132 end;
133 begin
134 assign(input,‘input.txt‘);assign(output,‘output.txt‘);
135 reset(input);rewrite(output);
136 init;
137 main;
138 close(input);close(output);
139 end.