HDU - 1540——Tunnel Warfare (线段树区间合并)

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 <bits/stdc++.h>

using namespace std;

const int MAXN = 50005;

struct D{
	int l,r,maxn,len;
}Tree[4*MAXN];
int S[MAXN];
int top;

void Up(int temp){
	Tree[temp].l = Tree[temp<<1].l;
	if(Tree[temp<<1].l == Tree[temp<<1].len)Tree[temp].l += Tree[temp<<1|1].l;
	Tree[temp].r = Tree[temp<<1|1].r;
	if(Tree[temp<<1|1].r == Tree[temp<<1|1].len)Tree[temp].r += Tree[temp<<1].r;
	Tree[temp].maxn = max(Tree[temp<<1].maxn,Tree[temp<<1|1].maxn);
	Tree[temp].maxn = max(Tree[temp].maxn,Tree[temp<<1].r+Tree[temp<<1|1].l);
}

void Build(int temp,int left,int right){
	Tree[temp].len = right-left+1;
	Tree[temp].l = Tree[temp].r = Tree[temp].maxn = Tree[temp].len;
	if(left == right)return;
	int mid = left + (right-left)/2;
	Build(temp<<1,left,mid);
	Build(temp<<1|1,mid+1,right);
}

void Updata(int temp,int left,int right,int tempt,int value){
	if(left == right){
		Tree[temp].l = Tree[temp].r = Tree[temp].maxn = value;
		return;
	}
	int mid = left + (right-left)/2;
	if(tempt<=mid)Updata(temp<<1,left,mid,tempt,value);
	else Updata(temp<<1|1,mid+1,right,tempt,value);
	
	Up(temp);
}

int Query(int temp,int left,int right,int tempt){
	if(Tree[temp].maxn == Tree[temp].len || Tree[temp].maxn == 0 || left == right)return Tree[temp].maxn;
	int mid = left + (right-left)/2;
	if(tempt<=mid){
		if(mid-Tree[temp<<1].r+1<=tempt)return Query(temp<<1,left,mid,tempt)+Query(temp<<1|1,mid+1,right,mid+1);
		else return Query(temp<<1,left,mid,tempt);
	}else {
		if(mid+Tree[temp<<1|1].l>=tempt)return Query(temp<<1,left,mid,mid)+Query(temp<<1|1,mid+1,right,tempt);
		else return Query(temp<<1|1,mid+1,right,tempt);
	}
}

int main(){
	
	int N,M;
	while(scanf("%d %d",&N,&M)!=EOF){
		Build(1,1,N);
		char ch;
		int A;
		top = 0;
		while(M--){
			getchar();
			scanf("%c",&ch);
			switch(ch){
				case 'D':
					scanf("%d",&A);
					Updata(1,1,N,A,0);
					S[top++] = A;
					break;
				case 'R':
					Updata(1,1,N,S[--top],1);
					break;
				case 'Q':
					scanf("%d",&A);
					printf("%d\n",Query(1,1,N,A));
			}
		}	
	}

	
	return 0;
}



posted @ 2018-05-17 11:05  Assassin_poi君  阅读(138)  评论(0编辑  收藏  举报