线段树 poj 2777 Count Color

poj 2777
https://vjudge.net/problem/POJ-2777

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

代码如下

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>

using namespace std;


/*
L T O  L线段  T 颜色种类  O操作个数
L (1 <= L <= 100000), T (1 <= T <= 30) , O (1 <= O <= 100000)

2 2 4
C 1 1 2
P 1 2
C 2 2 2
P 1 2
Sample Output
2
1
*/
const int N = 100010;
struct SEGMENT_TREE {
	int l; int r;
	int color;
}tr[N*4];

int n, m, o;

int cArr[50];

void build(int p, int l, int r) {
	tr[p].l = l; tr[p].r = r;
	if (l == r) {
		tr[p].color = 0;
		tr[p].l = l; tr[p].r = r;
		return;
	}

	int mid = (l + r) >> 1;
	if (l <= mid) build(p*2,l,mid);
	if (r > mid) build(p * 2 + 1, mid + 1, r);
	return;
}

void query(int p, int l, int r) {
	if (tr[p].color != 0) {
		cArr[tr[p].color] = 1;
		return;
	}

	int mid = (tr[p].l + tr[p].r)  >> 1;
	if (l <= mid) query(p * 2, l, r);
	if (r > mid)  query(p*2+1,l,r);
}

void pushDown(int p) {
	if (tr[p].color != 0) {
		tr[p * 2].color = tr[p].color; tr[p * 2+1].color = tr[p].color;
		tr[p].color = 0;
	}
}

void freshUp(int p) {
	if (tr[p * 2].color == tr[p * 2 + 1].color) {
		tr[p].color = tr[p * 2].color;
	}
}


void update(int p, int l, int r, int c) {
	if (l <= tr[p].l && r >= tr[p].r) {
		//刷颜色
		tr[p].color = c;
		return;
	}
	pushDown(p);
	int mid = (tr[p].l + tr[p].r) >> 1;
	if (l <= mid) update(p * 2, l, r, c);
	if (r > mid) update(p * 2 + 1, l, r, c);

	freshUp(p);
}

int main()
{
	cin >> n >> m >> o;
	build(1,1,n);
	//初始所有线段均涂抹颜色1
	tr[1].color = 1;

	while (o--) {
		char op[2]; int l, r, c;
		scanf("%s %d %d", op, &l, &r);
		if (op[0] == 'C') {
			scanf("%d", &c);
			update(1,l,r,c);
		}
		else if (op[0] == 'P') {
			memset(cArr, 0, sizeof cArr);
			query(1, l, r);
			int count = 0;
			for (int i = 1; i <= m; i++) {
				if (cArr[i] != 0) count++;
			}
			printf("%d
", count);
		}
	}

	return 0;
}

我的视频题解空间

作 者: itdef
欢迎转帖 请保持文本完整并注明出处
技术博客 http://www.cnblogs.com/itdef/
B站算法视频题解
https://space.bilibili.com/18508846
qq 151435887
gitee https://gitee.com/def/
欢迎c c++ 算法爱好者 windows驱动爱好者 服务器程序员沟通交流
如果觉得不错,欢迎点赞,你的鼓励就是我的动力
阿里打赏 微信打赏
原文地址:https://www.cnblogs.com/itdef/p/15380458.html