线段树统计颜色
先压位存储 类似一个bitset
输出答案的时候看看有几个1就好了
pushup的话或一下左右区间
#include<iostream>
#include<cstring>
using namespace std;
const int N = 1e6+10;
struct Segment{
int l,r,id,lz;
}tr[N<<2];
void pushup(int u){
tr[u].id = tr[u<<1].id | tr[u<<1|1].id;
}
void pushdown(int u){
if(tr[u].lz){
tr[u<<1].lz = tr[u].lz;
tr[u<<1|1].lz = tr[u].lz;
tr[u<<1].id = tr[u].id;
tr[u<<1|1].id = tr[u].id;
tr[u].lz = 0;
}
}
void build(int u,int l,int r){
tr[u] = {l,r,1,0};
if(l==r)return;
int mid = l+r>>1;
build(u<<1,l,mid),build(u<<1|1,mid+1,r);
pushup(u);
}
void modify(int u,int l,int r,int c){
if(l<=tr[u].l&&tr[u].r<=r){
tr[u].id = c;tr[u].lz = 1;return;
}
pushdown(u);
int mid = tr[u].l+tr[u].r>>1;
if(l<=mid)modify(u<<1,l,r,c);
if(r>mid)modify(u<<1|1,l,r,c);
pushup(u);
}
int query(int u,int l,int r){
if(l<=tr[u].l&&tr[u].r<=r)return tr[u].id;
pushdown(u);
int mid = tr[u].l+tr[u].r>>1;
int ans = 0;
if(l<=mid)ans|=query(u<<1,l,r);
if(r>mid)ans|=query(u<<1|1,l,r);
return ans;
}
int lowbit(int x){return x&-x;}
int cal(int x){
int res = 0;
for(int i=x;i;i-=lowbit(i))res++;
return res;
}
int main()
{
int n,m,q;
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
cin>>n>>m>>q;
build(1,1,n);
while(q--){
char c;cin>>c;int l,r;cin>>l>>r;
if(l>r)swap(l,r);
if(c=='C'){
int x;cin>>x;
modify(1,l,r,1<<(x-1));
}else{
cout<<cal(query(1,l,r))<<"\n";
}
}
return 0;
}