HDU 1540 Tunnel Warfare 线段树区间合并

Tunnel Warfare

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


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
 
题意:Q x 查询与x相连的村庄数量
R 修复上一次摧毁的村庄
D x 摧毁x村庄
#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<stack>
using namespace std;
const int maxm = 100005;
int now[maxm];
struct node
{
	int l, r, ls, rs, mx;
}tr[maxm * 2];
void build(int x, int a, int b)
{
	tr[x].l = a, tr[x].r = b;
	tr[x].ls = tr[x].rs = tr[x].mx = b - a + 1;
	if (a != b)
	{
		int mid = (a + b) / 2;
		build(x * 2, a, mid);
		build(x * 2 + 1, mid + 1, b);
	}
}
void update(int x, int k, int flag)
{
	//printf("%d %d\n", x, flag);
	if (tr[x].l == tr[x].r)
	{
		if (flag == 1) tr[x].ls = tr[x].rs = tr[x].mx = 1;
		else tr[x].ls = tr[x].rs = tr[x].mx = 0;
		return;
	}
	int mid = (tr[x].l + tr[x].r) / 2;
	if (k <= mid) update(x * 2, k, flag);
	else update(x * 2 + 1, k, flag);
	tr[x].ls = tr[x * 2].ls, tr[x].rs = tr[x * 2 + 1].rs;
	tr[x].mx = max(max(tr[x * 2].mx, tr[x * 2 + 1].mx), tr[x * 2].rs + tr[x * 2 + 1].ls);
	if (tr[x * 2].ls == tr[x * 2].r - tr[x * 2].l + 1) tr[x].ls += tr[x * 2 + 1].ls;
	if (tr[x * 2 + 1].rs == tr[x * 2 + 1].r - tr[x * 2 + 1].l + 1) tr[x].rs += tr[x * 2].rs;
}
int query(int x, int k)
{
	if (tr[x].mx == tr[x].r - tr[x].l + 1 || tr[x].mx == 0 || tr[x].l == tr[x].r)
		return tr[x].mx;
	int mid = (tr[x].l + tr[x].r) / 2;
	if (k <= mid)
	{
		if (k >= tr[x * 2].r - tr[x * 2].rs + 1)
			return query(x * 2, k) + query(x * 2 + 1, mid + 1);
		else
			return query(x * 2, k);
	}
	else
	{
		//printf("%d %d %d %d %d\n", k, x,tr[x*2+1].ls,tr[x*2+1].l,tr[x*2+1].r);
		if (k <= tr[x * 2 + 1].l + tr[x * 2 + 1].ls - 1)
			return query(x * 2 + 1, k) + query(x * 2, mid);
		else
			return query(x * 2 + 1, k);
	}
}
int main()
{
	int n, i, j, k, sum, m, cnt;
	char str[2];
	while (scanf("%d%d", &n, &m) != EOF)
	{
		cnt = 0;
		build(1, 1, n);
		for (i = 1;i <= m;i++)
		{
			scanf("%s", str);
			if (str[0] == 'R')
			{
				if (cnt > 0)
				{
					update(1, now[cnt], 1);
					cnt--;
				}
			}
			else
			{
				scanf("%d", &k);
				if (str[0] == 'Q')
					printf("%d\n", query(1, k));
				else
				{
					update(1, k, 0);
					now[++cnt] = k;
				}
			}
		}
	}
	return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值