HDU 1540 Tunnel Warfare (线段树)

Tunnel Warfare

Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 7442 Accepted Submission(s): 2901

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

Source
POJ Monthly

Recommend
LL | We have carefully selected several similar problems for you: 1828 2871 3016 1823 3397

线段树单点更新。节点维护左端点向右,右端点向左的最长连续区间,还有一个最长段内最长区间。

#include "cstring"
#include "iostream"
#include "string.h"
#include "cstdio"
#include "algorithm"
#include "stack"
#define MAX 50005
using namespace std;
struct node
{
    int left,right;
    int llen,rlen,maxlen;
}tree[MAX*4];
int n;
stack<int> list;
void build(int root,int lf,int rt)
{
    tree[root].left=lf;
    tree[root].right=rt;
    tree[root].llen=tree[root].rlen=tree[root].maxlen=rt-lf+1;
    if(lf==rt)
        return;
    int mid=(lf+rt)/2;
    build(root*2,lf,mid);
    build(root*2+1,mid+1,rt);
}
void destroy(int root,int target,int flag)
{
    if(tree[root].left==tree[root].right)
    {
        if(flag==1)//干掉
        {
            tree[root].llen=tree[root].rlen=tree[root].maxlen=0;
            return;
        }
        else
        {
            //恢复
            tree[root].llen=tree[root].rlen=tree[root].maxlen=1;
            return;
        }
    }
    int mid=(tree[root].left+tree[root].right)/2;
    if(target>=mid+1)
    {
        destroy(root*2+1,target,flag);
    }
    else
    {
        destroy(root*2,target,flag);
    }
    tree[root].llen=tree[root*2].llen;
    tree[root].rlen=tree[root*2+1].rlen;
    tree[root].maxlen=max(max(tree[root*2].maxlen,tree[root*2+1].maxlen),tree[root*2].rlen+tree[root*2+1].llen);
    if(tree[root*2].maxlen==tree[root*2].right-tree[root*2].left+1)
    {
        tree[root].llen=tree[root*2].maxlen+tree[root*2+1].llen;
    }
    if(tree[root*2+1].maxlen==tree[root*2+1].right-tree[root*2+1].left+1)
    {
        tree[root].rlen=tree[root*2+1].maxlen+tree[root*2].rlen;
    }
    return;
}
int cnt(int root,int target)
{
    if(tree[root].left==tree[root].right||tree[root].maxlen==0)
    {
        return tree[root].maxlen;
    }
    if(tree[root].maxlen==tree[root].right-tree[root].left+1)
    {
        return tree[root].maxlen;
    }
    int mid=(tree[root].left+tree[root].right)/2;
    if(target>=mid+1)
    {
        if(target<=tree[2*root+1].left+tree[2*root+1].llen-1)
        {
            return cnt(root*2+1,target)+cnt(root*2,mid);
        }
        else
        {
            return cnt(root*2+1,target);
        }

    }
    else
    {
        if(target>=tree[2*root].right-tree[2*root].rlen+1)
        {
            return cnt(root*2,target)+cnt(root*2+1,mid+1);
        }
        else
        {
            return cnt(root*2,target);
        }
    }
}
int main()
{
    int m;
    while(~scanf("%d%d",&n,&m))
    {
        while(!list.empty())
            list.pop();
        build(1, 1, n);
        while(m--)
        {
            char op[20];
            scanf("%s",op);
            if(op[0]=='D')
            {
                int x;
                scanf("%d",&x);
                destroy(1,x,1);
                list.push(x);
            }
            if(op[0]=='Q')
            {
                int x;
                scanf("%d",&x);
                printf("%d\n",cnt(1,x));
            }
            if(op[0]=='R')
            {
                int now=list.top();
                list.pop();
                destroy(1,now,0);
            }
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值