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

题意:

输入n,m:n表示有n个城市相连,m表示下面有m行指令。D X表示破坏X这个城市,R表示修复最近一次破坏的城市,Q X表示

查询包含X的最大连续区间的长度。

代码:

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

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值