当前位置:编程学习 > C/C++ >>

POJ 2892 Tunnel Wa易做图re

Tunnel Wa易做图re
Time Limit: 1000MS Memory Limit: 131072K
Total Submissions: 6386 Accepted: 2620
Description
During the War of Resistance Against Japan, tunnel wa易做图re 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
Hint
An illustration of the sample input:
      OOOOOOO
 
D 3   OOXOOOO
 
D 6   OOXOOXO
 
D 5   OOXOXXO
 
R     OOXOOXO
 
R     OOXOOOO
Source
POJ Monthly--2006.07.30, updog
 
  线段树:区间最值
#include <iostream>  
#include <cstring>  
#include <cstdio>  
#include <stack>  
#define N 50010  
using namespace std;  
struct num  
{  
    int l,r;  
    int Max,Min;  
}a[4*N];  
char s1[5];  
bool status[N];  
int res,n,m;  
int main()  
{  
    //freopen("data.in","r",stdin);  
    void pre_build(int k,int l,int r);  
    void update(int k,int l,int val);  
    void find_max(int k,int l,int r);  
    void find_min(int k,int l,int r);  
    while(scanf("%d %d",&n,&m)!=EOF)  
    {  
        pre_build(1,1,n);  
        stack<int>q;  
        memset(status,true,sizeof(status));  
        while(m--)  
        {  
            scanf("%s",s1);  
            int x;  
            if(s1[0]=='D')  
            {  
                scanf("%d",&x);  
                status[x]=false;  
                q.push(x);  
                update(1,x,x);  
            }else if(s1[0]=='Q')  
            {  
                scanf("%d",&x);  
                int s=0;  
                if(status[x])  
                {  
                    s++;  
                }  
                if(s==0)  
                {  
                    printf("0\n");  
                    continue;  
                }  
                res=0;  
                if(x-1>=1)  
                {  
                    find_max(1,1,x-1);  
                }  
                s+=(x-1-res);  
                res=(n+1);  
                if(x+1<=n)  
                {  
                    find_min(1,x+1,n);  
                }  
                s+=(res-(x+1));  
                printf("%d\n",s);  
            }else  
            {  
                int s=q.top();  
                q.pop();  
                status[s]=true;  
                update(1,s,0);  
            }  
        }  
    }  
    return 0;  
}  
void pushup(int k)  
{  
    a[k].Max=max(a[k<<1].Max,a[k<<1|1].Max);  
    a[k].Min=min(a[k<<1].Min,a[k<<1|1].Min);  
}  
void pre_build(int k,int l,int r)  
{  
    a[k].l=l;  
    a[k].r=r;  
    if(l==r)  
    {  
        a[k].Max=0;  
        a[k].Min=(n+1);  
        return ;  
    }  
    int mid=(l+r)>>1;  
    pre_build(k<<1,l,mid);  
    pre_build(k<<1|1,mid+1,r);  
    pushup(k);  
}  
void find_max(int k,int l,int r)  
{  
    if(a[k].l==l&&a[k].r==r)  
    {  
        res=max(res,a[k].Max);  
        return ;  
    }  
    int mid=(a[k].l+a[k].r)>>1;  
    if(r<=mid)  
    {  
        find_max(k<<1,l,r);  
    }else if(l>mid)  
    {  
        find_max(k<<1|1,l,r);  
    }else  
    {  
        find_max(k<<1,l,mid);  
        find_max(k<<1|1,mid+1,r);  
    }  
}  
void find_min(int k,int l,int r)  
{  
    if(a[k].l==l&&a[k].r==r)  
    {  
        res=min(res,a[k].Min);  
        return ;  
    }  
    int mid=(a[k].l+a[k].r)>>1;  
    if(r<=mid)  
    {  
        find_min(k<<1,l,r);  
    }else if(l>mid)  
    {  
        find_min(k<<1|1,l,r);  
    }else  
    {  
        find_min(k<<1,l,mid);  
        find_min(k<<1|1,mid+1,r);  
    }  
}  
void update(int k,int l,int val)  
{  
    if(a[k].l==a[k].r)  
    {  
        if(val==0)  
        {  
         a[k].Max=0;  
         a[k].Min=(n+1);  
        }else  
        {  
            a[k].Max=a[k].Min=val;  
        }  
        return ;  
    }  
    int mid=(a[k].l+a[k].r)>>1;  
    if(mid>=l)  
    {  
        update(k<<1,l,val);  
    }else  
    {  
        update(k<<1|1,l,val);  
    }  
    pushup(k);  
}  

 


补充:软件开发 , C++ ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,