poj2777(延迟更新+线段树)

Count Color
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 31475 Accepted: 9433

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

 
     本题要求给定区间内不同颜色的种类数。由于需要频繁的更新和查询,需要用线段树来降低时间复杂度。又由于是在区间上做更新操作,若每次都更新到叶节点,肯定会超时的。我们需要做线段树的成段延迟更新。
      对于本题,染的对象可以看做是点,初始时所有点为颜色1,若不做成段延迟更新我们需要使某个点颜色为1,这样会比较麻烦;我们可以标记根节点颜色为1(所有叶节点颜色为1,纯色)。
      当做更新操作时,若需更新的区间为[l,r],我们只需把区间更新到若干个连续的和区间为[l,r]的中间节点区间,保证每个中间点区间为纯色。 具体做法为当更新操作到某个节点时, 若需更新区间正好等于该节点覆盖区间,只需修改该节点颜色然后退出;若经过某个节点覆盖区间大于需更新区间,将该节点状态下压到左右子节点然后修改标记该区间非纯色,切割需更新区间到左右子节点区间继续更新操作。
      当做查询操作时,若需查询区间为[l,r],若查询到某节点的颜色为纯色,表示该区间只有一种颜色,直接退出;若不为纯色,切割需查询区间到左右子节点区间继续查询操作。
      具体见代码:
 
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;

const int MAXN=100000+10;
int color[50];
struct node 
{
	int left,right,kind;
}tree[MAXN*4];


void build(int id,int left,int right)
{
	tree[id].left=left;
	tree[id].right=right;
	tree[id].kind=1;
	if(tree[id].left==tree[id].right)
		return ;
	int mid=(tree[id].left+tree[id].right)>>1;
	build(id<<1,left,mid);
	build((id<<1)|1,mid+1,right);
}

void updata(int id,int left,int right,int color)
{
	if(tree[id].left==left&&tree[id].right==right||color==tree[id].kind)//color==tree[id].kind用来优化
	{
		tree[id].kind=color;
		return ;
	}
	if(tree[id].kind!=0)
	{
		tree[id<<1].kind=tree[(id<<1)|1].kind=tree[id].kind;
		tree[id].kind=0;//表示该区间颜色混合
	}
	int mid=(tree[id].left+tree[id].right)>>1;
	if(right<=mid)updata(id<<1,left,right,color);
	else if(left>mid)updata((id<<1)|1,left,right,color);
	else
	{
		updata(id<<1,left,mid,color);
		updata((id<<1)|1,mid+1,right,color);
	}
}


void query(int id,int left,int right)
{
	if(tree[id].kind>0)//纯色
	{
		color[tree[id].kind]++;
		return ;
	}
	int mid=(tree[id].left+tree[id].right)>>1;
	if(right<=mid)query(id<<1,left,right);
	else if(left>mid)query((id<<1)|1,left,right);
	else
	{
		query(id<<1,left,mid);
		query((id<<1)|1,mid+1,right);
	}
}

int main()
{
	int l,t,o,i,left,right,col,tmp,sum;
	char cmd[5];
	while(~scanf("%d%d%d",&l,&t,&o))
	{
		build(1,1,l);
		while(o--)
		{
			scanf("%s",cmd);
			if(cmd[0]=='P')
			{
				sum=0;
				scanf("%d%d",&left,&right);
				memset(color,0,sizeof(color));
				query(1,left,right);
				for(i=1;i<=t;i++)
				{
					if(color[i])
						sum++;
				}
				printf("%d\n",sum);
			}
			else 
			{
				scanf("%d%d%d",&left,&right,&col);
				updata(1,left,right,col);
			}
		}
	}
	return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值