我的思路:利用二进制储存每个区间的颜色种类数
WA 本蒟蒻不知道问题在哪里,求大佬帮忙
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+10;
int n,t,o;
struct segtree
{
int lazytag;
int sum;
int L,R;
}tr[N<<2];
int change(int a)
{
int x=a,s=0;
while(x)
{
s+=x%2;
x/=2;
}
return s;
}
int k(int p)
{
int cnt=1;
for(int i=1;i<=p;i++) cnt*=2;
return cnt;
}
void pushdown(int p)
{
if(!tr[p].lazytag) return;
int la=tr[p].lazytag;
tr[p<<1].sum=la;
tr[p<<1].lazytag=la;
tr[p<<1|1].sum=la;
tr[p<<1|1].lazytag=la;
tr[p].lazytag=0;
return;
}
void pushup(int p){tr[p].sum=tr[p<<1].sum|tr[p<<1|1].sum;}
void build(int p,int l,int r)
{
tr[p].L=l;
tr[p].R=r;
tr[p].lazytag=0;
if(l==r) tr[p].sum=1;
else
{
int mid=(l+r)>>1;
build(p<<1,l,mid);
build(p<<1|1,mid+1,r);
pushup(p);
}
}
void update(int p,int l,int r,int v)
{
if(tr[p].L>=l&&tr[p].R<=r)
{
int t=k(v-1);
tr[p].lazytag=t;
tr[p].sum=t;
return;
}
pushdown(p);
int mid=(tr[p].L+tr[p].R)>>1;
if(l<=mid) update(p<<1,l,r,v);
if(r>mid) update(p<<1|1,l,r,v);
pushup(p);
}
int query(int p,int l,int r)
{
if(tr[p].L>=l&&tr[p].R<=r) return tr[p].sum;
pushdown(p);
int mid=(tr[p].L+tr[p].R)>>1;
int v=0;
if(l<=mid) v|=query(p<<1,l,r);
if(r>mid) v|=query(p<<1|1,l,r);
return v;
}
void work()
{
cin>>n>>t>>o;
build(1,1,n);
char ch;
while(o--)
{
int a,b,c;
cin>>ch>>a>>b;
if(ch=='C')
{
cin>>c;
update(1,a,b,c);
}
else cout<<change(query(1,a,b))<<'\n';
}
}
int main()
{
work();
}