Tunnel Warfare----hdu1540线段树

题目链接

题意:有n个村庄,编号分别为1-n;由于战争会破坏村庄,但是我们也会修复;

D x代表村庄x被破坏;

Q x是求与x相连的有几个没有被破坏;

R 是修复最后一次被破坏的村庄;

接下来有m个操作,对于每次Q操作输出结果;

由于修复的是最后一次被破坏的所以要用stack

接下来看代码吧,加个图好理解一点

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h>
#include<stack>
#include<iostream>
#define INF 0xfffffff
#define N 50100
using namespace std;

#define Lson r<<1
#define Rson r<<1|1

struct SegTree
{
    int L, R;
    int s,ls,rs;
    //s代表L--R中最大连续个数,ls代表L开始所能没有破坏的连续个数;同理rs;
    int mid()
    {
        return (L+R)>>1;
    }
    int len()
    {
        return R-L+1;
    }
}a[N*4];

void Update(int r, int x, int op)
{
    if(a[r].L == a[r].R)
    {
        a[r].ls = a[r].rs = a[r].s = op;
        return;
    }
    if(x<=a[r].mid())
        Update(Lson, x, op);
    else
        Update(Rson, x, op);

    a[r].ls = a[Lson].ls;
    a[r].rs = a[Rson].rs;

    if(a[Lson].ls == a[Lson].len())
        a[r].ls = a[Lson].ls + a[Rson].ls;
    if(a[Rson].rs == a[Rson].len())
        a[r].rs = a[Rson].rs + a[Lson].rs;

    a[r].s = max(a[Lson].rs + a[Rson].ls, max(a[Lson].ls, a[Rson].rs));
}

void BuildSegTree(int r, int L, int R)
{
    a[r].L = L, a[r].R = R;
    a[r].ls = a[r].rs = a[r].s = a[r].len();
    if(L == R)return;
    BuildSegTree(Lson, L, a[r].mid());
    BuildSegTree(Rson, a[r].mid()+1, R);
}
int Query(int r, int x)
{
    if(a[r].s == 0)
        return 0;
    if(x < a[r].L + a[r].ls)//判断是否在左边
        return a[r].ls;
    if(x > a[r].R - a[r].rs)//判断是否在右边
        return a[r].rs;
    if(x > a[Lson].R - a[Lson].rs && x < a[Rson].L+a[Rson].ls)
        return a[Lson].rs + a[Rson].ls;

    if(x <= a[r].mid())
        return Query(Lson, x);
    else
        return Query(Rson, x);
}
int main()
{
    int n, m, x;
    char str[10];
    stack<int>Q;
    while(scanf("%d%d", &n, &m) != EOF)
    {
        BuildSegTree(1, 1, n);
        while(m--)
        {
            scanf("%s", str);
            if(str[0] == ‘D‘)
            {
                scanf("%d", &x);
                Update(1, x, 0);
                Q.push(x);
            }
            else if(str[0] == ‘Q‘)
            {
                scanf("%d", &x);
                printf("%d\n", Query(1, x));
            }
            else
            {
                Update(1, Q.top(), 1);
                Q.pop();
            }
        }
    }
    return 0;
}

时间: 2024-10-13 17:51:22

Tunnel Warfare----hdu1540线段树的相关文章

hdu 1540 Tunnel Warfare【线段树】

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1540 题目大意:抗日战争时期,各村庄被一条地道连接着(村庄排在一条线上),有三种操作: 第一种:某村庄被敌军摧毁: 第二种:修复上一个被摧毁的村庄: 第三种:查询与该村庄直接或间接链接的村庄有多少个(包括自己): 此题用线段树做,每个节点包含该区间从左端开始有多大连续区间ls,从右端向左有多大连续区间rs,该区间的最大连续区间mas: 代码如下: #include <iostream> #incl

hdu--1540 Tunnel Warfare(线段树+区间合并)

Description 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

hdu 1540/POJ 2892 Tunnel Warfare 【线段树区间合并】

Tunnel Warfare                                                             Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) 链接:hdu 1540        POJ 2892 Problem Description During the War of Resistance Against Japan,

POJ 2892 Tunnel Warfare(线段树单点更新区间合并)

Tunnel Warfare Time Limit: 1000MS   Memory Limit: 131072K Total Submissions: 7876   Accepted: 3259 Description During the War of Resistance Against Japan, tunnel warfare was carried out extensively in the vast areas of north China Plain. Generally sp

poj 2892 Tunnel Warfare(线段树)

Tunnel Warfare Time Limit: 1000MS   Memory Limit: 131072K Total Submissions: 7499   Accepted: 3096 Description During the War of Resistance Against Japan, tunnel warfare was carried out extensively in the vast areas of north China Plain. Generally sp

HDU 1540 Tunnel Warfare(线段树,单点更新,区间查询)

Problem Description 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 vill

HDU 1540 Tunnel Warfare 平衡树 / 线段树:单点更新,区间合并

Tunnel Warfare                                  Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Problem Description During the War of Resistance Against Japan, tunnel warfare was carried out extensively in the vast

POJ 题目2892 Tunnel Warfare(线段树单点更新查询,求单点所在最大连续区间长度)

Tunnel Warfare Time Limit: 1000MS   Memory Limit: 131072K Total Submissions: 7307   Accepted: 2997 Description During the War of Resistance Against Japan, tunnel warfare was carried out extensively in the vast areas of north China Plain. Generally sp

[HDOJ1540]Tunnel Warfare(线段树)

题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=1540 哈哈,终于过了卡了很久的线段树.每个节点维护左边的最长,右边的最长和当前节点往下的最值. 更新每个节点的左右最值的时候要判断一下这个点的左右儿子的区间里有没有切断的点,如果没有那么就将整条的长度更新上去,否则就直接更新为左儿子(右儿子)的左最值(右最值).以及剪枝,当当前节点最大值为0或和它的节点数相同的时候,则直接可以返回最值. 1 #include <algorithm> 2

HDU1540(线段树统计连续长度)

---恢复内容开始--- Tunnel Warfare Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Description During the War of Resistance Against Japan, tunnel warfare was carried out extensively in the vast areas of north China Plain. Genera