您的位置:首页 > 其它

hdu 1540 Tunnel Warfare(区间合并)

2016-12-22 10:09 260 查看
[align=left]Problem Description[/align]
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!

 

[align=left]Input[/align]
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.

 

[align=left]Output[/align]
Output the answer to each of the Army commanders’ request in order on a separate line.

 

[align=left]Sample Input[/align]

7 9
D 3
D 6
D 5
Q 4
Q 5
R
Q 4
R
Q 4

 

[align=left]Sample Output[/align]

1
0
2
4

 

[align=left]题意:D代表破坏村庄,R代表修复最后被破坏的那个村庄,Q代表询问包括x在内的最大连续区间是多少[/align]
[align=left]思路:线段树的增删查操作,增删操作就是区间合并问题,当查的时候需要判断左区间右区间是否为满;[/align]
[align=left]代码:[/align]
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<stack>
using namespace std;
const int N=50005;
char c[10];
struct node{
int l,r;
int lr,ll,ml;
}str[3*N];
void build(int l,int r,int n)
{
str
.l=l;
str
.r=r;
str
.lr=str
.ll=str
.ml=r-l+1;
if(l==r)
return;
int temp=(l+r)/2;
build(l,temp,2*n);
build(temp+1,r,2*n+1);
}
void updata(int n)
{
str
.ll=str[2*n].ll;
str
.lr=str[2*n+1].lr;
if(str[2*n].ml==str[2*n].r-str[2*n].l+1)
str
.ll+=str[2*n+1].ll;
if(str[2*n+1].ml==str[2*n+1].r-str[2*n+1].l+1)
str
.lr+=str[2*n].lr;
str
.ml=max(max(str[2*n].ml,str[2*n+1].ml),str[2*n].lr+str[2*n+1].ll);
}
void del(int x,int n)
{
if(str
.l==str
.r)
{
str
.ll=str
.lr=str
.ml=0;
return;
}
int temp=(str
.l+str
.r)/2;
if(x<=temp)
del(x,2*n);
else
del(x,2*n+1);
updata(n);
}
void insert(int x,int n)
{
if(x==str
.l&&str
.l==str
.r)
{
str
.ll=str
.lr=str
.ml=1;
return;
}
int temp=(str
.l+str
.r)/2;
if(x<=temp)
insert(x,2*n);
else
insert(x,2*n+1);
updata(n);
}
int query(int x,int n)
{
if(str
.l==str
.r||str
.ml==0||str
.ml==str
.r-str
.l+1)
return str
.ml;
int temp=(str
.l+str
.r)/2;
if(x<=temp)
{
if(x>=str[2*n].r-str[2*n].lr+1)
return query(x,2*n)+query(temp+1,2*n+1);
else
return query(x,2*n);
}
else
{
if(x<=str[2*n+1].l+str[2*n+1].ll-1)
return query(temp,2*n)+query(x,2*n+1);
else
return query(x,2*n+1);
}
}
int main()
{
int n,m;
while(~scanf("%d%d",&n,&m))
{
build(1,n,1);
stack<int>s;
while(m--)
{
scanf("%s",c);
if(c[0]=='D')
{
int x;
scanf("%d",&x);
s.push(x);
del(x,1);
}
else if(c[0]=='R')
{
int x=s.top();
s.pop();
insert(x,1);
}
else
{
int x;
scanf("%d",&x);
int sum=query(x,1);
printf("%d\n",sum);
}
}
}
}


[align=left]
[/align]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: