POJ2777 Count Color
? 解题记录 ? ? POJ ? ? 线段树 ? ? 状态压缩 ?    2017-10-01 14:27:00    544    0    0

 

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​​

 

区间覆盖问题首先可以想到线段树。但是如果直接按有多少种颜色作为节点键值的话很快就会发现是无法从儿子获得信息的。考虑到颜色只有30种,我们可以用一个二进制数的2^i为1来表示有i这种颜色,最后查询时返回记载哪些颜色出现过的二进制数,然后数有多少位为1即可。

之所以贴这道题是因为它与DP类似的核心的思想——把直接获得答案转换为表示某个答案或查找某个答案可不可行。间接地使得信息可以向上一层传递。

#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn=100007;

struct node
{
	int l,r;
	int color;//状态压缩 
	int covered; 
}tree[maxn*4];

void build_tree(int l,int r,int rt)
{
	tree[rt].l=l;tree[rt].r=r;
	tree[rt].color=1;
	tree[rt].covered=1;
	if(l==r)	return;
	int m=(l+r)/2;
	build_tree(l,m,rt*2);
	build_tree(m+1,r,rt*2+1);
}

void pushup(int rt)
{
	tree[rt].color=tree[rt*2].color | tree[rt*2+1].color;
}

void pushdown(int rt)
{
	if(tree[rt].covered)
	{
		tree[rt*2].color=tree[rt].color;
		tree[rt*2].covered=1;
		tree[rt*2+1].color=tree[rt].color;
		tree[rt*2+1].covered=1;
		tree[rt].covered=0;
	}
}

void update(int l,int r,int clr,int rt)/*处理过的颜色*/
{
	int tl=tree[rt].l,tr=tree[rt].r;
	if(l==tl && r==tr)
	{
		tree[rt].color=clr; //赋值 
		tree[rt].covered=1;
		return; 
	}
	pushdown(rt);
	int m=(tl+tr)/2;
	if(r<=m)	update(l,r,clr,rt*2);
	else if(l>m)	update(l,r,clr,rt*2+1);
	else{
		update(l,m,clr,rt*2);
		update(m+1,r,clr,rt*2+1);
	}
	pushup(rt);
}

int query(int l,int r,int rt)
{
	int tl=tree[rt].l,tr=tree[rt].r;
	if(tl==l && tr==r)
		return tree[rt].color;
	if(tree[rt].covered)
		return tree[rt].color;
	int m=(tl+tr)/2;
	if(r<=m)	return query(l,r,rt*2);
	else if(l>m)	return query(l,r,rt*2+1);
	else{
		int p1,p2;
		p1=query(l,m,rt*2);
		p2=query(m+1,r,rt*2+1);
		return p1 | p2;
	}
}

int cnt(int num)
{
	int ans=0;
	while(num!=0)
	{
		if(num&1)	
			ans++;
		num=num>>1;
	}
	return ans;
}

int main()
{
	int t,l,o;
	scanf("%d%d%d",&l,&t,&o);
	char c;
	build_tree(1,l,1);
	for(int i=1;i<=o;i++)
	{
		scanf(" %c",&c);
		if(c=='C')
			{
				int l,r,clr;
				scanf("%d%d%d",&l,&r,&clr);
				if(l>r) swap(l,r);
				update(l,r,1<<(clr-1),1);
			}
		else
			{
				int l,r;
				scanf("%d%d",&l,&r);
				if(l>r) swap(l,r);
				printf("%d\n",cnt(query(l,r,1)));
			}
	}
	return 0;
}

 

上一篇: 洛谷P1379 八数码难题 (A* + 康托展开)

下一篇: BZOJ1787 Meet 紧急集合

544 人读过
立即登录, 发表评论.
没有帐号? 立即注册
0 条评论
文档导航