Tunnel Warfare

本文探讨了在抗日战争期间,华北平原上利用隧道进行的游击战。详细描述了村庄通过隧道相连的方式,以及入侵者破坏隧道后,八路军司令部如何请求最新的村庄连接状态。文章涉及单点修改、线段树合并和查询等算法,旨在快速重建被破坏的隧道连接,确保村庄间的有效通信。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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:

  1. D x: The x-th village was destroyed.
  2. Q x: The Army commands requested the number of villages that x-th village was directly or indirectly connected with including itself.
  3. 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

Hint

An illustration of the sample input:

      OOOOOOO

D 3   OOXOOOO

D 6   OOXOOXO

D 5   OOXOXXO

R     OOXOOXO

R     OOXOOOO


题解:线段树合并,单点修改。查询的时候先在节点的中间连续部分去找,如果中间不连续,说明连续部分不在左孩子就在右孩子,因为左右孩子不可能连在一起(也就是断开的),如果连续,可能在该部分,如果不在,说明在孩子里面。画图解决。


#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;

struct Node
{
	int l,r;
	int ls,rs,ms;
};

Node arr[200005];
int d[50004];
int k;

void pushUp(int k)
{
	int li = k << 1;
	int ri = (k << 1) | 1;
	arr[k].ls = arr[li].ls;
	arr[k].rs = arr[ri].rs;
	arr[k].ms = 0; 
	if(arr[li].rs != 0 && arr[ri].ls != 0)
	{
		arr[k].ms = arr[li].rs + arr[ri].ls;
		if(arr[li].ls == arr[li].r - arr[li].l + 1)
		{
			arr[k].ls += arr[ri].ls;
		}
		if(arr[ri].rs == arr[ri].r - arr[ri].l + 1)
		{
			arr[k].rs += arr[li].rs;
		}
	}
}

void segTree(int k,int l,int r)
{
	arr[k].l = l;
	arr[k].r = r;
	if(l == r)
	{
		arr[k].ls = arr[k].rs = arr[k].ms = 1;
		return;
	}
	int mid = (l + r) >> 1;
	segTree(k << 1,l,mid);
	segTree((k << 1) | 1,mid + 1,r);
	pushUp(k);
}

void update(int k,int x,int y)
{
	if(arr[k].l == arr[k].r)
	{
		arr[k].ls = y;
		arr[k].rs = y;
		arr[k].ms = y;
		return;
	}
	int mid = (arr[k].l + arr[k].r) >> 1;
	if(x <= mid)
	{
		update(k << 1,x,y);
	}
	else
	{
		update((k << 1) | 1,x,y);
	}
	pushUp(k);
}

int query(int k,int x)   //不在中间(说明对于该点两个孩子没有相交部分)就在孩子里面, 
{
	if(arr[k].l == arr[k].r)
	{
		return arr[k].ls;
	}
	int mid = (arr[k].l + arr[k].r) >> 1; 
    if(arr[k].ms != 0)
	{
		if(x > arr[k << 1].r - arr[k << 1].rs && x < arr[(k << 1) | 1].l + arr[(k << 1) | 1].ls)
		{
			return arr[k].ms;
		}
	} 
	if(x <= mid)
	{
		return query(k << 1,x);
	}
	return query((k << 1) | 1,x);
}

int main()
{
	int n,m;
	while(scanf("%d%d",&n,&m) != EOF)
	{
		char s[3];
		int x;
		k = 0;
		segTree(1,1,n);
		while(m--)
		{
			scanf("%s",s);
			if(s[0] == 'D')
			{
				scanf("%d",&x);
				d[k++] = x;
				update(1,x,0);
			}
			else if(s[0] == 'R')
			{
				update(1,d[--k],1);
			}
			else
			{
				scanf("%d",&x);
				printf("%d\n",query(1,x));
			}
		}
	}
	
	
	return 0;
 } 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值