您的位置:首页 > 其它

【POJ】2777 Count Color

2012-05-22 17:29 267 查看
#include<cstdio>
#include<cstring>
#define MAXN 100010
int cnt;
struct node
{
int bit,val;
};
node tree[MAXN<<2];
inline void swap(int &x,int &y)
{
int temp=x;
x=y;
y=temp;
}
inline void PushUp(int rt)
{
tree[rt].bit=tree[rt<<1].bit|tree[rt<<1|1].bit;
}
inline void PushDown(int rt)
{
if(tree[rt].val)
{
tree[rt<<1]=tree[rt<<1|1]=tree[rt];
tree[rt].val=0;
}
}
void Update(int x,int y,int val,int L,int R,int rt)
{
if(x<=L&&R<=y)
{
tree[rt].val=val;
tree[rt].bit=1<<(val-1);
}
else
{
int mid=(L+R)>>1;
PushDown(rt);
if(mid>=x)
Update(x,y,val,L,mid,rt<<1);
if(y>mid)
Update(x,y,val,mid+1,R,rt<<1|1);
PushUp(rt);
}
}
void Query(int x,int y,int L,int R,int rt)
{
if(x<=L&&R<=y)
cnt|=tree[rt].bit;
else
{
int mid=(L+R)>>1;
PushDown(rt);
if(mid>=x)
Query(x,y,L,mid,rt<<1);
if(y>mid)
Query(x,y,mid+1,R,rt<<1|1);
}
}
int main()
{
char ch;
int n,t,q,a,b,c,ans,i;
while(~scanf("%d%d%d",&n,&t,&q))
{
memset(tree,0,sizeof(tree));
Update(1,n,1,1,n,1);
while(q--)
{
scanf(" %c%d%d",&ch,&a,&b);
if(a>b)
swap(a,b);
if(ch=='C')
{
scanf("%d",&c);
Update(a,b,c,1,n,1);
}
else
{
cnt=0;
Query(a,b,1,n,1);
for(i=ans=0;i<t;i++)
{
if(cnt&(1<<i))
ans++;
}
printf("%d\n",ans);
}
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: