HDu 1540 Tunnel Warfareセグメントツリー区間マージ

4160 ワード

Tunnel Warfare
Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 6565    Accepted Submission(s): 2527
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

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

#define lson l, m, rt << 1
#define rson m + 1, r, rt << 1 | 1

using namespace std;

const int maxn = 50000 + 10;

int msum[maxn << 2], lsum[maxn << 2], rsum[maxn << 2];

void PushUp(int rt, int m) {
	lsum[rt] = lsum[rt << 1];
	rsum[rt] = rsum[rt << 1 | 1];

	//         ,         
	if (lsum[rt << 1] == m - (m >> 1)) {
		lsum[rt] += lsum[rt << 1 | 1];
	}

	//  
	if (rsum[rt << 1 | 1] == (m >> 1)) {
		rsum[rt] += rsum[rt << 1];
	}

	//                           
	msum[rt] = max(rsum[rt << 1] + lsum[rt << 1 | 1], max(msum[rt << 1], msum[rt << 1 | 1]));
}

void build(int l, int r, int rt) {
	msum[rt] = lsum[rt] = rsum[rt] = r - l + 1;
	if (l == r) return;
	int m = (l + r) >> 1;
	build(lson);
	build(rson);
}

//c 1        ,c 0      
void update(int x, int c, int l, int r, int rt) {
	if (l == r) {
		msum[rt] = lsum[rt] = rsum[rt] = c;
		return;
	}
	int m = (l + r) >> 1;
	if (x <= m) {
		update(x, c, lson);
	}
	else {
		update(x, c, rson);
	}
	PushUp(rt, r - l + 1);
}

int query(int x, int l, int r, int rt) {
	//               0,         
	if (l == r || msum[rt] == 0 || msum[rt] == r - l + 1) {
		return msum[rt];
	}
	int m = (l + r) >> 1;
	if (x <= m) {
		//                 ,        
		if (x >= m - rsum[rt << 1] + 1) {
			return query(x, lson) + query(m + 1, rson); //   m + 1,       
		}
		else {
			//           
			return query(x, lson);
		}
	}
	else {
		//  
		if (x <= (m + 1) + lsum[rt << 1 | 1] - 1) {
			return query(x, rson) + query(m, lson);  //   m,       
		}
		else {
			return query(x, rson);
		}
	}
}

int main()
{
	int n, m, x;
	char op[2];
	while (~scanf("%d%d", &n, &m)) {
		build(1, n, 1);
		stack<int> s;
		while (m--) {
			scanf("%s", op);
			if (op[0] == 'D') {
				scanf("%d", &x);
				s.push(x);
				update(x, 0, 1, n, 1);
			}
			else if (op[0] == 'Q') {
				scanf("%d", &x);
				printf("%d
", query(x, 1, n, 1)); } else { if (!s.empty()) { x = s.top(); s.pop(); update(x, 1, 1, n, 1); } } } } return 0; }