Tunnel Warfare(线段树+区间最值)

Tunnel Warfare

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 11572    Accepted Submission(s): 4544


Problem Description
During the War of Resistance Against Japan, tunnel warfare was carried out extensively in the vast areas of north China Plain. Generally speaking, villages connected by tunnels lay in a line. Except the two at the ends, every village was directly connected with two neighboring ones.

Frequently the invaders launched attack on some of the villages and destroyed the parts of tunnels in them. The Eighth Route Army commanders requested the latest connection state of the tunnels and villages. If some villages are severely isolated, restoration of connection must be done immediately!
 

Input
The first line of the input contains two positive integers n and m (n, m ≤ 50,000) indicating the number of villages and events. Each of the next m lines describes an event.

There are three different events described in different format shown below:

D x: The x-th village was destroyed.

Q x: The Army commands requested the number of villages that x-th village was directly or indirectly connected with including itself.

R: The village destroyed last was rebuilt.
 

Output
Output the answer to each of the Army commanders’ request in order on a separate line.
 

Sample Input
 
  
7 9 D 3 D 6 D 5 Q 4 Q 5 R Q 4 R Q 4
 

Sample Output
 
  
1 0 2 4

题目可以理解为求线段树的区间最大值和最小值。

我们在left域上求区间最大值,在right上求区间最小值

对于没有被摧毁的村子,也就是初始时,left = 0,right = n+1

在样例中初始时

线段树的left域初始时为 0 0 0 0 0 0 0

线段树的right域初始时为 n+1 n+1 n+1 n+1 n+1 n+1 n+1

当2,5被摧毁了

2 3 4 5 6 7

线段树的left域为 0 2 0 0 5 0 0

线段树的right域为 n+1 2 n+1 n+1 5 n+1 n+1

这时如果要求第4个村子
只需求出来   
1->4区间中被毁村子的最大值(2),在left域中 

和                 4->7 区间中被毁村子的最小值(5),在right域中

根据两者求出村子的连续区间,即  2->5   所以其村子连续个数 为 5-2-1 = 2

即right - left -1

特殊情况: 求2的连续区间

只需求出来   1->2区间中被毁村子的最大值(2),在left域中 

和                 2->7 区间中被毁村子的最小值(2),在right域中

写程序的时候判断一下,当 left和right相等时,直接返回0

代码中也有较为详细的解释

#include <iostream>
#include <stack>
#include <string.h>
#define size 50010 << 2
#define max(x,y) x>y?x:y
#define min(x,y) x<y?x:y
using namespace std;
// 申请一个线段树,节点有2个域,left保存一个区间内最大的值,right保存一个区间内最小的值
struct node {
	int left;
	int right;
}tree[size];
int n, m;


// 更新节点值
void PushUp(int p) {
	// left保存一个区间内最大的值,right保存一个区间内最小的值
	tree[p].right = min(tree[p * 2].right, tree[p * 2 + 1].right);
	tree[p].left = max(tree[p * 2].left, tree[p * 2 + 1].left);
}

// 构造函数
void Build(int l, int r, int p) {
	// 因为以后树的节点left保存的是区间最大值,right保存的是区间最小值,所以
	// 初始时,将left初始化为最小值,这里可以置为1;将right初始化为最大值,这里可以置为n+1
	if (l == r) {
		tree[p].left = 0;
		tree[p].right = n + 1;
		return;
	}
	int m = (l + r) / 2;
	Build(l, m, p * 2);
	Build(m + 1, r, p * 2 + 1);
	PushUp(p);
}

// 区域操作
// Update对left数据域进行操作
// “D”操作调用此函数时,会将t置为目标城市;C也置为目标城市
// “R”操作调用此函数时,会将t置为目标城市;C置为0,因为要还原为初始值
void Update_left(int t, int C, int l, int r, int p) {
	if (t == l && t == r) {
		tree[p].left = C;
		return;
	}

	int m = (l + r) / 2;
	if (m >= t)
		Update_left(t, C, l, m, p * 2);
	else
		Update_left(t, C, m + 1, r, p * 2 + 1);
	PushUp(p);
}
// Update对right数据域进行操作
// “D”操作调用此函数时,会将t置为目标城市;C也置为目标城市
// “R”操作调用此函数时,会将t置为目标城市;C置为n+1,因为要还原为初始值
void Update_right(int t, int C, int l, int r, int p) {
	if (t == l && t == r) {
		tree[p].right = C;
		return;
	}

	int m = (l + r) / 2;
	if (m >= t)
		Update_right(t, C, l, m, p * 2);
	else
		Update_right(t, C, m + 1, r, p * 2 + 1);
	PushUp(p);
}

// 查询操作
// 在目标城市的左边查找最大值
int Query_left(int L, int R, int l, int r, int p) {
	if (L <= l && r <= R) {
		return tree[p].left;
	}
	int m = (l + r) / 2;
	int maxx = 0;
	if (L <= m)
		maxx = Query_left(L, R, l, m, p * 2);
	if (R >= m + 1)
		maxx = max(Query_left(L, R, m + 1, r, p * 2 + 1), maxx);
	return maxx;
}
// 在目标城市的右边查找最小值
int Query_right(int L, int R, int l, int r, int p) {
	if (L <= l && r <= R) {
		return tree[p].right;
	}
	int m = (l + r) / 2;
	int minn = n + 1;
	if (L <= m)
		minn = Query_right(L, R, l, m, p * 2);
	if (R >= m + 1)
		minn = min(Query_right(L, R, m + 1, r, p * 2 + 1), minn);
	return minn;
}

int main() {
	//freopen("1.txt", "r", stdin);
	char o;
	int num;
	while (scanf("%d %d", &n, &m) == 2) {
		getchar();
		memset(tree, 0, sizeof(tree));
		Build(1, n, 1);
		stack<int> s;
		while (m--) {
			scanf("%c", &o);
			if (o == 'D') {
				scanf("%d", &num);
				s.push(num);
				Update_left(num, num, 1, n, 1);
				Update_right(num, num, 1, n, 1);
			}
			else if (o == 'Q') {
				scanf("%d", &num);
				// 这里记住左右端点都要包含目标城市
				int max = Query_left(1, num, 1, n, 1);
				int min = Query_right(num, n, 1, n, 1);
				// 如果查询的城市为已摧毁,则直接返回0
				if (max == min)
					printf("0\n");
				else
					printf("%d\n", min - max - 1);
			}
			else {
				num = s.top();
				s.pop();
				Update_left(num, 0, 1, n, 1);
				Update_right(num, n + 1, 1, n, 1);
			}
			getchar();
		}
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/wbb1997/article/details/80560300