HDU - 1540 Tunnel Warfare 线段树+最值更新

题目:

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

思路:

线段树维护最大值和最小值。

建立的时候进行预处理。

代码如下:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <stack>
using namespace std;
const int maxn=5*1e4+5;
int ta[maxn<<2];
int tb[maxn<<2];
int n,m;
void push_up(int rt)
{
    ta[rt]=max(ta[rt<<1],ta[rt<<1|1]);
    tb[rt]=min(tb[rt<<1],tb[rt<<1|1]);
}
void build (int l,int r,int rt)
{
    if(l==r)
    {
        ta[rt]=0;
        tb[rt]=n+1;
        return;
    }
    int m=(l+r)>>1;
    build(l,m,rt<<1);
    build(m+1,r,rt<<1|1);
    push_up(rt);
}
void update (int d,int l,int r,int rt,int q)
{
    if(l==r)
    {
        if(q==0) tb[rt]=ta[rt]=d;
        else tb[rt]=n+1,ta[rt]=0;
        return;
    }
    int m=(l+r)>>1;
    if(m>=d) update (d,l,m,rt<<1,q);
    else update (d,m+1,r,rt<<1|1,q);
    push_up(rt);
}
int query_max(int L,int R,int l,int r,int rt)
{
    if(l>=L&&r<=R) return ta[rt];
    int m=(l+r)>>1;
    int ans=-1;
    if(m>=L) ans=max(ans,query_max(L,R,l,m,rt<<1));
    if(m<R) ans=max(ans,query_max(L,R,m+1,r,rt<<1|1));
    return ans;
}
int query_min(int L,int R,int l,int r,int rt)
{
    if(l>=L&&r<=R) return tb[rt];
    int m=(l+r)>>1;
    int ans=0x3f3f3f3f;
    if(m>=L) ans=min(ans,query_min(L,R,l,m,rt<<1));
    if(m<R) ans=min(ans,query_min(L,R,m+1,r,rt<<1|1));
    return ans;
}
int main()
{
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        build(1,n,1);
        stack<int>s;
        while(m--)
        {
            char c[2];
            scanf("%s",c);
            if(c[0]=='D')
            {
                int w;
                scanf("%d",&w);
                s.push(w);
                update (w,1,n,1,0);
            }
            if(c[0]=='Q')
            {
                int w;
                scanf("%d",&w);
                int Max=query_max(1,w,1,n,1);
                int Min=query_min(w,n,1,n,1);
                if(Max==Min) printf("0\n");
                else printf("%d\n",Min-1-Max);
            }
            if(c[0]=='R')
            {
                if(s.empty()) continue;
                int to=s.top(); s.pop();
                update (to,1,n,1,1);
            }
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_41410799/article/details/88614572