http://poj.org/problem?id=2777

Description

Chosen Problem Solving and Program design as an optional course, you are required to solve all kinds of problems. Here, we get a new problem.

There is a very long board with length L centimeter, L is a positive integer, so we can evenly divide the board into L segments, and they are labeled by 1, 2, ... L from left to right, each is 1 centimeter long. Now we have to color the board - one segment with only one color. We can do following two operations on the board:

1. "C A B C" Color the board from segment A to segment B with color C.
2. "P A B" Output the number of different colors painted between segment A and segment B (including).

In our daily life, we have very few words to describe a color (red, green, blue, yellow…), so you may assume that the total number of different colors T is very small. To make it simple, we express the names of colors as color 1, color 2, ... color T. At the beginning, the board was painted in color 1. Now the rest of problem is left to your.

Input

First line of input contains L (1 <= L <= 100000), T (1 <= T <= 30) and O (1 <= O <= 100000). Here O denotes the number of operations. Following O lines, each contains "C A B C" or "P A B" (here A, B, C are integers, and A may be larger than B) as an operation defined previously.

Output

Ouput results of the output operation in order, each line contains a number.

Sample Input

2 2 4
C 1 1 2
P 1 2
C 2 2 2
P 1 2

Sample Output

2
1

题意:

有长度为n的序列和30种颜色,要求一种数据结构,支持两种操作:

①C  a b c :将区间[a,b]涂为c色

②P a b :问区间[a,b]的颜色种类数

思路:

颜色有30种,压位到一个int里就够了,用线段树+懒标记来维护。

cover[o]:如果只执行o结点及其子结点的setv标记,而不考虑o结点以上结点的setv标记,o结点对应区间的颜色覆盖状况。

setv[o]:标记o结点对应区间覆盖,如果o及其子结点都有setv标记,那么只考虑o结点的。

PS:一开始想只用一个cover,不用setv,区间覆盖时也直接修改cover。这样看似行得通,实际上是不行的,比如现在[1,8]中[1,4]有1,2颜色,[5,8]有3,4颜色,[1,8]cover标记4种颜色,现在要更新[5,8]区间涂成5色,然后标记下传就出问题了,直接把[1,8]的4种颜色传到[1,4]和[5,8],然后[5,8]标记5色,maintain时[1,8]区间变成了含有1,2,3,4,5共5种颜色,错了。

#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
#define maxn (100000+100)

int n,t,q;
int cover[maxn*4],setv[maxn*4];

void build()
{
	memset(setv,-1,sizeof(setv));
	setv[1]=(1<<1);
	cover[1]=(1<<1);
}

void pushdown(int o)
{
	if(setv[o]!=-1)
	{
		setv[o*2]=setv[o*2+1]=setv[o];
		setv[o]=-1;
	}
}

void maintain(int o,int l,int r)
{
	if(setv[o]!=-1)cover[o]=setv[o];
	else cover[o]=cover[o*2]|cover[o*2+1];
}

int ql,qr,v;
void update(int o,int l,int r)
{
	int lc=o*2,rc=o*2+1;
	if(ql<=l&&r<=qr)setv[o]=(1<<v);
	else
	{
		pushdown(o);
		int m=(l+r)/2;
		if(ql<=m)update(lc,l,m); else maintain(lc,l,m);
		if(qr>m)update(rc,m+1,r); else maintain(rc,m+1,r);
	}
	maintain(o,l,r);
}

int query(int o,int l,int r)
{
	int lc=o*2,rc=o*2+1;
	int m=(l+r)/2;
	if(setv[o]!=-1)return cover[o]; 
	else if(ql<=l&&qr>=r)return cover[o];
	else 
	{
		int ans=0;
		if(ql<=m)ans|=query(lc,l,m);
		if(qr>m)ans|=query(rc,m+1,r);
		return ans;
	}
}

void debug(int o,int l,int r)
{
	printf("%d-%d:cover:%d\n",l,r,cover[o]);
	int m=(l+r)/2;
	if(l<r)debug(o*2,l,m),debug(o*2+1,m+1,r);
}

int main()
{
	freopen("input.in","r",stdin);
	char com[2];
	cin>>n>>t>>q;
	build();
	while(q--)
	{
		scanf("%s%d%d",com,&ql,&qr);
		if(com[0]=='C')
		{
			scanf("%d",&v);
			update(1,1,n);
		}
		else 
		{
			int ans=query(1,1,n);
			int num=0;
			for(int i=1;i<=30;i++)if(ans&(1<<i))num++;
			cout<<num<<"\n";
		}
	}
//	debug(1,1,n);
	return 0;
}