您的位置:首页 > 其它

HDU 1540 Tunnel Warfare(线段树区间合并)

2017-07-16 21:03 441 查看
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


题解:

比赛的时候没写出来。。。这还是区间合并比较基础的题目,我还是太弱了qaq,搜了篇博客学了下

代码:

#include<algorithm>
#include<iostream>
#include<cstring>
#include<stdio.h>
#include<math.h>
#include<string>
#include<stdio.h>
#include<queue>
#include<stack>
#include<map>
#include<deque>
using namespace std;
stack<int>s;//由于要恢复上一个被破坏的所以要用栈
struct node
{
int l,r;
int ls,rs,ms;//ls表示区间从左起数的连续数,rs同理,ms是区间最大的连续数
};
node t[200005];
void Build(int l,int r,int num)
{
t[num].l=l;
t[num].r=r;
t[num].ls=t[num].rs=t[num].ms=r-l+1;//初始化为满长度
if(l==r)
return;
int mid=(l+r)/2;
Build(l,mid,num*2);
Build(mid+1,r,num*2+1);
}
void update(int x,int num,int d)
{
if(t[num].l==t[num].r)
{
t[num].ls=t[num].rs=t[num].ms=d;
return;
}
int mid=(t[num].l+t[num].r)/2;
if(x<=mid)
update(x,num*2,d);
else
update(x,num*2+1,d);
t[num].ms=max(max(t[num*2].ms,t[num*2+1].ms),t[num*2].rs+t[num*2+1].ls);//区间最大值为左区间最大,右区间最大,和左区间右连续+右区间左连续之中的最大值
t[num].ls=t[num*2].ls;
t[num].rs=t[num*2+1].rs;
if(t[num].ls==t[num*2].r-t[num*2].l+1)//如果左子区间是满的,那么加上右子区间的左连续
t[num].ls+=t[num*2+1].ls;
if(t[num].rs==t[num*2+1].r-t[num*2+1].l+1)//如果右子区间是满的,那么加上左子区间的右连续
t[num].rs+=t[num*2].rs;
}
int query(int x,int num)
{
if(t[num].l==t[num].r||t[num].ms==0||t[num].ms==t[num].r-t[num].l+1)//如果访问到了该点或者区间全满或者全空都直接退出访问
return t[num].ms;
int mid=(t[num].l+t[num].r)/2;
if(x<=mid)
{
if(x>=t[num*2].r-t[num*2].rs+1)//重点理解!!右边式子代表左子树右边连续区间的左边界值,如果x在左子树的右区间内,则要看右子树的左区间有多长并返回
return query(x,num*2)+query(mid+1,num*2+1);
else
return query(x,num*2);
}
else
{
if(x<=t[num*2+1].l+t[num*2+1].ls-1)//同理
return query(x,num*2+1)+query(mid,num*2);
else
return query(x,num*2+1);
}
}
int main()
{
int i,j,n,m,d,x;
char c;
scanf("%d%d",&n,&m);
Build(1,n,1);
for(i=0;i<m;i++)
{
getchar();
scanf("%c",&c);
if(c=='D')
{
scanf("%d",&x);
s.push(x);
update(x,1,0);
}
else if(c=='R')
{
x=s.top();
s.pop();
update(x,1,1);
}
else
{
scanf("%d",&x);
printf("%d\n",query(x,1));
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: