Poj2777count color

阅读全文大概需要 3分钟
本文总阅读量
Posted by yjjr's blog on February 6, 2018

POJ2777Count Color

标签:线段树

http://www.sakurasake.icoc.me/nd.jsp?id=8#_np=2_327

Time Limit: 1000MS


Memory Limit: 65536K

Description

Chosen Problem Solving and Program designas 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 colorthe board - one segment with only one color. We can do following two operationson the board:

1. "C A B C" Color the board from segment A to segment B with colorC.
2. "P A B" Output the number of different colors painted betweensegment 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 Tis 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. Nowthe 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 Odenotes the number of operations. Following O lines, each contains "C A BC" or "P A B" (here A, B, C are integers, and A may be largerthan B) as an operation defined previously.

Output

Ouput results of the output operation inorder, 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

POJMonthly--2006.03.26,dodo

 

分析:颜色数T不多,只有30种,用二进制位运算表示当前区间的颜色情况,如果左子树颜色为101,右子树为011,那么父亲节点为两者的异或Xor运算,这样可以避免重复的情况

在每条命令更新时,我们需要考虑保证所有的节点都能够正确得代表段内颜色集合,那么每一次更新都需要涉及所有的父亲和孩子节点,复杂度过高

所以在这时我们引入lazy思想,当更新的区间与当前区间完全匹配时,直接在当前节点打上lazy标记,下次如果遇到lazy标记,直接push_down传递给左右两个儿子,自己标记清空,不需要做任何判断

所以,本题使用了成段更新,lazy标记,位运算加速

好厉害的样子2333

#include<iostream>
#include<cstdio>
#include<cstdlib>
#define maxn 100005
struct data
{
   int a,b;
   int color,lazy;
}tree[maxn*4];

int code(int x)
{
	return 1<<(x-1);
}
int uncode(int x)
{
	int t=0;
	while(x>0)
	{
		if((x%2)==1)t++;
		x=x/2;
	}
	return t;
}
void build(int p,int l,int r)
{
	tree[p].a=l;tree[p].b=r;
	tree[p].color=1;tree[p].lazy=0;
	if(l==r)return;
	build(p*2,l,(l+r)/2); 
	build(p*2+1,(l+r)/2+1,r);
}

void change(int p,int l,int r,int w)
{
	if(tree[p].a==l&&tree[p].b==r)
	{
		tree[p].color=w;
		if(l!=r)tree[p].lazy=1;
		return;
	}
	else
	{ 
	    if(tree[p].lazy==1)
	    {
		    tree[p*2].lazy=tree[p*2+1].lazy=1;
		    tree[p*2].color=tree[p*2+1].color=tree[p].color;
		    tree[p].lazy=0;
	    }
	    int mid=(tree[p].a+tree[p].b)/2;
	    if(r<=mid)change(p*2,l,r,w);
	    else if(l>mid)change(p*2+1,l,r,w);
	    else {change(p*2,l,mid,w);change(p*2+1,mid+1,r,w);}
	}
	tree[p].color=tree[p*2].color|tree[p*2+1].color;
}

int query(int p,int l,int r)
{
	if((tree[p].a==l&&tree[p].b==r)||tree[p].lazy==1)return tree[p].color;
	else
	{
		int mid=(tree[p].a+tree[p].b)/2;
		if(r<=mid)query(p*2,l,r);
		else if(l>mid)query(p*2+1,l,r);
		else return query(p*2,l,mid)|query(p*2+1,mid+1,r);
	}
}
void swap(int x,int y)
{
	int t=x;
	x=y;
	y=t;
}
int main()
{
	int i,L,m,t,ans;
	char ch;
	while(scanf("%d%d%d",&L,&t,&m)!=EOF)
	{
		build(1,1,L);
		for(i=1;i<=m;i++)
		{
			scanf(" %c",&ch);
			if(ch=='C')
			{
			    int a,b,c;
				scanf("%d%d%d",&a,&b,&c);
				c=code(c);
				if(a>b)swap(a,b);
				change(1,a,b,c); 
			}
			else 
			{
				int st,en;
				scanf("%d%d",&st,&en);
				if(st>en)swap(st,en);
				ans=uncode(query(1,st,en));
				printf("%d\n",ans);
			}
		}
	}
	return 0;
}



 

本文可以转载,但必须附上原文链接,否则你会终生找不到妹子!!!欢迎关注我的CSDN: ahyjjr