HDU-1540 Tunnel Warfare (线段树 维护端点信息)

Tunnel Warfare

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 10405    Accepted Submission(s): 4086


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 <bits/stdc++.h>
using namespace std;
int h[50001];
struct tree{
	int l, r, mx;
}c[50000 << 2];
void pushup(int o, int l, int r){
	c[o].mx = max(c[o << 1].mx, c[o << 1 | 1].mx);
	c[o].mx = max(c[o].mx, c[o << 1].r + c[o << 1 | 1].l);
	int mid = l + r >> 1;
	if(c[o << 1].l == mid - l + 1){
		c[o].l = c[o << 1].mx + c[o << 1 | 1].l;
	}
	else{
		c[o].l = c[o << 1].l;
	}
	if(c[o << 1 | 1].r == r - mid){
		c[o].r = c[o << 1 | 1].mx + c[o << 1].r;
	}
	else{
		c[o].r = c[o << 1 | 1].r;
	}
}
void build(int o, int l, int r){
	c[o].l = c[o].r = c[o].mx = r - l + 1;
	if(l == r) return;
	int mid = l + r >> 1;
	build(o << 1, l, mid);
	build(o << 1 | 1, mid + 1, r);
}
void add(int o, int l, int r, int id, int v){
	if(l == r){
		c[o].r = c[o].l = c[o].mx = v;
		return;
	}
	int mid = l + r >> 1;
	if(id <= mid) add(o << 1, l, mid, id, v);
	else add(o << 1 | 1, mid + 1, r, id, v);
	pushup(o, l, r);
}
int query(int o, int l, int r, int id){
	if(l == r) return c[o].mx;
	if(c[o].mx == 0) return 0;
	if(c[o].l == r - l + 1) return c[o].l;
	int mid = l + r >> 1;
	if(id <= mid){
		if(id >= mid - c[o << 1].r + 1){
			return query(o << 1, l, mid, id) + query(o << 1 | 1, mid + 1, r, mid + 1);
		}
		else{
			return query(o << 1, l, mid, id);
		}
	}
	else{
		if(id <= mid + c[o << 1 | 1].l){
			return query(o << 1 | 1, mid + 1, r, id) + query(o << 1, l, mid, mid);
		}
		else{
			return query(o << 1 | 1, mid + 1, r, id);
		}
	}
}
int main(){
	int n, m;
	while(scanf("%d %d", &n, &m) != EOF){
		int pos = 0, x;
		build(1, 1, n);
		char s[10];
		for(int i = 1; i <= m; ++i){
			scanf("%s", s);
			if(s[0] == 'D'){
				scanf("%d", &x);
				h[++pos] = x;
				add(1, 1, n, x, 0);
			}
			if(s[0] == 'R'){
				add(1, 1, n, h[pos--], 1);
			}
			if(s[0] == 'Q'){
				scanf("%d", &x);
				printf("%d\n", query(1, 1, n, x));
			}
		}
	}
}

/*
题意:
5e4的点,每个点要么为1要么为0,5e4次操作,每次某点要么0变1,要么1变0,要么查询某点所在
的最长连续1的长度。

思路:
线段树维护端点在区间内的最长连续1的长度,查询时,即当某点所在线段树的区间是满的时候,注意扩散一下。
*/


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值