码迷,mamicode.com
首页 > 其他好文 > 详细

POJ 题目2892 Tunnel Warfare(线段树单点更新查询,求单点所在最大连续区间长度)

时间:2015-08-05 22:23:25      阅读:209      评论:0      收藏:0      [点我收藏+]

标签:

Tunnel Warfare
Time Limit: 1000MS   Memory Limit: 131072K
Total Submissions: 7307   Accepted: 2997

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

Source

POJ Monthly--2006.07.30, updog

ac代码

#include<stdio.h>
#include<string.h>
#define max(a,b) (a>b?a:b)
struct s
{
	int rl,ll,ml;
}node[50500<<2];
int stack[50500],top;
void build(int l,int r,int tr)
{
	node[tr].ll=node[tr].rl=node[tr].ml=r-l+1;
	if(l==r)
		return;
	int mid=(l+r)>>1;
	build(l,mid,tr<<1);
	build(mid+1,r,tr<<1|1);
}
void update(int pos,int l,int r,int tr,int val)
{
	if(l==r)
	{
		if(val)
			node[tr].ll=node[tr].rl=node[tr].ml=1;
		else
			node[tr].ll=node[tr].rl=node[tr].ml=0;
		return;
	}
	int mid=(l+r)>>1;
	if(pos<=mid)
	{
		update(pos,l,mid,tr<<1,val);
	}
	else
		update(pos,mid+1,r,tr<<1|1,val);
	node[tr].ll=node[tr<<1].ll;
	node[tr].rl=node[tr<<1|1].rl;
	node[tr].ml=max(node[tr<<1].ml,node[tr<<1|1].ml);
	node[tr].ml=max(node[tr].ml,node[tr<<1].rl+node[tr<<1|1].ll);
	if(node[tr<<1].ll==mid-l+1)
		node[tr].ll+=node[tr<<1|1].ll;
	if(node[tr<<1|1].rl==r-(mid+1)+1)
		node[tr].rl+=node[tr<<1].rl;
}
int query(int pos,int l,int r,int tr)
{
	if(l==r||node[tr].ml==0||node[tr].ml==r-l+1)
	{
		return node[tr].ml;
	}
	int mid=(l+r)>>1;
	if(pos<=mid)
	{
		if(pos>=mid-node[tr<<1].rl+1)
			return query(pos,l,mid,tr<<1)+query(mid+1,mid+1,r,tr<<1|1);
		return query(pos,l,mid,tr<<1);
	}
	else
	{
		if(pos<=mid+1+node[tr<<1|1].ll-1)
			return query(pos,mid+1,r,tr<<1|1)+query(mid,l,mid,tr<<1);
		return query(pos,mid+1,r,tr<<1|1);
	}
}
int main()
{
	int n,m;
	while(scanf("%d%d",&n,&m)!=EOF)
	{
		build(1,n,1);
		top=0;
		while(m--)
		{
			char s[2];
			int x;
			scanf("%s",s);
			if(s[0]=='D')
			{
				scanf("%d",&x);
				stack[top++]=x;
				update(x,1,n,1,0);
			}
			else
			{
				if(s[0]=='Q')
				{
					int x;
					scanf("%d",&x);
					int ans=query(x,1,n,1);
					printf("%d\n",ans);
				}
				else
				{
					int x=stack[top-1];
					top--;
					update(x,1,n,1,1);
				}
			}
		}
	}
}


版权声明:本文为博主原创文章,未经博主允许不得转载。

POJ 题目2892 Tunnel Warfare(线段树单点更新查询,求单点所在最大连续区间长度)

标签:

原文地址:http://blog.csdn.net/yu_ch_sh/article/details/47303211

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!