HDU 1540 Tunnel Warfare (线段树)

时间:2021-02-14 21:58:46

Tunnel Warfare

Problem 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
2
4
题目大意:有n个村庄,初始的时候都连通,有m次操作,每一次不同的操作会改变村庄的连通情况或者是查询某个村庄的情况。
思路:啊!真的是蠢到家了,一开始想的是用线段树+二分来做,后头想了一想好像有点不对劲,明明线段树就是一次二分了,为啥还要加一重二分呢?。。。(我怕真是个*)
  后来借鉴了一下大佬的博客https://blog.csdn.net/libin56842/article/details/14105071 哎呀,用了肥虎之力,总算是把这个题给ac了。
  总的来说,在进行每一次点更新的时候我们维护三个值,对于当前的这个点,维护左边最大连续的区间,右边最大的连续区间,和当前最大的连续区间,在点更新的时候不断更新父亲节点的区间即可
 #include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<stack> using namespace std;
const int maxn = ;
struct node{
int l,r;
int ls,rs,ms;//ls左端最大连续区间,rs右边最大连续区间,ms最大连续区间
}sum[maxn<<];
void build(int l,int r,int rt)
{
sum[rt].l=l;sum[rt].r=r;
sum[rt].ls=sum[rt].rs=sum[rt].ms=(r-l+);
if(l!=r){
int mid = (l+r)>>;
build(l,mid,rt<<);
build(mid+,r,rt<<|);
}
}
void update(int L,int R,int rt)
{
if(sum[rt].l==sum[rt].r){//单点更新操作的村庄
if(R==)
sum[rt].ls = sum[rt].rs = sum[rt].ms = ;
else
sum[rt].ls = sum[rt].rs = sum[rt].ms = ;
return;
}
int mid = (sum[rt].l+sum[rt].r)>>;
if(L<=mid) update(L,R,rt<<);//向左边找
else update(L,R,rt<<|);//右边找
//更新子区间之后再更新当前的区间
sum[rt].ls = sum[rt<<].ls;
sum[rt].rs = sum[rt<<|].rs;
sum[rt].ms = max(max(sum[rt<<].ms,sum[rt<<|].ms),sum[rt<<].rs+sum[rt<<|].ls);
if(sum[rt<<].ls==sum[rt<<].r-sum[rt<<].l+)//如果左子树满了需要加上右子树的左区间
sum[rt].ls += sum[rt<<|].ls;
if(sum[rt<<|].rs==sum[rt<<|].r-sum[rt<<|].l+)//同理
sum[rt].rs += sum[rt<<].rs;
}
int query(int L,int rt)
{
if(sum[rt].l==sum[rt].r||sum[rt].ms==||sum[rt].ms==(sum[rt].r-sum[rt].l+))
return sum[rt].ms;//返回的条件
int mid = (sum[rt].l+sum[rt].r)>>;
if(L<=mid){//如果要查的点再mid的左子树
if(L>=sum[rt<<].r-sum[rt<<].rs+)return query(L,rt<<)+query(mid+,rt<<|);//左区间已满需要加上右子树的左边
else return query(L,rt<<);//左子树未满,只需要加左子树的值
}else{
if(L<=sum[rt<<|].l+sum[rt<<|].ls-)return query(L,rt<<|)+query(mid,rt<<);//同理
else query(L,rt<<|);
}
}
int main()
{
int n,m,x;char op;
while(scanf("%d%d",&n,&m)!=EOF){
stack<int>Q;
build(,n,);
while(m--){
scanf(" %c",&op);//去掉行末换行符的影响
if(op=='D'){
scanf("%d",&x);
Q.push(x);
update(x,,);
}else if(op=='Q'){
scanf("%d",&x);
printf("%d\n",query(x,));
}else if(op=='R'){
if(!Q.empty()&&x>){
update(Q.top(),,);
Q.pop();
}
}
}
}
return ;
}