[线段树 区间合并] I - Tunnel Warfare HDU - 1540

Tunnel Warfare

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


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



#include <bits/stdc++.h>
using namespace std;

const int mn = 50010;

struct node
{
	int l, r;
	int ls, rs, ms;     /// 存左连续 右连续 最长连续3个值
} t[4 * mn];

void build(int i, int l, int r)
{
	t[i].l = l, t[i].r = r;
	if (l == r)
	{
		t[i].ls = t[i].rs = t[i].ms = 1;
		return;
	}
	int m = (l + r) / 2;
	build(2 * i, l, m);
	build(2 * i + 1, m + 1, r);
	t[i].ls = t[i].rs = t[i].ms = t[2 * i].ms + t[2 * i + 1].ms;
	return;
}

bool y;
void update(int i, int a)
{
	if (t[i].l == t[i].r)
	{
		if (y == 1)  // 修复
			t[i].ls = t[i].rs = t[i].ms = 1;
		else if (y == 0)  // 破坏
			t[i].ls = t[i].rs = t[i].ms = 0;
		return;
	}

	int m = (t[i].l + t[i].r) / 2;
	if (a <= m)
		update(2 * i, a);
	else // if (a > m)
		update(2 * i + 1, a);

	t[i].ls = t[2 * i].ls;
	t[i].rs = t[2 * i + 1].rs;
	/// 若半区间内无分割 则加上另半区间的分割值
	if (t[2 * i].ls == (t[2 * i].r - t[2 * i].l + 1))
		t[i].ls += t[2 * i + 1].ls;
	if (t[2 * i + 1].rs == (t[2 * i + 1].r - t[2 * i + 1].l + 1))
		t[i].rs += t[2 * i].rs;

	t[i].ms = max(t[2 * i].rs + t[2 * i + 1].ls, max(t[2 * i].ms, t[2 * i + 1].ms));

	return;
}

int query(int i, int a)
{
	int l = t[i].l, r = t[i].r;
	if (l == r || (t[i].ms == r - l + 1) || t[i].ms == 0)
		return t[i].ms;  /// ↑ 区间内不分割 当年区间ms=a.ms

	int m = (l + r) / 2;
	if (a <= m)  // 左子树
	{
	    /// a 在左子树的右区间
		if (a >= t[2 * i].r - t[2 * i].rs + 1)
			return query(2 * i, a) + query(2 * i + 1, m + 1);
		else
			return query(2 * i, a);
	}
	else // if (a > m)  // 右子树
	{
		if (a < t[2 * i + 1].l + t[2 * i + 1].ls)
			return query(2 * i + 1, a) + query(2 * i, m);
		else
			return query(2 * i + 1, a);
	}
}

int d[mn];
int main()
{
	int n, m;
	while (~scanf("%d %d", &n, &m))
	{
		build(1, 1, n);

		int t = 0;
		while (m--)
		{
			char ch[5];
			scanf("%s", ch);
			if (ch[0] == 'D')
			{
				int a;
				scanf("%d", &a);
				d[t++] = a;
				y = 0;
				update(1, a);  // 删除
			}
			else if (ch[0] == 'Q')
			{
				int a;
				scanf("%d", &a);
				printf("%d\n", query(1, a));  // 查询a所在区间的ms
			}
			else if (ch[0] == 'R')
			{
				int a = d[--t];
				y = 1;
				update(1, a);  // 修复
			}
		}
	}
	return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值