bzoj 1493: [NOI2007]项链工厂(线段树)

时间:2023-12-25 22:44:25

1493: [NOI2007]项链工厂

Time Limit: 30 Sec  Memory Limit: 64 MB
Submit: 1256  Solved: 545
[Submit][Status][Discuss]

Description

bzoj 1493: [NOI2007]项链工厂(线段树)bzoj 1493: [NOI2007]项链工厂(线段树)

Input


入文件第一行包含两个整数N, c,分别表示项链包含的珠子数目以及颜色 数目。第二行包含N 个整数,x1, x2…, xn,表示从位置1 到位置N
的珠子的颜色, 1 ≤ xi ≤ c。第三行包含一个整数Q,表示命令数目。接下来的Q 行每行一条命令, 如上文所述。

Output

对于每一个C 和CS 命令,应输出一个整数代表相应的答案。

Sample Input

5 3
1 2 3 2 1
4
C
R 2
P 5 5 2
CS 4 1

Sample Output

4
1

HINT

bzoj 1493: [NOI2007]项链工厂(线段树)bzoj 1493: [NOI2007]项链工厂(线段树) 对于60%的数据,N ≤ 1 000,Q ≤ 1 000; 对于100%的数据,N ≤ 500 000,Q ≤ 500 000,c ≤ 1 000。

【思路】

线段树。

R F之后虽然珠子的顺序变了,但是相对顺序是不变的。

我们用MOV记录顺时针旋转量,F记录是否翻转。

如果翻转则MOV-=k,否则MOV+=k。然后用MOV计算出查询位置对应的原位置,在原位置上进行操作。

剩下的就是区间上的颜色修改以及统计颜色段数的问题了,可以用线段树完成。

注意珠子是环形的,别忘比较一下1和n的颜色。

【代码】

 #include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define FOR(a,b,c) for(int a=(b);a<=(c);a++)
using namespace std; const int N = *1e5+;
int n,m,c,MOV,F,co[N];
//SEGMENT TREE
struct Trie{ int l,r,s,c,lc,rc,setv; }T[N*];
void pushdown(int u) {
if(T[u].l<T[u].r && T[u].setv!=-) {
int lc=u<<,rc=lc|;
T[lc].c=T[rc].c=T[lc].setv=T[rc].setv=T[u].setv;
T[lc].lc=T[lc].rc=T[rc].lc=T[rc].rc=T[u].setv;
T[lc].s=T[rc].s=;
T[u].setv=-;
}
}
void maintain(int u) {
int lc=u<<,rc=lc|;
T[u].s=T[lc].s+T[rc].s-(T[lc].rc==T[rc].lc);
T[u].lc=T[lc].lc,T[u].rc=T[rc].rc;
}
void build(int u,int L,int R) {
T[u].l=L,T[u].r=R; T[u].setv=-;
if(L==R) {
T[u].c=T[u].lc=T[u].rc=co[L]; T[u].s=;
return ;
}
int M=(L+R)>>;
build(u<<,L,M) , build(u<<|,M+,R);
maintain(u);
}
void update(int u,int L,int R,int x) {
pushdown(u);
if(L<=T[u].l && T[u].r<=R) {
T[u].setv=T[u].c=T[u].lc=T[u].rc=x;
T[u].s=;
}
else {
int M=(T[u].l+T[u].r)>>;
if(L<=M) update(u<<,L,R,x);
if(M<R) update(u<<|,L,R,x);
maintain(u);
}
}
int query(int u,int L,int R) {
pushdown(u);
if(L==T[u].l && T[u].r==R) {
return T[u].s;
}
else {
int M=(T[u].l+T[u].r)>>;
if(R<=M) return query(u<<,L,R);
else if(L>M) return query(u<<|,L,R);
else {
int ans=query(u<<,L,M);
ans+=query(u<<|,M+,R);
ans-=T[u<<].rc==T[u<<|].lc;
return ans;
}
}
}
int getcolor(int u,int x) {
pushdown(u);
if(T[u].l==T[u].r) return T[u].c;
else {
int M=(T[u].l+T[u].r)>>;
if(x<=M) return getcolor(u<<,x);
else return getcolor(u<<|,x);
}
} int get(int x) {
if(F) x=n+-x;
x-=MOV;
while(x<) x+=n;
while(x>n) x-=n;
return x;
}
void read(int& x) {
char c=getchar(); x=; int f=;
while(!isdigit(c)){if(c=='-')f=-;c=getchar();}
while(isdigit(c)) x=x*+c-'',c=getchar();
}
int main() {
freopen("in.in","r",stdin);
freopen("out.out","w",stdout);
read(n),read(c);
int x,a,b,c; char s[];
for(int i=;i<=n;i++) read(co[i]);
build(,,n);
read(m);
while(m--) {
scanf("%s",s);
if(s[]=='R') {
read(a);
if(F) MOV-=a; else MOV+=a;
while(MOV<) MOV+=n;
while(MOV>n) MOV-=n;
} else
if(s[]=='F') F^=; else
if(s[]=='S') {
read(a),read(b);
a=get(a),b=get(b);
int c1=getcolor(,a),c2=getcolor(,b);
update(,a,a,c2),update(,b,b,c1);
} else
if(s[]=='P') {
read(a),read(b),read(c);
a=get(a),b=get(b);
if(F) swap(a,b);
if(a>b) update(,a,n,c),update(,,b,c);
else update(,a,b,c);
} else
if(s[] && strlen(s)==) {
int ans=query(,,n);
if(ans>&&T[].lc==T[].rc) ans--;
printf("%d\n",ans);
} else {
read(a),read(b);
a=get(a),b=get(b);
if(F) swap(a,b);
int ans;
if(a<=b) {
ans=query(,a,b);
if(a==&&b==n&&ans>&&T[].lc==T[].rc) ans--;
} else {
ans=query(,a,n)+query(,,b);
if(T[].lc==T[].rc) ans--;
}
printf("%d\n",ans);
}
}
return ;
}