您的位置:首页 > 其它

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

2015-08-05 20:47 435 查看
Tunnel Warfare

Time Limit: 1000MSMemory Limit: 131072K
Total Submissions: 7307Accepted: 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:

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

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.ll=node.rl=node.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.ll=node.rl=node.ml=1;
else
node.ll=node.rl=node.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.ll=node[tr<<1].ll;
node.rl=node[tr<<1|1].rl;
node.ml=max(node[tr<<1].ml,node[tr<<1|1].ml);
node.ml=max(node.ml,node[tr<<1].rl+node[tr<<1|1].ll);
if(node[tr<<1].ll==mid-l+1)
node.ll+=node[tr<<1|1].ll;
if(node[tr<<1|1].rl==r-(mid+1)+1)
node.rl+=node[tr<<1].rl;
}
int query(int pos,int l,int r,int tr)
{
if(l==r||node.ml==0||node.ml==r-l+1)
{
return node.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);
}
}
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: