天天看點

HDU1540 Tunnel Warfare (線段樹區間合并) Tunnel Warfare

Tunnel Warfare

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 9923    Accepted Submission(s): 3884

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
        

Source POJ Monthly

題意:有n個村莊,每個村莊都用道地連接配接着,分為三種操作,R表示修複最後一個被破壞的村莊,D x表示破話村莊x, Q x表示求包括村莊x的最長連續區間。

思路:線段樹的區間合并題目,對于求一個連續最長區間,分三種情況,①是左子樹最長連續區間,②是右子樹最長連續區間,③是左子樹最長右連續區間+右子樹最長左連續區間的值,這三種情況取最大值即可。具體可看代碼注釋,很詳細。

AC代碼如下:

#include <cstdio>
#include <stack>
#include <cstring>
#include <algorithm>
#define LL long long
using namespace std;

const int maxn = 5e4 + 10;
int n,m,s[maxn],top;
char op[3];
struct node{
	int l,r;    //以rt為根結點的左右區間l表示左端點,r表示右端點 
	int ls,rs,ms;//ls:左起最大連續區間,rs:右起最大連續區間 ms:最大連續區間 
}p[maxn << 2];

void build(int l, int r, int rt){//建樹 & 初始化 
	p[rt].l = l; p[rt].r = r;  //以rt為根結點的左右區間[l,r] 
	p[rt].ls = p[rt].ms = p[rt].rs = r - l + 1;   //初始該區間的所有值均為1,故為區間大小 
	if(l != r){
		int m = (l + r) >> 1;
		build(l,m,rt << 1);
		build(m+1,r,rt << 1 | 1);
	}
}

void update(int rt, int x, int flag){//更新x節點的狀态 
	if(p[rt].l == p[rt].r){
		if(flag) p[rt].ls = p[rt].ms = p[rt].rs = 1;  //修複 
		else p[rt].ls = p[rt].ms = p[rt].rs = 0;    //破壞 
		return ;
	}
	
	int m = (p[rt].l + p[rt].r) >> 1;
	if(x <= m) update(rt << 1,x,flag);  //要破壞或者修複的點在左子樹 
	else update(rt << 1 | 1,x,flag);    //要破壞或者修複的點在右子樹 
	
	p[rt].ls = p[rt << 1].ls;     //父節點的左區間最大連續長度等與左子樹的, 
	p[rt].rs = p[rt << 1 | 1].rs; //着重分析最後兩個if語句 
	p[rt].ms = max(max(p[rt << 1].ms,p[rt << 1 | 1].ms),p[rt << 1].rs + p[rt << 1 | 1].ls);
	//區間的最大連續區間分三種情況:左子樹最大連續區間,右子樹最大連續區間,左子樹右連續+右子樹左連續 
	if(p[rt << 1].ls == p[rt << 1].r - p[rt << 1].l + 1) p[rt].ls += p[rt << 1 | 1].ls;
	//左子樹的左連續區間長度等于 左子樹區間大小,則其父節點的長度應為加上右子樹左連續長度 
	if(p[rt << 1 | 1].rs == p[rt << 1 | 1].r - p[rt << 1 | 1].l + 1) p[rt].rs += p[rt << 1].rs;
	//右子樹右連續長度等于右子樹區間大小,則其父節點的長度為再加上左子樹的右連續的長度 
}

int query(int rt, int x){//查詢含有節點x的最長連續區間
	//找到x節點或者最長連續區間為0或者區間值全為1 
	if(p[rt].l == p[rt].r || p[rt].ms == 0 || p[rt].ms == p[rt].r - p[rt].l + 1)
		return p[rt].ms;
	int m = (p[rt].l + p[rt].r) >> 1;
	if(x <= m){ //目前結點在左子樹 
		//目前節點大于等于左子樹右連續區間的左端點值 ,則要再計算上右子樹的左連續區間
		if(x >= p[rt << 1].r - p[rt << 1].rs + 1)
			return query(rt << 1,x) + query(rt << 1 | 1,m+1);
		else
			return query(rt << 1,x);
	}
	else{//目前節點小于等于右子樹左連續區間的右端點值,則要計算上左子樹的右連續區間 
		if(x <= p[rt << 1 | 1].l + p[rt << 1 | 1].ls - 1)
			return query(rt << 1 | 1,x) + query(rt << 1, m);
		else
			return query(rt << 1 | 1,x);  
	}
}

int main(){
	while(~scanf("%d%d",&n,&m)){
		build(1,n,1);
		int num;
		top = 0;
		while(m --){
			scanf("%s",op);
			if(op[0] == 'R'){
				if(num > 0){
					num = s[-- top];
					update(1,num,1);
				}
			}
			else{
				scanf("%d",&num);
				if(op[0] == 'D'){
					s[top ++] = num;
					update(1,num,0);
				}
				else{
					printf("%d\n",query(1,num));
				}
			}
		}
	}
	return 0;
}