HDU1540: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

https://blog.csdn.net/chudongfang2015/article/details/52133243

这篇博客讲的非常详细

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <algorithm>
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;
#define M 50015
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
typedef struct SegTreeNode
{
    int maxx;
    int minn;
} segtree;  //线段树结构体,一个用来求最大值,一个用来求最小值

segtree arr[M<<2];
int n;
int history[M<<2];//保存历史,用于R

void build(int l,int r,int rt)//建树
{
    if(l == r)
    {
        arr[rt].minn = n+1;
        arr[rt].maxx = 0;
        return;
    }

    int m = (l+r)>>1;
    build(lson);
    build(rson);
    arr[rt].maxx = max(arr[rt<<1].maxx,arr[rt<<1|1].maxx);
    arr[rt].minn = min(arr[rt<<1].minn,arr[rt<<1|1].minn);
}

void update_maxx(int p,int sc,int l,int r,int rt) //更新求最大值的线段树
{
    if(l == r)
    {
        arr[rt].maxx = sc;
        return ;
    }
    int m = (l+r)>>1;

    if(p<=m)
        update_maxx(p,sc,lson);
    else
        update_maxx(p,sc,rson);

    arr[rt].maxx = max(arr[rt<<1].maxx,arr[rt<<1|1].maxx);
}

void update_minn(int p,int sc,int l,int r,int rt) //更新求最小值的线段树
{
    if(l == r)
    {
        arr[rt].minn = sc;//p和sc在破坏时其实是一个值
        return ;
    }
    int m = (l+r)>>1;//

    if(p<=m)
        update_minn(p,sc,lson);
    else
        update_minn(p,sc,rson);

    arr[rt].minn = min(arr[rt<<1].minn,arr[rt<<1|1].minn);//更新最小值
}

int query_minn(int L,int R,int l,int r,int rt)    //查找区间最小值
{
    if(L<=l && r<=R)
    {
        return arr[rt].minn;
    }
    int m = (l + r) >>1;
    int ret = INF;
    if(L<=m)
        ret = min(ret,query_minn(L,R,lson));
    if(R>m)
        ret = min(ret,query_minn(L,R,rson));
    return ret;
}


int query_maxx(int L,int R,int l,int r,int rt)  //查找区间最大值
{
    if(L<=l && r<=R)
    {
        return arr[rt].maxx;
    }
    int m = (l + r) >>1;
    int ret = 0;
    if(L<=m)
        ret = max(ret,query_maxx(L,R,lson));
    if(R>m)
        ret = max(ret,query_maxx(L,R,rson));
    return ret;
}

int main(int argc, char const *argv[])
{
    int m,a,temp,count;
    char ch[10];
    while(scanf("%d %d",&n,&m) == 2)
    {
        count = 0;  //历史计算器
        memset(history,0,sizeof(history));
        memset(arr,0,sizeof(arr));
        build(1,n,1);
        while(m--)
        {
            scanf("%s",ch);
            if(ch[0] == 'D')
            {
                scanf("%d",&a);
                update_minn(a,a,1,n,1);//更新线段数的值,把a对应的值更新成a
                update_maxx(a,a,1,n,1);
                history[++count] = a;  //存储到历史记录中
            }
            else if(ch[0] == 'Q')
            {
                int re,max1,min1;
                scanf("%d",&a);
                max1=query_maxx(1,a,1,n,1);//根据线段树查询
                min1=query_minn(a,n,1,n,1);
                if(max1 == min1)         //考虑特殊情况
                    printf("%d\n",0);
                else
                    printf("%d\n",min1-max1-1);
            }
            else
            {
                temp = history[count--];
                update_minn(temp,n+1,1,n,1); //如果恢复,就把对应的值改为0或n+1
                update_maxx(temp,0,1,n,1);
            }
        }

    }
    return 0;
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值