POJ 2777 Count Color

题目

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.

输入

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.

输出

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

思路

因为只有至多30种颜色,所以我们可以用二进制来表示每一种颜色的存在与否。再利用线段树来维护各个区间内的颜色数量。

代码

#include<iostream>
#include<stdio.h>
#include<string.h>

using namespace std;
const int MAXN = 100000;

int L, T, O;
char op[10];
int a, b, c;
int Color[35];
int board[MAXN<<3];
int lazy[MAXN<<3];

void creat(int l = 1, int r = L, int p = 1)
{
	int mid;
	if(l == r) board[p] = Color[1];
	else
	{
		mid = (l + r)/2;
		creat(l, mid, 2*p);
		creat(mid+1, r, 2*p+1);
		board[p] = board[2*p] | board[2*p+1];
	}
			
}

void pushdown(int p)
{
	if(lazy[p])
	{
		lazy[2*p] = lazy[p];
		lazy[2*p+1] = lazy[p];
		board[2*p] = board[p];
		board[2*p+1] = board[p];
		lazy[p] = 0;
	}
}

void pushup(int p)
{
	board[p] = board[2*p] | board[2*p+1];
}
//目标区间, 当前区间, 节点, 颜色 
void paint(int l, int r, int cl, int cr, int p, int c)
{
	int mid;
	if(cr < l || r < cl) return;
	else if(l <= cl && cr <= r)
	{
		board[p] = Color[c];
		if(cr > cl) lazy[p] = Color[c];	
	 } 
	 else
	 {
	 	pushdown(p);
	 	mid = (cl + cr) / 2;
		paint(l, r, cl, mid, 2*p, c);
		paint(l, r, mid+1, cr, 2*p+1, c);
		pushup(p);
	 }
}

int count(int l, int r, int cl = 1, int cr = L, int p = 1)
{
	int mid;
	if(cr < l || cl > r) return 0;
	else if(cl >= l && cr <= r) return board[p];
	else
	{
		mid = (cl + cr) /2;
		pushdown(p);
		return count(l, r, cl, mid, 2*p) | count(l, r, mid+1, cr, 2*p+1);
	}
	
			
}

int main()
{
	scanf("%d %d %d", &L, &T, &O);
	
	Color[1] = 1;
	for(int i = 2; i <= T; i++) Color[i] = Color[i-1] << 1;
	
	creat();
	memset(lazy, 0, sizeof(lazy));
			
	int temp;
	while(O--)
	{
		//printf("O=%d\n", O);
		scanf("%s", op);
		if(op[0] == 'C')
		{
			scanf("%d %d %d", &a, &b, &c);
			if(a > b) temp = a, a = b, b = temp;
			paint(a, b, 1, L, 1, c);
		}
		else if(op[0] == 'P')
		{
			scanf("%d %d", &a, &b);
			if(a > b) temp = a, a = b, b = temp;
			int subboard = count(a, b);
			int ans = 0;
			while(subboard != 0)
			{
				if(subboard&1) ans++;
				subboard >>= 1;
			}
			printf("%d\n", ans);
		}
		
	}
	
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值