1 (*
2 Problem: NOI2007 项链工厂
3 Author: Chen Yang
4 Time: 2012.5.29 3:50 pm
5 State: Solved
6 Memo: 线段树、坐标变换
7 Lesson: To try
8 *)
9 program necklace;
10 uses math;
11 const maxn=500100;
12 maxm=2000000;
13 type
14 lt=record
15 l,r,sum,rc,lc:longint;
16 lazy:boolean;
17 end;
18 var
19 n,col,i,j,one,fs,q,k,t,k1:longint;
20 t1,t2:lt;
21 p:char;
22 c:array[0..maxn] of longint;
23 s:array[0..maxm] of lt;
24 //=======================
25 procedure maintain(var a,b,c:lt); inline;
26 begin
27 a.sum:=b.sum+c.sum;
28 if b.rc=c.lc then dec(a.sum);
29 a.lc:=b.lc; a.rc:=c.rc;
30 if a.sum=1 then a.lazy:=true else a.lazy:=false;
31 end;
32 //=======================
33 procedure pushlazy(x:longint); inline;
34 var
35 ls,rs:longint;
36 begin
37 ls:=x shl 1; rs:=x shl 1+1;
38 s[ls].lazy:=true; s[ls].sum:=1;
39 s[ls].lc:=s[x].lc; s[ls].rc:=s[x].rc;
40 s[rs].lazy:=true; s[rs].sum:=1;
41 s[rs].lc:=s[x].lc; s[rs].rc:=s[x].rc;
42 end;
43 //=======================
44 procedure built(x,l,r:longint);
45 var
46 m:longint;
47 begin
48 s[x].l:=l; s[x].r:=r;
49 if l=r then
50 begin
51 s[x].lc:=c[l]; s[x].rc:=c[r];
52 s[x].sum:=1; s[x].lazy:=true;
53 exit;
54 end;
55 s[x].sum:=0; s[x].lazy:=false;
56 s[x].lc:=0; s[x].rc:=0;
57 m:=(l+r)>>1;
58 built(x shl 1,l,m);
59 built(x shl 1+1,m+1,r);
60 maintain(s[x],s[x shl 1],s[x shl 1+1]);
61 end;
62 //=======================
63 function find_col(x,p:longint):longint;
64 var
65 m:longint;
66 begin
67 if (s[x].l<=p)and(p<=s[x].r)and s[x].lazy then exit(s[x].lc);
68 if s[x].lazy then pushlazy(x);
69 m:=(s[x].l+s[x].r)>>1;
70 if p<=m then exit(find_col(x shl 1,p)) else
71 if p>m then exit(find_col(x shl 1+1,p));
72 end;
73 //=======================
74 function find(x,l,r:longint):lt;
75 var
76 m:longint;
77 t1,t2,t:lt;
78 begin
79 if (l<=s[x].l)and(s[x].r<=r)and s[x].lazy then exit(s[x]);
80 if s[x].lazy then pushlazy(x);
81 m:=(s[x].l+s[x].r)>>1;
82 if r<=m then exit(find(x shl 1,l,r)) else
83 if l>m then exit(find(x shl 1+1,l,r)) else
84 begin
85 t1:=find(x shl 1,l,r); t2:=find(x shl 1+1,l,r);
86 maintain(t,t1,t2);
87 end;
88 exit(t);
89 end;
90 //=======================
91 procedure change(x,l,r,c:longint);
92 var
93 m:longint;
94 begin
95 if (l<=s[x].l)and(s[x].r<=r) then
96 begin
97 s[x].lazy:=true; s[x].sum:=1;
98 s[x].lc:=c; s[x].rc:=c;
99 exit;
100 end;
101 if s[x].lazy then pushlazy(x);
102 m:=(s[x].l+s[x].r)>>1;
103 if r<=m then change(x shl 1,l,r,c) else
104 if l>m then change(x shl 1+1,l,r,c) else
105 begin
106 change(x shl 1,l,r,c); change(x shl 1+1,l,r,c);
107 end;
108 maintain(s[x],s[x shl 1],s[x shl 1+1]);
109 end;
110 //=======================
111 begin
112 assign(input,'necklace.in'); reset(input);
113 assign(output,'necklace.out'); rewrite(output);
114 read(n,col);
115 for i:=1 to n do read(c[i]); c[0]:=c[n];
116 built(1,0,n-1);
117 one:=1; fs:=1;
118 readln(q);
119 for i:=1 to q do
120 begin
121 read(p);
122 case p of
123 'R':begin
124 readln(k);
125 one:=(one+n-fs*k) mod n;
126 end;
127 'F':begin
128 fs:=-fs; readln;
129 end;
130 'S':begin
131 readln(j,k); j:=(one+j*fs-fs) mod n; k:=(one+k*fs-fs) mod n;
132 if j=k then continue;
133 t:=find_col(1,k); k1:=find_col(1,j);
134 if t=k1 then continue;
135 change(1,j,j,t); change(1,k,k,k1);
136 end;
137 'P':begin
138 readln(j,k,t); j:=(one+j*fs-fs) mod n; k:=(one+k*fs-fs) mod n;
139 if fs=1 then
140 begin
141 if k>=j then change(1,j,k,t) else
142 begin
143 change(1,j,n-1,t); change(1,0,k,t);
144 end;
145 end else
146 begin
147 if j>=k then change(1,k,j,t) else
148 begin
149 change(1,k,n-1,t); change(1,0,j,t);
150 end;
151 end;
152 end;
153 'C':begin
154 read(p);
155 if p='S' then
156 begin
157 readln(j,k); j:=(one+j*fs-fs) mod n; k:=(one+k*fs-fs) mod n; t:=1;
158 if fs=1 then
159 begin
160 if k>=j then t:=find(1,j,k).sum
161 else begin
162 t1:=find(1,j,n-1); t2:=find(1,0,k);
163 t:=t1.sum+t2.sum;
164 if t1.rc=t2.lc then dec(t);
165 end;
166 end else
167 begin
168 if j>=k then t:=find(1,k,j).sum
169 else begin
170 t1:=find(1,k,n-1); t2:=find(1,0,j);
171 t:=t1.sum+t2.sum;
172 if t1.rc=t2.lc then dec(t);
173 end;
174 end;
175 writeln(t);
176 end else
177 begin
178 t:=find(1,0,n-1).sum;
179 if (t>1)and(s[1].lc=s[1].rc) then dec(t);
180 writeln(t);
181 end;
182 end;
183 end;
184 end;
185 close(input); close(output);
186 end.