题意: 有一个有 N 个区间木板,存在最多 30 种颜色,一开始木板的颜色都为第一种颜色。
C x y c 将区间[x,y]涂成 c 颜色。
P x y 询问区间[x,y]共有多少种颜色。
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define havemid int m=(l+r)>>1
#define left (rt<<1)
#define right (rt<<1|1)
#define clr(x) memset(x,0,sizeof(x))
const int maxn=100100;
int col[maxn<<2];
int v[40];
void pushdown(int rt){
if(col[rt]){
col[left]=col[right]=col[rt];
col[rt]=0;
}
}
void build(int l,int r,int rt){
col[rt]=1;
if(l==r)return ;
havemid;
build(lson);
build(rson);
}
void update(int L,int R,int c,int l,int r,int rt){
if(L<=l&&r<=R){
col[rt]=c;
return ;
}
if(col[rt]==c)return ;
pushdown(rt);
havemid;
if(L<=m)update(L,R,c,lson);
if(R>m)update(L,R,c,rson);
}
void query(int L,int R,int l,int r,int rt){
if(col[rt]){
v[col[rt]]=1;
return ;
}
havemid;
if(L<=m)query(L,R,lson);
if(R>m)query(L,R,rson);
}
int main(){
int L,T,O,a,b,c,i;
char s[10];
while(scanf("%d%d%d",&L,&T,&O)!=EOF){
build(1,L,1);
while(O--){
scanf("%s",s);
if(s[0]=='C'){
scanf("%d%d%d",&a,&b,&c);
update(a,b,c,1,L,1);
}
else {
scanf("%d%d",&a,&b);
clr(v);
int res=0;
query(a,b,1,L,1);
for(i=1;i<=30;i++)
if(v[i])res++;
printf("%d\n",res);
}
}
}
return 0;
}