Tunnel Warfare(HDU-1540)

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
n 개 마을 m 개 조작, D x 파괴 호 마을, Q x 대 표 는 x 를 포함 한 최대 연속 구간 을 물 었 고, R 대 표 는 마지막 에 파 괴 된 마을 을 복원 했다.
사고: 선분 트 리 구간 통합
선분 수 를 이용 하여 구간 의 최대 치 와 최소 치 를 구하 여 마을 의 연속 구간 을 구 할 수 있다.
예 를 들 어 5 개 마을: 12345, 몇 개의 마을 이 파괴 되 었 다.
Q 3 를 드 리 겠 습 니 다. 그러면 [1, 3] 의 max, [3, 5] 의 min 을 요구 하고 이들 에 따라 연속 구간 을 구하 면 됩 니 다.
min - max - 1
Source Program
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define PI acos(-1.0)
#define E 1e-9
#define INF 0x3f3f3f3f
#define N 100001
#define LL long long
const int MOD=20091226;
const int dx[]= {-1,1,0,0};
const int dy[]= {0,0,-1,1};
using namespace std;
struct Node{
    int l,r;
    int maxx,minn;
} tree[N*4];
int history[N*4];//      
int n,m;
void build(int i,int l,int r){//  
    tree[i].l=l;
    tree[i].r=r;

    if(l==r){
        tree[i].minn=n+1;
        tree[i].maxx=0;
        return;
    }

    int mid=(l+r)>>1;
    build(i*2,l,mid);//      
    build(i*2+1,mid+1,r);//      

    tree[i].maxx=max(tree[i*2].maxx,tree[i*2+1].maxx);
    tree[i].minn=min(tree[i*2].minn,tree[i*2+1].minn);
}
void updateMin(int i,int id,int val){//     
    if(tree[i].l==tree[i].r){
        tree[i].minn=val;
        return;
    }

    int mid=(tree[i].l+tree[i].r)/2;
    if(id<=mid)
        updateMin(i*2,id,val);
    else
        updateMin(i*2+1,id,val);

    tree[i].minn=min(tree[i*2].minn,tree[i*2+1].minn);
}
void updateMax(int i,int id,int val){//     
    if(tree[i].l==tree[i].r){
        tree[i].maxx=val;
        return;
    }

    int mid=(tree[i].l+tree[i].r)/2;
    if(id<=mid)
        updateMax(i*2,id,val);
    else
        updateMax(i*2+1,id,val);

    tree[i].maxx=max(tree[i*2].maxx,tree[i*2+1].maxx);
}
int queryMin(int i,int ql,int qr){//     
    if(ql<=tree[i].l&&qr>=tree[i].r)//          
        return tree[i].minn;

    int mid=(tree[i].l+tree[i].r)/2;

    int res=INF;
    if(ql<=mid)
        res=min(res,queryMin(i*2,ql,qr));
    if(qr>mid)
        res=min(res,queryMin(i*2+1,ql,qr));

    return res;
}
int queryMax(int i,int ql,int qr){//     
    if(ql<=tree[i].l&&qr>=tree[i].r)//          
        return tree[i].maxx;

    int mid=(tree[i].l+tree[i].r)/2;

    int res=0;
    if(ql<=mid)
        res=max(res,queryMax(i*2,ql,qr));
    if(qr>mid)
        res=max(res,queryMax(i*2+1,ql,qr));

    return res;
}
int main(){

    while(scanf("%d%d",&n,&m)!=EOF){
        build(1,1,n);
        int cnt=0;
        memset(history,0,sizeof(history));
        while(m--){
            char str[5];
            scanf("%s",str);
            if(str[0]=='D'){
                int x;
                scanf("%d",&x);
                updateMax(1,x,x);// x       x
                updateMin(1,x,x);// x       x
                history[++cnt]=x;
            }
            else if(str[0]=='Q'){
                int x;
                scanf("%d",&x);
                int maxx=queryMax(1,1,x);
                int minn=queryMin(1,x,n);
                if(maxx==minn)//  
                    printf("0
"); else printf("%d
",minn-maxx-1); } else{ int temp=history[cnt--]; // , updateMin(1,temp,n+1); updateMax(1,temp,0); } } } return 0; }

좋은 웹페이지 즐겨찾기