解题思路:这道题利用了线段树+位运算的思想,由于颜色的种类只有30种,所以int可以存下来,所以我们在线段树的节点里面加上status的状态信息,表示这段区间内的颜色信息,而且我们可以知道,父节点的status是两个儿子节点的status异或得到的,同时为了能够提高查找效率,还要设置一个信息cover,cover=1表示这段区间只有一种颜色,cover=0表示有多种颜色被覆盖了,所以在找到cover=1的节点时直接回溯(因为子节点都是一种颜色,不需要再去查找)。



#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;

const int maxn = 100005;
struct Segment
{
	int l,r;
	int status;
	int cover;
}tree[maxn<<2];
int n,color,m;

void build(int rt,int l,int r)
{
	tree[rt].l = l, tree[rt].r = r;
	tree[rt].status = tree[rt].cover = 1;
	if(l == r) return;
	int mid = (l + r) >> 1;
	build(rt<<1,l,mid);
	build(rt<<1|1,mid+1,r);
}

void update(int rt,int l,int r,int val)
{
	if(l <= tree[rt].l && tree[rt].r <= r)
	{
		tree[rt].status = 1 << (val - 1);
		tree[rt].cover = 1;
		return;
	}
	if(tree[rt].cover)
	{
		tree[rt<<1].cover = tree[rt<<1|1].cover = tree[rt].cover;
		tree[rt<<1].status = tree[rt<<1|1].status = tree[rt].status;
		tree[rt].cover = 0;
	}
	int mid = (tree[rt].l + tree[rt].r) >> 1;
	if(mid >= l) update(rt<<1,l,r,val);
	if(mid < r) update(rt<<1|1,l,r,val);
	tree[rt].status = tree[rt<<1].status | tree[rt<<1|1].status;
	if(tree[rt<<1].cover && tree[rt<<1|1].cover && tree[rt<<1].status == tree[rt<<1|1].status)
		tree[rt].cover = 1;
}

int query(int rt,int l,int r)
{
	if(l <= tree[rt].l && tree[rt].r <= r)
		return tree[rt].status;
	if(tree[rt].cover) return tree[rt].status;
	int mid = (tree[rt].l + tree[rt].r) >> 1;
	int ans = 0;
	if(l <= mid) ans |= query(rt<<1,l,r);
	if(mid < r) ans |= query(rt<<1|1,l,r);
	return ans;
}

int main()
{
	char ch;
	int a,b,c;
	while(scanf("%d%d%d",&n,&color,&m)!=EOF)
	{
		build(1,1,n);
		while(m--)
		{
			getchar();
			scanf("%c",&ch);
			if(ch == 'C')
			{
				scanf("%d%d%d",&a,&b,&c);
				if(a > b) swap(a,b);
				update(1,a,b,c);
			}
			else
			{
				scanf("%d%d",&a,&b);
				if(a > b) swap(a,b);
				int ans = query(1,a,b),sum = 0;
				for(int i = 0; i < color; i++)
					if(ans & (1 << i))
						sum++;
				printf("%d\n",sum);
			}
		}
	}
	return 0;
}