天天看点

HDU5023:线段树应用于染色问题

早听说过线段树,但一直没有学习过,昨天终于学习了一下,并且刷了一道线段树的题,记录一下。

线段树就是把一个区间分两半,左子树存左半段,右子树存右半段,然后重复这个步骤,直到叶子节点(即区间长度为1的节点),就像下面这个图

HDU5023:线段树应用于染色问题

然后每个节点上可以记录当前节点存储区间的一些信息,比如和,最值,颜色,容易知道这些信息都可以从左右子树获得。

下面上题上代码:

Problem Description

Corrupt governors always find ways to get dirty money. Paint something, then sell the worthless painting at a high price to someone who wants to bribe him/her on an auction, this seemed a safe way for mayor X to make money.

Because a lot of people praised mayor X's painting(of course, X was a mayor), mayor X believed more and more that he was a very talented painter. Soon mayor X was not satisfied with only making money. He wanted to be a famous painter. So he joined the local painting associates. Other painters had to elect him as the chairman of the associates. Then his painting sold at better price.

The local middle school from which mayor X graduated, wanted to beat mayor X's horse fart(In Chinese English, beating one's horse fart means flattering one hard). They built a wall, and invited mayor X to paint on it. Mayor X was very happy. But he really had no idea about what to paint because he could only paint very abstract paintings which nobody really understand. Mayor X's secretary suggested that he could make this thing not only a painting, but also a performance art work.

This was the secretary's idea:

The wall was divided into N segments and the width of each segment was one cun(cun is a Chinese length unit). All segments were numbered from 1 to N, from left to right. There were 30 kinds of colors mayor X could use to paint the wall. They named those colors as color 1, color 2 .... color 30. The wall's original color was color 2. Every time mayor X would paint some consecutive segments with a certain kind of color, and he did this for many times. Trying to make his performance art fancy, mayor X declared that at any moment, if someone asked how many kind of colors were there on any consecutive segments, he could give the number immediately without counting.

But mayor X didn't know how to give the right answer. Your friend, Mr. W was an secret officer of anti-corruption bureau, he helped mayor X on this problem and gained his trust. Do you know how Mr. Q did this?

Input

There are several test cases.

For each test case:

The first line contains two integers, N and M ,meaning that the wall is divided into N segments and there are M operations(0 < N <= 1,000,000; 0<M<=100,000)

Then M lines follow, each representing an operation. There are two kinds of operations, as described below:

1) P a b c

a, b and c are integers. This operation means that mayor X painted all segments from segment a to segment b with color c ( 0 < a<=b <= N, 0 < c <= 30).

2) Q a b

a and b are integers. This is a query operation. It means that someone asked that how many kinds of colors were there from segment a to segment b ( 0 < a<=b <= N).

Please note that the operations are given in time sequence.

The input ends with M = 0 and N = 0.

Output

For each query operation, print all kinds of color on the queried segments. For color 1, print 1, for color 2, print 2 ... etc. And this color sequence must be in ascending order.

#include <stdio.h>
#include <stdlib.h>

typedef struct Node {
	int l;
	int r;
	int color;
	int lazy;
	Node(int l, int r, int c) {
		this->l = l;
		this->r = r;
		this->color = c;
		this->lazy = 0;
	};
}node_t; 

static node_t *tree;

void build(int id, int l, int r) {
	tree[id] = node_t(l, r, 1 << 2);//初始颜色为2
	if (l == r)
		return;
	int m = (l + r) >> 1;
	build(id << 1, l, m);
	build(id << 1 | 1, m + 1, r);
}

void pushDown(int id) {
	if (tree[id].lazy > 0) {
		tree[id << 1].lazy = tree[id].lazy;
		tree[id << 1 | 1].lazy = tree[id].lazy;
		tree[id << 1].color = 1 << tree[id].lazy;
		tree[id << 1 | 1].color = 1 << tree[id].lazy;
		tree[id].lazy = 0;
	}
}

void pushUp(int id) {
	tree[id].color = tree[id << 1].color | tree[id << 1 | 1].color;
}

void change(int l, int r, int c, int id) {
	if (tree[id].l == l && tree[id].r == r) {
		tree[id].color = 1 << c;
		tree[id].lazy = c;
		return;
	}
	pushDown(id);
	if (r <= tree[id << 1].r) {//目标线段在左子树
		change(l, r, c, id << 1);
	}
	else if (l >= tree[id << 1 | 1].l) {//目标线段在右子树
		change(l, r, c, id << 1 | 1);
	}
	else {//目标线段跨左右子树
		change(l, tree[id << 1].r, c, id << 1);
		change(tree[id << 1 | 1].l, r, c, id << 1 | 1);
	}
	pushUp(id);
}

int query(int l, int r, int id) {
	if (tree[id].l == l && tree[id].r == r)
		return tree[id].color;
	pushDown(id);
	if (r <= tree[id << 1].r) {//目标线段在左子树
		return query(l, r, id << 1);
	}
	else if (l >= tree[id << 1 | 1].l) {//目标线段在右子树
		return query(l, r, id << 1 | 1);
	}
	else {//目标线段跨左右子树
		return query(l, tree[id << 1].r, id << 1) | query(tree[id << 1 | 1].l, r, id << 1 | 1);
	}
}

void printRes(int res) {
	char flag = 0;
	for (int i = 1; i <= 30; i++) {
		if ((res&(1 << i)) > 0) {
			if (flag == 0) {
				printf("%d", i);
				flag = 1;
			}else 
				printf(" %d", i);
		}	
	}
	printf("\n");
}

int main()
{
	int N, M;
	while (1) {
		scanf("%d%d", &N, &M);//墙分N段,共有M个操作
		if (N == 0)
			break;
		tree = (node_t*)malloc(sizeof(node_t) * 4 * N);
		build(1, 1, N);//建树
		char op;
		int a, b, c;
		for (int i = 0; i < M; i++) {
			scanf("%c", &op);//回车符
			scanf("%c", &op);
			if (op=='P')//P a b c表示把a到b段涂成颜色c
			{
				scanf("%d%d%d", &a, &b, &c);
				change(a, b, c, 1);
			}
			else {//Q a b查询a到b段有哪些颜色
				scanf("%d%d", &a, &b);
				int res = query(a, b, 1);
				printRes(res);
			}
		}
		free(tree);
	}
	return 0;
}
           

继续阅读