POJ-2892-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 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 (nm ≤ 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:

  1. D x: The x-th village was destroyed.
  2. Q x: The Army commands requested the number of villages that x-th village was directly or indirectly connected with including itself.
  3. 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

Hint

An illustration of the sample input:

      OOOOOOO

D 3   OOXOOOO

D 6   OOXOOXO

D 5   OOXOXXO

R     OOXOOXO

R     OOXOOOO

Source

POJ Monthly--2006.07.30, updog

思路:线段树维护每个区间的左右两端分别到中间的最大连续区间的大小,注意左右子树可能存在最大连续区间大小就等于该节点的区间大小的情况。

#include <stdio.h>

struct{
int l,r;//分别记录左右两边开始的最大连续区间大小
}node[200000];

int stk[50000],top,n,m;

void build(int idx,int s,int e)
{
    node[idx].l=node[idx].r=e-s+1;

    if(s!=e)
    {
        int mid=(s+e)>>1;

        build(idx<<1,s,mid);
        build(idx<<1|1,mid+1,e);
    }
}

void update(int idx,int s,int e,int pos,int val)
{
    if(s==e) node[idx].l=node[idx].r=val;//val为0代表摧毁村庄,为1代表修复村庄
    else
    {
        int mid=(s+e)>>1;

        if(pos<=mid) update(idx<<1,s,mid,pos,val);
        else update(idx<<1|1,mid+1,e,pos,val);

        if(node[idx<<1].l==mid-s+1) node[idx].l=mid-s+1+node[idx<<1|1].l;//左子树满了
        else node[idx].l=node[idx<<1].l;

        if(node[idx<<1|1].r==e-mid) node[idx].r=e-mid+node[idx<<1].r;//右子树满了
        else node[idx].r=node[idx<<1|1].r;
    }
}

int query(int idx,int s,int e,int pos)
{
    if(s==e) return 0;//如果到了叶子节点就肯定为0

    int mid=(s+e)>>1;

    if(pos<=mid)
    {
        if(pos>=mid-node[idx<<1].r+1) return node[idx<<1].r+node[idx<<1|1].l;//如果被包含在左子树的右区间还要加上右子树的左区间
        else return query(idx<<1,s,mid,pos);
    }
    else
    {
        if(pos<=mid+node[idx<<1|1].l) return node[idx<<1|1].l+node[idx<<1].r;//如果被包含在右子树的左区间还要加上左子树的右区间
        else return query(idx<<1|1,mid+1,e,pos);
    }
}

int main()
{
    int t;
    char s[5];

    while(~scanf("%d%d",&n,&m))
    {
        build(1,1,n);

        top=0;

        while(m--)
        {
            scanf("%s",s);

            if(s[0]=='D')
            {
                scanf("%d",&t);

                stk[top++]=t;

                update(1,1,n,t,0);
            }
            else if(s[0]=='R')
            {
                update(1,1,n,stk[--top],1);
            }
            else
            {
                scanf("%d",&t);

                printf("%d\n",query(1,1,n,t));
            }
        }
    }
}

POJ-2892-Tunnel Warfare(线段树)

时间: 2024-08-28 15:24:51

POJ-2892-Tunnel Warfare(线段树)的相关文章

HDU 1540 &amp;&amp; POJ 2892 Tunnel Warfare (线段树,区间合并).

~~~~ 第一次遇到线段树合并的题,又被律爷教做人.TAT. ~~~~ 线段树的题意都很好理解吧.. 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1540 http://poj.org/problem?id=2892 ~~~~ 我的代码:200ms #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #defin

POJ 2892 Tunnel Warfare (树状数组+二分)

题目大意: 三个操作 D pos  将pos位置摧毁,让它和周围不相连. Q pos 问和pos 相连的有多少个村庄. R 修复最近摧毁的村庄. 思路分析: 树状数组记录这个区间有多少个1. 如果  [s-e] 有e-s+1个1 的话.那么这个区间是相连的. 这样的话,我们就可以用二分的办法求出与某个位置最大相连的数量. 还有这里二分 while(l<=r) { if(满足) { ans=mid; l=mid+1; } else r=mid-1; } #include <cstdio>

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

POJ 2892 Tunnel Warfare [树状数组]

题目链接: http://poj.org/problem?id=2892 题意:一个长度为n的线段,下面m个操作 D x 表示将单元x毁掉 R  表示修复最后毁坏的那个单元 Q x  询问这个单元以及它周围有多少个连续的单元,如果它本身已经被毁坏了就是0 思路: 这道题是经典的线段树入门题目,由于只是进行单点更新, 不涉及区间更新,用树状数组更简洁. 维护两个树状数组,一个是把所有的1进行维护,一个是把所有的0进行维护. 翻转(炸毁或修复)任何一个单元,同时修改这两个树状数组,仅仅是为了 合并 

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 con

hdu 1540 Tunnel Warfare 线段树 单点更新,查询区间长度,区间合并

Tunnel Warfare Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1540 Description During the War of Resistance Against Japan, tunnel warfare was carried out extensively in the vast areas of north China Plain. Gene

HDU 1540 Tunnel Warfare 线段树区间合并

Tunnel Warfare 题意:D代表破坏村庄,R代表修复最后被破坏的那个村庄,Q代表询问包括x在内的最大连续区间是多少 思路:一个节点的最大连续区间由(左儿子的最大的连续区间,右儿子的最大连续区间,左儿子的最大连续右区间+右儿子的最大连续左区间)决定 所以线段树的节点应该维护当前节点的最大连续左区间,最大连续右区间,和最大连续区间. 注意更新的时候,如果左儿子全满,父亲节点的左连续区间还要加上右儿子的左区间.反之同理. 查询的时候,可以剪枝,如果是叶子,或为空,或满,则不用往下查询. 查询

hdu 1540 Tunnel Warfare(线段树)

题目链接:hdu 1540 Tunnel Warfare 题目大意:有连续的N个城镇,三种操作: D x:第x城镇被破坏 Q x:插叙第x城镇所在联通块有多少个城镇没有被破坏 R:修复最后一个被破坏的城镇 解题思路:线段树区间合并,每个城镇看成一个叶子节点,用一个vector记录破坏顺序.对于查询来说,每次只要判断是否在mid?R[lson(u)],mid+L[rson(u)]之间即可,否则即递归查询左右子树. #include <cstdio> #include <cstring>