poj 2777

时间:2023-03-09 19:15:25
poj 2777

题意:两个操作:c l r x   l到r之间的颜色变成x

q l r      询问l到r有多少种颜色

思路:记一个整数表示哪种颜色是否取了

这里真的是煞笔了,看到这一题第一直觉是异或,但是A^A=0,相同的肿么办..然后搜题解....反应了一个下午,发现有按位或这样神气的存在

        1|1=1
        1|0=1
        0|1=1
        0|0=0
代码
#include "stdio.h"
#include "string.h"
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#include "algorithm"
#define MAX 100010
using namespace std;
int sum[MAX<<],col[MAX<<];
void pushup(int rt)
{
sum[rt]=(sum[rt<<]|sum[rt<<|]);
}
void pushdown(int rt)
{
if(col[rt])
{
col[rt<<]=col[rt<<|]=col[rt];
sum[rt<<]=sum[rt<<|]=<<(col[rt]-);
col[rt]=;
}
}
void build(int l,int r,int rt)
{
col[rt]=;
if(l==r)
{
sum[rt]=;
return;
}
int m=(l+r)>>;
build(lson);
build(rson);
pushup(rt);
}
void update(int L,int R,int c,int l,int r,int rt)
{
if(l>=L&&r<=R)
{
col[rt]=c;
sum[rt]=<<(c-);
return;
}
pushdown(rt);
int m=(l+r)>>;
if(L<=m)
update(L,R,c,lson);
if(R>m)
update(L,R,c,rson);
pushup(rt);
}
int query(int L,int R,int l,int r,int rt)
{
if(l>=L&&r<=R)
{
return sum[rt];
}
pushdown(rt);
int ans=;
int m=(l+r)>>;
if(L<=m)
ans=(ans|query(L,R,lson));
if(R>m)
ans=(ans|query(L,R,rson));
return ans;
}
int main()
{
int n,l,r,col,x;
int q;
char s[];
while(scanf("%d%d%d",&n,&col,&q)==)
{
build(,n,);
while(q--)
{
scanf("%s",s);
if(s[]=='C')
{
scanf("%d%d%d",&l,&r,&x);
if(l>r)
l^=r^=l^=r;
update(l,r,x,,n,);
}
else
{
scanf("%d%d",&l,&r);
if(l>r)
l^=r^=l^=r;
int temp=query(l,r,,n,);
int ans=;
while(temp)
{
if(temp&)
ans++;
temp/=;
}
printf("%d\n",ans);
}
}
}
return ;
}