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

Tunnel Warfare

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


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
 
思路:
这道题主要难点在于求出Q操作,Q  x要求x左右两边连续村庄的个数,之前一直没想到怎么把一个点转成一个区间,其实我们可以直接将这个区间分为两部分:
1 - x,x - n,求出1 - x从最右边开始连续区间的长度,和x - n,从最左边开始连续区间的长度,这样就转换成了很简单的区间合并问题了。最后因为中间重复求
了x的值,当x未被摧毁时相当于多加了1,减掉1就好了,如果x是被摧毁的点,那么他左右区间相加必为0,减一的话就变成-1了,我们把他变成0就好了
 
实现代码:
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define mid int m = (l + r) >> 1
const int M = 1e5+10;
int lsum[M<<2],rsum[M<<2];
void pushup(int l,int r,int rt){
    lsum[rt] = lsum[rt<<1];
    rsum[rt] = rsum[rt<<1|1];
    mid;
    if(lsum[rt] == m-l+1) lsum[rt] += lsum[rt<<1|1];
    if(rsum[rt] == r - m) rsum[rt] += rsum[rt<<1];
}

void update(int p,int c,int l,int r,int rt){
    if(l == r){
        lsum[rt] = rsum[rt] = c;
        return ;
    }
    mid;
    if(p <= m) update(p,c,lson);
    else update(p,c,rson);
    pushup(l,r,rt);
}

void build(int l,int r,int rt){
    if(l == r){
        lsum[rt] = rsum[rt] = 1;
        return ;
    }
    mid;
    build(lson);
    build(rson);
    pushup(l,r,rt);
}
int queryl(int L,int R,int l,int r,int rt){
    if(L <= l&&R >= 0){
        return lsum[rt];
    }
    mid;
    if(L > m) return queryl(L,R,rson);
    if(R <= m) return queryl(L,R,lson);
    int t1 = queryl(L,m,lson);
    int t2 = queryl(m+1,R,rson);
    if(t1 == m-L+1) t1+=t2;
    return t1;
}

int queryr(int L,int R,int l,int r,int rt){
     if(L <= l&&R >= r){
        return rsum[rt];
     }
     mid;
     if(L > m) return queryr(L,R,rson);
     if(R <= m) return queryr(L,R,lson);
     int t1 = queryr(L,m,lson);
     int t2 = queryr(m+1,R,rson);
     if(t2 == R - m) t2 += t1;
     return t2;
}
stack<int>st;
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    int n,m,x;
    char op;
     while(cin>>n>>m){
        memset(lsum,0,sizeof(lsum));
        memset(rsum,0,sizeof(rsum));
        build(1,n,1);
        while(m--){
            cin>>op;
            if(op == 'D'){
                cin>>x;
                update(x,0,1,n,1);
                st.push(x);
            }
            else if(op == 'R'){
                x = st.top();
                //cout<<x<<endl;
                st.pop();
                update(x,1,1,n,1);
            }
            else{
                cin>>x;
                int num = queryr(1,x,1,n,1) + queryl(x,n,1,n,1)-1;
                if(num < 0) num = 0;
                //cout<<queryr(1,x,1,n,1)<<" "<<queryl(x,n,1,n,1)<<endl;
                cout<<num<<endl;
            }
        }
     }
     return 0;
}

猜你喜欢

转载自www.cnblogs.com/kls123/p/8921418.html