POJ 2777(线段树,区间更新,使用二进制染色问题)

Count Color
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 41379   Accepted: 12507

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

Source

POJ Monthly--2006.03.26,dodo



题意:有一条线段,有O次操作,每次回给一段线段染色,每次查询一段线段的颜色种数



题解:开始不知道用什么去表示颜色的种数,查了题解才发现,要利用神奇的二进制,按位保存,因为颜色种数不超过30,使用int就可以保存啦,接下来就是线段树维护一下区间更新与查询就可以啦,这里顺便复习了一下bitset的函数。


reset()清空对象,所有位置为0

count()输出1的个数



#include<cstdio>  
#include<cstring>  
#include<cstdlib>  
#include<cmath>  
#include<iostream>  
#include<algorithm>  
#include<vector>  
#include<map>  
#include<set>  
#include<queue>  
#include<string>  
#include<bitset>  
#include<utility>  
#include<functional>  
#include<iomanip>  
#include<sstream>  
#include<ctime>  
using namespace std;  
  
#define N int(1e5)  
#define inf int(0x3f3f3f3f)  
#define mod int(1e9+7)  
typedef long long LL;  
#define lson L,mid,rt<<1
#define rson mid+1,R,rt<<1|1
  
  
#ifdef CDZSC  
#define debug(...) fprintf(stderr, __VA_ARGS__)  
#else  
#define debug(...)   
#endif  
  
struct segment
{
	int lc,rc,add,va;
	int mid(){return (lc+rc)>>1;}
}tree[N<<2];
bitset<40>ans;
void pushup(int rt)
{
	tree[rt].va=(tree[rt<<1].va|tree[rt<<1|1].va);
}
void pushdown(int rt)
{
	if(tree[rt].add)
	{
		tree[rt<<1|1].va=tree[rt<<1].va=tree[rt].va;
		tree[rt<<1|1].add=tree[rt<<1].add=1;
		tree[rt].add=0;
	}
}

void build(int L,int R,int rt)
{
	tree[rt].lc=L;
	tree[rt].rc=R;
	tree[rt].add=0;
	tree[rt].va=2;
	if(R==L)
	{		
		return ;
	}
	int mid=tree[rt].mid();
	build(lson);
	build(rson);
}

void update(int L,int R,int rt,int va)
{
	if(tree[rt].lc==L&&tree[rt].rc==R)
	{
		tree[rt].va=(1<<va);
		tree[rt].add=1;
		return ;
	}
	pushdown(rt);
	int mid=tree[rt].mid();
	if(R<=mid)update(L,R,rt<<1,va);
	else if(L>mid)update(L,R,rt<<1|1,va);
	else
	{
		update(lson,va);
		update(rson,va);
	}
	pushup(rt);
}
void query(int L,int R,int rt)
{
	if(tree[rt].lc==L&&tree[rt].rc==R)
	{
		ans|=tree[rt].va;
		return ;
	}
	pushdown(rt);
	int mid=tree[rt].mid();
	if(R<=mid)query(L,R,rt<<1);
	else if(L>mid)query(L,R,rt<<1|1);
	else
	{
		query(lson);
		query(rson);
	}
}
int main()  
{  
#ifdef CDZSC  
    freopen("i.txt", "r", stdin);  
    //freopen("o.txt","w",stdout);  
    int _time_jc = clock();  
#endif 
	char s[10]; 
	int n,m,t,x,y,z;
	while(~scanf("%d%d%d",&n,&t,&m))
	{
		build(1,n,1);
		while(m--)
		{
			scanf("%s",s);
			if(s[0]=='C')
			{
				scanf("%d%d%d",&x,&y,&z);
				if(x>y)swap(x,y);
				update(x,y,1,z);
			}
			else
			{
				ans.reset();
				scanf("%d%d",&x,&y);
				if(x>y)swap(x,y);
				query(x,y,1);
				printf("%d\n",ans.count());
			}
		}
	}
#ifdef CDZSC  
        debug("time: %d\n", int(clock() - _time_jc));  
#endif  
    return 0;  
}  






你可能感兴趣的:(POJ 2777(线段树,区间更新,使用二进制染色问题))