Tunnel Warfare HDU - 1540

 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

题意:D代表破坏村庄,R代表修复最后被破坏的那个村庄,Q代表询问包括x在内的最大连续区间是多少?线段树区间维护,lsum 代表区间左端点开始的最大连续值, rsum 代表区间右端点开始的最大连续值,msum 代表整个区间的最大连续值。要求从左往右区间和连续的最大值,和右往左区间和连续的最大值。

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define maxn 50010
int s[maxn],top;
struct node
{
    int l,r;
    int lsum,rsum,msum;
}Tree[maxn*4];
int max(int a,int b)
{
    return a > b ? a : b;
}
void PushUp(int rt)
{
    Tree[rt].lsum=Tree[rt*2].lsum;
    Tree[rt].rsum=Tree[rt*2+1].rsum;
    Tree[rt].msum=max(max(Tree[rt*2].msum,Tree[rt*2+1].msum),Tree[rt*2].rsum+Tree[rt*2+1].lsum);
    if(Tree[rt*2].lsum==Tree[rt*2].r-Tree[rt*2].l+1)
    Tree[rt].lsum+=Tree[rt*2+1].lsum;
    if(Tree[rt*2+1].rsum==Tree[rt*2+1].r-Tree[rt*2+1].l+1)
    Tree[rt].rsum+=Tree[rt*2].rsum;
}
void Build(int l,int r,int rt)
{
    Tree[rt].l=l;
    Tree[rt].r=r;
    Tree[rt].lsum=Tree[rt].rsum=Tree[rt].msum=r-l+1;
    if(l==r)
    return ;
    int m=(l+r)/2;
    Build(l,m,rt*2);
    Build(m+1,r,rt*2+1);
}
void update(int pos,int val,int rt)
{
    if(Tree[rt].l==Tree[rt].r)
    {
        if(val==1)
        Tree[rt].lsum=Tree[rt].rsum=Tree[rt].msum=1;
        else
        Tree[rt].lsum=Tree[rt].rsum=Tree[rt].msum=0;
        return ;
    }
    int mid=(Tree[rt].l+Tree[rt].r)/2;
    if(pos<=mid)
    update(pos,val,rt*2);
    else
    update(pos,val,rt*2+1);
    PushUp(rt);
}

int query(int rt,int pos)
{
    if(Tree[rt].l==Tree[rt].r||Tree[rt].msum==0||Tree[rt].msum==Tree[rt].r-Tree[rt].l+1)
    return Tree[rt].msum;
    int mid=(Tree[rt].l+Tree[rt].r)/2;
    if(pos<=mid)
    {
        if(pos>=Tree[rt*2].r-Tree[rt*2].rsum+1)
            return query(rt*2,pos)+query(rt*2+1,mid+1);
        else
            return query(rt*2,pos);
    }
    else
    {
        if(pos<=Tree[rt*2+1].l+Tree[rt*2+1].lsum-1)
            return query(rt*2+1,pos)+query(rt*2,mid);
        else
            return query(rt*2+1,pos);
    }
}
int main()
{
    int n,q,x;
    char op[3];
    while(~scanf("%d%d",&n,&q))
    {
        top=0;
        Build(1,n,1);
        for(int i=1; i<=q; i++)
        {
            scanf("%s",op);
            if(op[0]=='D')
            {
                scanf("%d",&x);
                s[top++]=x;
                update(x,0,1);
            }
            else if(op[0]=='Q')
            {
                scanf("%d",&x);
                printf("%d\n",query(1,x));
            }
            else
            {
                if(x>0){
                    x=s[--top];
                    update(x,1,1);
                }
            }
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值