Tunnel Warfare

HDU 1540

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

REEE

🙃

代码

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<stack>
#include<queue>
#include<set>
using namespace std;
/*template<typename T>inline void read(T &x)
{
    x=0;
    T f=1;
    char c=getchar();
    for(; c<'0'||c>'9'; c=getchar())
        if(c=='-')
            f=-1;
    for(; c>='0'&&c<='9'; c=getchar())
        x=(x<<1)+(x<<3)+(c&15);
    x*=f;
}*/
/*template<typename T>inline void print(T x)
{
    if(x<0)
        putchar('-'),x*=-1;
    if(x>=10)
        print(x/10);
    putchar(x%10+'0');
}*/
const int N=5e4+5;
const int INF=0x3f3f3f3f;
int n,m;
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
struct node
{
    int maxn,minn;
}tr[N<<2];
void pushup(int x)
{
    tr[x].minn=min(tr[x<<1].minn,tr[x<<1|1].minn);
    tr[x].maxn=max(tr[x<<1].maxn,tr[x<<1|1].maxn);
}
void build(int x,int l,int r)
{
    if(l==r)
    {
        tr[x].maxn=0;
        tr[x].minn=n+1;
        return ;
    }
    int mid=(l+r)/2;
    build(x<<1,l,mid);
    build(x<<1|1,mid+1,r);
    pushup(x);
}
void update(int L,int x,int y,int l,int r,int rt)
{
    if(l==r)
    {
        tr[rt].minn=y;
        tr[rt].maxn=x;
        return ;
    }
    int mid=(l+r)>>1;
    if(L<=mid)
        update(L,x,y,l,mid,rt<<1);
    else
        update(L,x,y,mid+1,r,rt<<1|1);
    pushup(rt);
}
/*void update_max(int L,int k,int l,int r,int rt){
    if(l==r){
        tr[rt].maxn=k;
        return;
    }
    int mid=(l+r)/2;
    if(L<=mid) update_max(L,k,lson);
    else update_max(L,k,rson);
    tr[rt].maxn=max(tr[rt<<1].maxn,tr[rt<<1|1].maxn);
}

void update_min(int L,int k,int l,int r,int rt){
    if(l==r){
        tr[rt].minn=k;
        return;
    }
    int mid=(l+r)/2;
    if(L<=mid) update_min(L,k,lson);
    else update_min(L,k,rson);
    tr[rt].minn=min(tr[rt<<1].minn,tr[rt<<1|1].minn);
}*/
int querymin(int L,int R,int l,int r,int rt)
{
    if(L<=l&&R>=r)
    {
        return tr[rt].minn;
    }
    int mid=(l+r)/2;
    int ans=INF;
    if(L<=mid) ans=min(ans,querymin(L,R,l,mid,rt<<1));
    if(R>mid)  ans=min(ans,querymin(L,R,mid+1,r,rt<<1|1));
    return ans;
}
int querymax(int L,int R,int l,int r,int rt)
{
    if(L<=l&&R>=r)
    {
        return tr[rt].maxn;
    }
    int mid=(l+r)/2;
    int ans=0;
    if(L<=mid) ans=max(ans,querymax(L,R,l,mid,rt<<1));
    if(R>mid)  ans=max(ans,querymax(L,R,mid+1,r,rt<<1|1));
    return ans;
}
int query_max(int L,int R,int l,int r,int rt){
    if(L<=l&&R>=r){
        return tr[rt].maxn;

    }
    int mid=(l+r)/2;
    int ans=0;
    if(L<=mid) ans=max(ans,query_max(L,R,lson));
    if(R>mid) ans=max(ans,query_max(L,R,rson));
    return ans;
}

int query_min(int L,int R,int l,int r,int rt){
    if(L<=l&&R>=r){
        return tr[rt].minn;
    }
    int mid=(l+r)/2;
    int ans=0x3f3f3f3f;
    if(L<=mid) ans=min(ans,query_min(L,R,lson));
    if(R>mid) ans=min(ans,query_min(L,R,rson));
    return ans;
}

int main()
{
    std::ios::sync_with_stdio(false);
    while(cin>>n>>m)
    {
        build(1,1,n);
        char ch;
        int t;
        stack<int>s;
        while(m--)
        {
            cin>>ch;
            if(ch=='D')
            {
                cin>>t;
                //update_max(t,t,1,n,1);
               // update_min(t,t,1,n,1);
               update(t,t,t,1,n,1);
                s.push(t);
            }
            else if(ch=='Q')
            {
                cin>>t;
                int qx,qm;
                qm=querymin(t,n,1,n,1);
                qx=querymax(1,t,1,n,1);
                cout<<max(qm-qx-1,0)<<endl;
            }
            else if(ch=='R')
            {
                t=s.top();
                s.pop();
               // update_max(t,0,1,n,1);
                //update_min(t,n+1,1,n,1);
                update(t,0,n+1,1,n,1);
            }
        }
    }
    return 0;
}

思路

(1)单点更新,区间查询最值。

初始各点的最小值为n+1,最大值为0;

查找该点右边的最小值,左边的最大值。

(2) 区间合并(emmm....待补充)

posted @ 2019-12-01 18:49  Anticlock  阅读(250)  评论(0编辑  收藏  举报