Tunnel Warfare HDU - 1540 (線段樹區間最值***)

 

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

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章