您的位置:首页 > 其它

文章标题 HDU 1540 : Tunnel Warfare (线段树+最大连续区间)

2017-06-30 11:53 477 查看

Tunnel Warfare

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

题意:题意:D代表破坏村庄,R代表修复最后被破坏的那个村庄,Q代表询问包括x在内的最大连续区间是多少

分析:线段树求解,节点上保存ml 记录区间左端点开始的最大连续个数,rl 记录区间右端点开始的最大的连续个数,ms 表示该区间最大的连续点的个数。

代码:

#include<iostream>
#include<string>
#include<cstdio>
#include<cstring>
#include<vector>
#include<math.h>
#include<map>
#include<queue>
#include<algorithm>
using namespace std;
const int inf = 0x3f3f3f3f;
typedef pair<int,int> pii;

const int maxn=50005<<2;

int ml[maxn],mr[maxn],ms[maxn];

void push_up(int rt,int len){
ml[rt]=ml[rt*2];
mr[rt]=mr[rt*2+1];
ms[rt]=max(ml[rt*2],mr[rt*2+1]);
ms[rt]=max(ms[rt],ml[rt*2+1]+mr[rt*2]);
//左子树区间满了的话,父亲左区间要加上右孩子的左区间
if (ml[rt]==(len-len/2)){
ml[rt]+=ml[rt*2+1];
}
//同理
if (mr[rt]==(len/2)){
mr[rt]+=mr[rt*2];
}

}

void build(int rt,int l,int r){
ml[rt]=mr[rt]=ms[rt]=r-l+1;
if (l==r)return;
int mid=(r+l)/2;
build(rt*2,l,mid);
build(rt*2+1,mid+1,r);
}

void update(int rt,int l,int r,int val,int place){//place 为位置,val为1表示恢复,0表示破坏
if (l==r){
if (val){
ml[rt]=mr[rt]=ms[rt]=1;
}
else ml[rt]=mr[rt]=ms[rt]=0;
return;
}
int mid=(l+r)/2;
if (place<=mid)update(rt*2,l,mid,val,place);//左子树
if (place>mid)update(rt*2+1,mid+1,r,val,place);//右子树
push_up(rt,r-l+1);
}

int query(int rt,int l,int r,int place){
if (l==r||ms[rt]==0||ms[rt]==(r-l+1)){
return ms[rt];
}
int mid=(l+r)/2;
if (place<=mid){
if (place>=mid-mr[rt*2]+1){
//因为place<=mid,看左子树,
//mid-mr[rt*2]+1表左子树右边连续区间的左边界值,
//果t在左子树的右区间内,则要看右子树的左区间有多长并返回
return query(rt*2,l,mid,place)+query(rt*2+1,mid+1,r,mid+1);
}
else return query(rt*2,l,mid,place);
}
else {//同理
if (place<=mid+ml[rt*2+1]){
return query(rt*2+1,mid+1,r,place)+query(rt*2,l,mid,mid);
}
else return query(rt*2+1,mid+1,r,place);
}

}

int N,M;
int st[50005],top;//栈,保存破坏的村庄

int main ()
{

while (scanf ("%d%d",&N,&M)!=EOF){
build(1,1,N);
char ch[10];
int tmp;
top=0;
while (M--){
scanf ("%s",ch);
if (ch[0]=='D'){
scanf ("%d",&tmp);
st[++top]=tmp;
update(1,1,N,0,tmp);//更新
}else if (ch[0]=='Q'){
scanf ("%d",&tmp);
printf ("%d\n",query(1,1,N,tmp));
}else if (ch[0]=='R'){
tmp=st[top];top--;
update(1,1,N,1,tmp);

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