hdu 1540 Tunnel Warfare (line segment tree interval merge)

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
 

 

Ideas:
The main difficulty of this problem lies in finding the Q operation. Q x requires the number of consecutive villages on the left and right sides of x. I have never thought of how to convert a point into an interval. In fact, we can directly divide this interval into two parts:
1 - x, x - n, find the length of the continuous interval from 1 - x from the far right, and x - n, the length of the continuous interval from the far left, which is converted into a very simple interval merging problem. Finally, because of repeated requests in the middle
The value of x, when x is not destroyed, is equivalent to adding 1 more, and subtracting 1 is fine. If x is the point that was destroyed, then the addition of his left and right intervals must be 0, and if it is subtracted by 1, it becomes -1 Now, let's make it 0
 
Implementation code:
#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 >> on;
            if (at == ' 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;
}

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324774987&siteId=291194637