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

Tunnel Warfare

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 12244    Accepted Submission(s): 4794

 

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 0 2 4

Source

POJ Monthly

 有注释应该好理解,如果是对线段树的区间合并一无所知的童鞋,你先看看这个博客(这个人挺牛的):

 https://www.cnblogs.com/scau20110726/archive/2013/05/07/3065418.html

#include 
#define root 1,n,1
#define lc rt<<1
#define rc rt<<1|1
#define mid ((l+r)>>1)
#define lson l,mid,lc
#define rson mid+1,r,rc
using namespace std;
const int maxn=50000+10;
int lsum[maxn<<2],rsum[maxn<<2];
void build(int l,int r,int rt)
{
    lsum[rt]=rsum[rt]=r-l+1;
    if(l==r) return;
    build(lson);
    build(rson);
}
void update(int l,int r,int rt,int c,int f)
{
    if(l==r){
        if(f) lsum[rt]=rsum[rt]=0;
        else lsum[rt]=rsum[rt]=1;
        return;
    }
    if(c<=mid) update(lson,c,f);
    else update(rson,c,f);
    int len=r-l+1;
    lsum[rt]=lsum[lc];
    rsum[rt]=rsum[rc];
    if(lsum[rt]==len-(len>>1)) lsum[rt]+=lsum[rc];
    if(rsum[rt]==(len>>1)) rsum[rt]+=rsum[lc];
}
int query(int l,int r,int rt,int c)
{
    if(l==r) return lsum[rt];
    int ans=-1;
    if(l<=c&&c<=l+lsum[rt]-1) ans=lsum[rt];//如果在最左连续
    if(r-rsum[rt]+1<=c&&c<=r) ans=max(ans,rsum[rt]);//如果在最右连续
    //如果在横跨连续,右边界限不要写成mid+lsum[rc]-1,因为右区间是从mid+1开始的所以要加一(这地方找了许久,尴尬)
    if(mid-rsum[lc]+1<=c&&c<=mid+lsum[rc]) ans=max(ans,lsum[rc]+rsum[lc]);
    if(ans!=-1) return ans;//若有返回最大的,否则再去找更小区间
    if(c<=mid) return query(lson,c);
    else return query(rson,c);
}
int main ()
{
    int n,m,x;
    while(~scanf("%d%d",&n,&m))
    {
        int st[maxn],k=0;
        build(root);
        while(m--){
            char s[2];
            scanf("%s",s);
            if(s[0]=='D'){
                scanf("%d",&x);
                st[k++]=x;
                update(root,x,1);
            }else if(s[0]=='Q'){
                scanf("%d",&x);
                printf("%d\n",query(root,x));
            }
            else
                update(root,st[--k],0);
        }
    }
    return 0;
}

 

你可能感兴趣的:(线段树,线段树)