线段树-------I - 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

这个题意有点难懂,不就是求最大连续区间吗,维护一个最大值线段树和最小值线段树,还是不会做,看网上的解法。。。感觉比较巧妙,起始最大值树被初始化为0,最小值树被初始化成n + 1,首先如果村庄被摧毁的话,就单点更新对应节点,将最大值树和最小值树更新为对应的id号;如果恢复村庄的话,将最大值树的该点更新为0,最小值树的该点更新为n + 1;查询时,查询1到查询id的最大值MAX的查询id到n的最小值MIN,如果相等,说明该村被摧毁了,否则就存在连续区间,答案是MIN - MAX - 1.。。。。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<map>
#include<stack>
#include<cmath>
using namespace std;
typedef long long LL;
const int inf = 0x3f3f3f3f;
const double eps = 1e-8;
#define pb push_back
#define mp make_pair
#define fi first
#define se second
const int N = 1e5 + 5;

int tree1[N << 2];
int tree2[N << 2];
int n,m;

void build(int root,int l,int r)
{
    if(l == r){
        tree1[root] = 0;
        tree2[root] = n + 1;
        return ;
    }
    int mid = (l + r) / 2;
    build(root * 2 + 1,l,mid);
    build(root * 2 + 2,mid + 1,r);
    tree1[root] = max(tree1[root * 2 + 1],tree1[root * 2 + 2]);
    tree2[root] = min(tree2[root * 2 + 1],tree2[root * 2 + 2]);
}

void updateone(int root,int l,int r,int index,int Bool)
{
    if(l == r){
        if(l == index){
            if(Bool){
                tree1[root] = tree2[root] = l;
            }else{
                tree1[root] = 0;
                tree2[root] = n + 1;
            }
        }
        return ;
    }
    int mid = (l + r) / 2;
    if(index <= mid){
        updateone(root * 2 + 1,l,mid,index,Bool);
    }else{
        updateone(root * 2 + 2,mid + 1,r,index,Bool);
    }
    tree1[root] = max(tree1[root * 2 + 1],tree1[root * 2 + 2]);
    tree2[root] = min(tree2[root * 2 + 1],tree2[root * 2 + 2]);
}

int query1(int root,int l,int r,int ql,int qr)
{
    if(r < ql || l > qr){
        return 0;
    }
    if(l >= ql && r <= qr){
        return tree1[root];
    }
    int mid = (l + r) / 2;
    return max(query1(root * 2 + 1,l,mid,ql,qr),query1(root * 2 + 2,mid + 1,r,ql,qr));
}

int query2(int root,int l,int r,int ql,int qr)
{
    if(r < ql || l > qr){
        return inf;
    }
    if(l >= ql && r <= qr){
        return tree2[root];
    }
    int mid = (l + r) / 2;
    return min(query2(root * 2 + 1,l,mid,ql,qr),query2(root * 2 + 2,mid + 1,r,ql,qr));
}

int main()
{
    while(~scanf("%d %d",&n,&m))
    {
        build(1,1,n);
        stack<int>sta;
        for(int i = 0;i < m;++i)
        {
            char ch;
            getchar();
            scanf("%c",&ch);
            if(ch == 'D'){
                int x;scanf("%d",&x);
                sta.push(x);
                updateone(1,1,n,x,1);
            }else if(ch == 'Q'){
                int x;scanf("%d",&x);
                int MAX = query1(1,1,n,1,x);
                int MIN = query2(1,1,n,x,n);
                //cout << MAX << " " << MIN << endl;
                if(MIN != MAX)
                    printf("%d\n",MIN - MAX - 1);
                else
                    printf("0\n");
            }else{
                if(!sta.empty()){
                    updateone(1,1,n,sta.top(),0);
                    sta.pop();
                }
            }
        }
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值