虽然我知道这么水的题是不应该写题解的。。但是长时间不写的话 感觉自己好像好久没刷过题了。。
就是裸的线段树
一共30种颜色,一开始我每个节点用个 color[30]来记录...结果T了
然后想到用位运算,然后A了。
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <iostream>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define MAX 101000
int L,T,O;
int color[MAX<<2];
int data[MAX<<2];
void PushUp(int rt)
{
data[rt]=data[rt<<1]|data[rt<<1|1];
}
void PushDown(int rt)
{
if(color[rt])
{
int &l=data[rt<<1];
int &r=data[rt<<1|1];
l=color[rt];
r=color[rt];
color[rt<<1]=color[rt];
color[rt<<1|1]=color[rt];
color[rt]=0;
}
}
int query(int a,int b,int l,int r,int rt)
{
if(a>r||b<l)
return 0;
//printf("%d %d %d %d %d \n",a,b,l,r,rt);
if(a<=l&&r<=b)
return data[rt];
PushDown(rt);
int m=(l+r)>>1;
int v1=query(a,b,lson);
int v2=query(a,b,rson);
PushUp(rt);
return v1|v2;
}
void update(int a,int b,int c,int l,int r,int rt)
{
if(a>r||b<l)
return ;
//printf("%d %d %d %d %d %d\n",a,b,c,l,r,rt);
if(a<=l&&r<=b)
{
data[rt]=0;
data[rt]|=c;
color[rt]=c;
return ;
}
PushDown(rt);
int m=(l+r)>>1;
update(a,b,c,lson);
update(a,b,c,rson);
PushUp(rt);
}
void build(int l,int r,int rt)
{
if(l==r)
{
data[rt]=1<<1;
return ;
}
int m=(l+r)>>1;
build(lson);
build(rson);
PushUp(rt);
}
int main()
{
freopen("acm.in","r",stdin);
while(scanf("%d%d%d",&L,&T,&O)!=EOF)
{
memset(color,0,sizeof(color));
memset(data,0,sizeof(data));
build(1,L,1);
while(O--)
{
char o[3];
scanf("%s",&o);
int f,t,c;
if(o[0]=='C')
{
scanf("%d%d%d",&f,&t,&c);
if(f>t)
swap(f,t);
c=1<<c;
update(f,t,c,1,L,1);
}
else
{
scanf("%d%d",&f,&t);
if(f>t)
swap(f,t);
int ans=query(f,t,1,L,1);
int cnt=0;
for(int i=1;i<=30;i++)
if((1<<i)&ans)
cnt++;
printf("%d\n",cnt);
}
}
}
return 0;
}