POJ 2892 & HDU 1540 Tunnel Warfare (线段树)

Tunnel Warfare
Time Limit: 1000MSMemory Limit: 131072K
Total Submissions: 5977Accepted: 2436

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

  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
 
HDU的比较坑,破坏的可以再破坏,但只需修复一次。也就是如果有D1 D2 D3 D1,修复的顺序应该是1,3,2,而不是3,2,1。
此题跟POJ 3667类似,都是线段树区间合并的问题,只不过是属于单点更新,所以省去了pushDown。关键就是查询问题了,首先先判断该点属于当前节点的左孩子还是右孩子,如果在左孩子,则如果它在左孩子的右区间,答案就是左孩子的右区间+右孩子的左区间,否则查找从左孩子往下找。同理,如果它在当前节点的右孩子,则先判断是否在右孩子的左区间,如果是,则答案为左孩子的右区间+右孩子的左区间,否则从右孩子往下找。
下面给出POJ和HDU都能AC的版本:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <stack>
#define SIZE 50005
#define ls l,mid,rt<<1
#define rs mid+1,r,rt<<1|1

using namespace std;

int sum[SIZE<<2],lsum[SIZE<<2],rsum[SIZE<<2];
stack <int> sta;
bool vis[SIZE];

void pushUp(int rt,int itv)
{
    lsum[rt] = lsum[rt<<1];
    rsum[rt] = rsum[rt<<1|1];
    if(lsum[rt] == (itv - (itv>>1)))
        lsum[rt] += lsum[rt<<1|1];
    if(rsum[rt] == (itv>>1))
        rsum[rt] += rsum[rt<<1];
    sum[rt] = max(max(sum[rt<<1],sum[rt<<1|1]),rsum[rt<<1] + lsum[rt<<1|1]);
}

void build(int l,int r,int rt)
{
    sum[rt] = lsum[rt] = rsum[rt] = r - l + 1;
    if(l == r) return ;
    int mid = (l + r) >> 1;
    build(ls);
    build(rs);
}

void update(int l,int r,int rt,int p,int w)
{
    if(l == r)
    {
        sum[rt] = lsum[rt] = rsum[rt] = w;
        return;
    }
    int mid = (l + r) >> 1;
    if(p <= mid) update(ls,p,w);
    else update(rs,p,w);
    pushUp(rt,r-l+1);
}

int query(int l,int r,int rt,int p)
{
    if(sum[rt] == 0) return 0;
    int mid = (l + r) >> 1;
    if(p <= mid) //该点在左孩子
    {
        if(p >= mid - rsum[rt<<1]) //该点在左孩子的右区间
            return rsum[rt<<1] + lsum[rt<<1|1];
        else
            return query(ls,p);
    }
    if(p > mid) //该点在右孩子
    {
        if(p <= mid + lsum[rt<<1|1]) //该点在右孩子的左区间
            return rsum[rt<<1] + lsum[rt<<1|1];
        else
            return query(rs,p);
    }
}

int N,M;
char o;
int pos;

int main()
{
    while(~scanf("%d%d",&N,&M))
    {
        build(1,N,1);
        while(!sta.empty()) sta.pop();
        memset(vis,0,sizeof(vis));
        while(M--)
        {
            getchar();
            scanf("%c",&o);
            if(o == 'D')
            {
                scanf("%d",&pos);
                if(!vis[pos])
                    update(1,N,1,pos,0);
                vis[pos] = true;
                sta.push(pos);
            }
            else if(o == 'Q')
            {
                scanf("%d",&pos);
                if(vis[pos])
                {
                    printf("0\n");
                    continue;
                }
                printf("%d\n",query(1,N,1,pos));
            }
            else
            {
                while(!sta.empty())
                {
                    pos = sta.top();
                    sta.pop();
                    if(vis[pos])
                    {
                        update(1,N,1,pos,1);
                        vis[pos] = false;
                        break;
                    }
                }
            }
        }
    }
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值