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 9D 3D 6D 5Q 4Q 5RQ 4RQ 4
 

Sample Output
 
 
1024

具体题解点这里

#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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值