hdu 1540 线段树区间合并

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 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在内的最大连续区间是多少

代码:

#include <iostream>
#define maxn 50010
#include <stdio.h>
using namespace std;
struct Tree
{
    int ls,rs,ms,l,r;
}tree[maxn*4];
int n,m;
int s[maxn],top;
void build(int root,int l,int r)
{
    tree[root].l=l;
    tree[root].r=r;
    tree[root].ls=tree[root].rs=tree[root].ms=r-l+1;
    if(l!=r)
    {
        int mid=(tree[root].l+tree[root].r)>>1;
        build(root<<1,l,mid);
        build(root<<1|1,mid+1,r);
    }
}
void insert(int root,int t,int x)
{
    if(tree[root].l==tree[root].r)
    {
        if(x==1)
        tree[root].ls=tree[root].rs=tree[root].ms=1;
        else
        tree[root].ls=tree[root].rs=tree[root].ms=0;
        return;
    }
    int mid=(tree[root].l+tree[root].r)>>1;
    if(t<=mid)
        insert(root<<1,t,x);
    else
    insert(root<<1|1,t,x);
    tree[root].ls=tree[root<<1].ls;
    tree[root].rs=tree[root<<1|1].rs;
    tree[root].ms=max(max(tree[root<<1].ms,tree[root<<1|1].ms),tree[root<<1].rs+tree[root<<1|1].ls);
    if(tree[root<<1].ls==tree[root<<1].r-tree[root<<1].l+1)
    tree[root].ls+=tree[root<<1|1].ls;
    if(tree[root<<1|1].rs==tree[root<<1|1].r-tree[root<<1|1].l+1)
    tree[root].rs+=tree[root<<1].rs;
}
int query(int root,int t)
{
    if(tree[root].l==tree[root].r||tree[root].ms==0||tree[root].ms==tree[root].r-tree[root].l+1)
    return tree[root].ms;
    int mid=(tree[root].l+tree[root].r)>>1;
    if(t<=mid)
    {
        if(t>=tree[root<<1].r-tree[root<<1].rs+1)
        return query(root<<1,t)+query(root<<1|1,mid+1);
        else
        return query(root<<1,t);
    }
    else
    {
        if(t<=tree[root<<1|1].l+tree[root<<1|1].ls-1)
        return query(root<<1|1,t)+query(root<<1,mid);
        else
        return query(root<<1|1,t);
    }
}
int main()
{
    char c[2];
    int x;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        top=0;
        build(1,1,n);
        while(m--)
        {
            scanf("%s",c);
            if(c[0]=='D')
            {
                scanf("%d",&x);
                s[top++]=x;
                insert(1,x,0);
            }
            else if(c[0]=='Q')
            {
                scanf("%d",&x);
                printf("%d\n",query(1,x));
            }
            else
            {
                if(x>0)
                {
                    x=s[--top];
                    insert(1,x,1);
                }
            }
        }
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值