Tunnel Warfare

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 
#include 
#include 

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;
 } 


你可能感兴趣的:(线段树)