Tunnel Warfare HDU - 1540 (线段树区间最值***)

6 篇文章 0 订阅
5 篇文章 0 订阅

 

Tunnel Warfare

 HDU - 1540 

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

一开始错到爆炸的代码(ac了):

#include <cstdio>
#include <cstring>
#include <iostream>
#include <cmath>
#include <algorithm>

using namespace std;
const int INF = 1e9 + 7;
const int N = 1e6 + 7;
struct Tree {
	int l, r, ma,mi;
};

struct Tree tree[N];
int n, stk[N];

void build (int l, int r, int rt) {
	
	tree[rt].l = l, tree[rt].r = r;
	if (l == r) {
		//一开始赋值写反了debug到怀疑人生 
		tree[rt].ma = 0;   
		tree[rt].mi = n+1;
		return;
	}
	int mid = (l + r) >> 1;
	build (l, mid, rt << 1);
	build (mid + 1, r, rt << 1 | 1);
	tree[rt].ma = max (tree[rt<<1].ma, tree[rt<<1|1].ma);
	tree[rt].mi = min (tree[rt<<1].mi, tree[rt<<1|1].mi);
	
}

void update (int l, int r, int rt, int op) {
	
	if(tree[rt].l == l && l == tree[rt].r) {
		if (op == 1) tree[rt].ma = r;
		else tree[rt].mi = r;
		return;
	} 
	int mid = (tree[rt].l + tree[rt].r) >> 1;
	if (l <= mid) update (l, r, rt << 1, op);
	else update (l, r, rt << 1 | 1, op);
	if (op == 1) {
		tree[rt].ma = max (tree[rt<<1].ma, tree[rt<<1|1].ma);
	}
	else {
		tree[rt].mi = min (tree[rt<<1].mi, tree[rt<<1|1].mi);
	}
	
	
}


int query (int l, int r, int rt,int op) {
	if (l <= tree[rt].l && tree[rt].r <= r) {
		//puts("debug!");
		if (op == 1)
		return tree[rt].ma;
		else
		return tree[rt].mi;
	}
	int mid = (tree[rt].l + tree[rt].r) >> 1;
	int ans = (op == 1) ? 0 : INF;
	if (l <= mid) ans = query (l, r, rt << 1, op);
	if (r > mid) {
		if (op == 1)
		ans = max(ans, query (l, r, rt << 1 | 1, op));
		else
		ans = min(ans, query (l, r, rt << 1 | 1, op));
	}
	//printf ("op = %d ans = %d\n",op, ans);
	return ans;
	
}


int main (void) {
	
	int m;
	while (~scanf ("%d %d", &n, &m)){
		build(1, n, 1);
		char op;
		int x, t=0;
		while (m--) {
			scanf (" %c", &op);
			if (op == 'D') {
				scanf ("%d", &x);
				
				update (x, x, 1, 1);
				update (x, x, 1, 2);
				//puts("debug!");
				stk[++t] = x;
			} else if (op == 'Q') {
				scanf ("%d", &x);
				
				int mi = query (x, n, 1, 2);
				int ma = query (1, x, 1, 1);
				if (mi == ma) {
					puts("0");
					continue;
				}
				//printf (">> %d %d ", mi, ma);
				printf ("%d\n",mi - ma -1);
				//puts("debug!");
			}  else {
				if (t == 0) continue;
				//恢复 
				
				update (stk[t], 0, 1, 1); // 左边最大值暂时设为0 //沿途如果遇到有比0大的值则再更新 
				update (stk[t], n+1, 1, 2); //右边最小值暂时设为n+1 
				--t;
			}
		}
	}
	return 0;
} 

下面的好理解一点: 

#include <bits/stdc++.h>
using namespace std;

const int N = 1e6 + 7;
struct Tree {
	int left; // 左边被摧毁建筑的最大编号,初始值为0 
	int right; // 右边被摧毁建筑的最小的编号,初始值为n+1 
};
Tree tree[N];
int n;

void pushup (int rt) {
	tree[rt].left = max(tree[rt*2].left, tree[rt*2+1].left);
	tree[rt].right = min(tree[rt*2].right, tree[rt*2+1].right);
}

void build (int l, int r, int rt) {
	
	if (l == r) {
		tree[rt].left = 0, tree[rt].right = n + 1;
		return;
	}
	int mid = (l + r) >> 1;
	build (l, mid, rt<<1);
	build (mid + 1, r, rt<<1|1);
	pushup (rt);
	
}

void update_l (int v, int w, int l, int r, int rt) {
	// v:要更新的建筑编号,w: 更新为多少 
	if (l == v && v == r) {
		tree[rt].left = w;
		return;
	}
	int mid = (l + r) >> 1;
	if (v <= mid) update_l (v, w, l, mid, rt<<1);
	else update_l (v, w, mid+1, r, rt<<1|1);
	tree[rt].left = max (tree[rt<<1].left, tree[rt<<1|1].left);
	
}

void update_r (int v, int w, int l, int r, int rt) {
	
	if (l == v && v == r) {
		tree[rt].right = w;
		return;
	}
	int mid = (l + r) >> 1;
	if (v <= mid) update_r (v, w, l, mid, rt<<1);
	else update_r (v, w, mid + 1, r, rt<<1|1);
	tree[rt].right = min (tree[rt<<1].right, tree[rt<<1|1].right);
	
}

int query_l (int L, int R, int l, int r, int rt) {
	
	if (L <= l && r <= R) {
		return tree[rt].left;
	}
	int mid = (l + r) >> 1;
	int ans = 0;
	if (L <= mid) ans = query_l (L, R, l, mid, rt<<1);
	if (R > mid) ans = max (ans, query_l (L, R, mid+1, r, rt<<1|1));
	return ans;
	
}

int query_r (int L, int R, int l, int r, int rt) {
	
	if (L <= l && r <= R) {
		return tree[rt].right;
	}
	int mid = (l + r) >> 1;
	int ans = n+1;
	if (L <= mid) ans = query_r (L, R, l, mid, rt<<1);
	if (R > mid) ans = min (ans, query_r (L, R, mid+1, r, rt<<1|1));
	return ans;
}
int stk[N]; //用于恢复的栈 

int main () {
	
	int q, x, k;
	char op;
	while (~scanf ("%d %d", &n, &q)) {
		
		k = 0;
		build (1, n, 1);
		while (q--) {
			
			scanf (" %c", &op);
			if (op == 'D') {
				scanf ("%d", &x);
				stk[++k] = x;
				update_l (x, x, 1, n, 1);
				update_r (x, x, 1, n, 1);
			} else if (op == 'Q') {
				scanf ("%d", &x);
				int mi = query_r (x, n, 1, n, 1);
				int ma = query_l (1, x, 1, n, 1);
				
				if (mi == ma) {
					puts("0");
					continue;
				}
				//printf (">> %d %d  ", mi, ma);
				printf ("%d\n", mi-ma-1);
			} else {
				//恢复 
				if (k == 0) continue;
				//printf ("<<R: %d\n", stk[k]); 
				update_l (stk[k], 0, 1, n, 1); // 左边最大值暂时设为0 //沿途如果遇到有比0大的值则再更新
				update_r (stk[k], n+1, 1, n, 1); //右边最小值暂时设为n+1 
				--k;
			}
		}
		
	}
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值