http://poj.org/problem?id=2777
区间更新线段树。
思路:颜色总数只有30种,所以可以用一个整数的二进制形式来表示所有颜色,如第i位为1表示该区间内有颜色i。这里为了方便,第0位不计。父亲节点的值就等于两个子节点或的结果。
吐槽:忍不住骂自己一声。。刚开始看题时看到了a可能比b大,可代码写到最后就忘了。。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <stack>
#include <queue>
#define fi first
#define se second
#define lson l, m, rt<<1
#define rson m+1, r, rt<<1|1
using namespace std;
typedef long long LL;
typedef pair<int, int> P;
//head
const int maxn = 1e5+5;
int t[maxn<<2];
int lazy[maxn<<2];
void pushUp(int rt)
{
t[rt] = t[rt<<1] | t[rt<<1|1];
}
void pushDown(int rt)
{
if(lazy[rt])
{
lazy[rt<<1] = lazy[rt<<1|1] = lazy[rt];
t[rt<<1] = t[rt<<1|1] = lazy[rt];
lazy[rt] = 0;
}
}
void build(int l, int r, int rt)
{
lazy[rt] = 0;
t[rt] = 2;
if(l==r)
return;
int m = (l+r)>>1;
build(lson);
build(rson);
}
void update(int L, int R, int c, int l, int r, int rt)
{
if(L<=l && r<=R)
{
lazy[rt] = t[rt] = c;
return;
}
pushDown(rt);
int m = (l+r)>>1;
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 t[rt];
pushDown(rt);
int m = (l+r)>>1;
int ret = 0;
if(L<=m)
ret |= query(L, R, lson);
if(R>m)
ret |= query(L, R, rson);
return ret;
}
int main()
{
int L,T,O;
while(scanf("%d%d%d",&L,&T,&O)==3)
{
build(1, L, 1);
while(O--)
{
char s[2];
int a,b,c;
scanf("%s",s);
if(s[0]=='C')
{
scanf("%d%d%d",&a,&b,&c);
if(a>b)swap(a,b);
update(a, b, 1<<c, 1, L, 1);
}
else
{
scanf("%d%d",&a,&b);
if(a>b)swap(a,b);
int tmp = query(a, b, 1, L, 1);
int ans = 0;
while(tmp!=0)
{
ans += tmp&1;
tmp /= 2;
}
printf("%d\n",ans);
}
}
}
return 0;
}