HDU 1540 Tunnel Warfare 地道战 线段树

Tunnel Warfare

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


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
 
只要维护左右最长连续长度就可以过了 区间合并

#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
const int MAX_N = 50050;
int lmax[MAX_N<<2],rmax[MAX_N<<2];
int Stack[MAX_N];
void up(int p,int l,int r){
    int mid = (l+r)>>1;
   lmax[p] = lmax[p*2];
   if(lmax[p]==(mid-l+1)){
       lmax[p]+=lmax[p*2+1];
   }
   rmax[p]= rmax[p*2+1];
   if(rmax[p]==(r-mid)){
       rmax[p]+=rmax[p*2];
   }
}
void build(int p,int l,int r){
   if(l==r){
    lmax[p]=rmax[p] = 1;
    return ;
   }
   int mid = (l+r)>>1;
   build(p*2,l,mid);
   build(p*2+1,mid+1,r);
   up(p,l,r);
}
void change(int p,int l,int r,int x,int v){
    if(l==r){
        lmax[p] = rmax[p] = v;
        return;
    }
    int mid =(l+r)>>1;
    if(x<=mid) change(p*2,l,mid,x,v);
    else change(p*2+1,mid+1,r,x,v);
    up(p,l,r);
}

int lnum,rnum;

int query(int p,int l,int r,int x){
   if(l==r){
    lnum = rnum = x;
    return lmax[p];
   }
   int mid = (l+r)>>1;
   if(x<=mid){
    int ans = query(p*2,l,mid,x);
    if(!ans) return 0;
    else if(rnum==mid&&lmax[p*2+1]){
        rnum+=lmax[p*2+1];
        ans+=lmax[p*2+1];
    }
            return ans;
   }
   else {
    int ans = query(p*2+1,mid+1,r,x);
    if(!ans) return 0;
    else if(lnum == mid+1 && rmax[p*2]){
        lnum-=rmax[p*2];
        ans+=rmax[p*2];
    }
            return ans;
   }
}

int main(){
    char str[5];
   int n,m,cnt;
   while(~scanf("%d%d",&n,&m)){
   cnt = 0;
   build(1,1,n);
   while(m--){
    scanf("%s",str);
    if(str[0]=='D'){
        int d;
        scanf("%d",&d);
        Stack[cnt++] = d;
        change(1,1,n,d,0);
    }
    else if(str[0]=='Q'){
        int d;
        scanf("%d",&d);
        printf("%d\n",query(1,1,n,d));
    }
    else {
        if(cnt==0) continue;
        change(1,1,n,Stack[cnt-1],1);
        cnt--;
    }
   }
   }
   return 0;
}

猜你喜欢

转载自blog.csdn.net/heucodesong/article/details/80698787