HDOJ 1540 Tunnel Warfare (线段树之区间合并)


Tunnel Warfare

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


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


题意:给定n个排成一行的村庄(编号从1到n)和m次操作。

D x 表示破坏x村庄,Q x 表示查询x村庄所能联络的村庄数,R 表示重建最后一个被破坏的村庄,求单点所在连续村庄的最大长度

思路:分为两种情况:(1).区间内的连续长度 (2).  左区间右连续+右区间左连续


#include <bits/stdc++.h>
using namespace std;
#define mst(a,b) memset((a),(b),sizeof(a))
#define f(i,a,b) for(int i=(a);i<=(b);++i)
const int maxn =50005;
const int mod = 1e9+7;
const int INF = 1e9;
//#define m ((l+r)>>1)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define ll long long
#define rush() int T;scanf("%d",&T);while(T--)
struct node
{
    int l,r,len;
    int lsum,rsum,sum;
}tree[maxn<<2];
void pushup(int rt)
{
    tree[rt].lsum=tree[rt<<1].lsum;
    tree[rt].rsum=tree[rt<<1|1].rsum;
    tree[rt].sum=max(tree[rt<<1].sum,tree[rt<<1|1].sum);
    tree[rt].sum=max(tree[rt].sum,tree[rt<<1|1].lsum+tree[rt<<1].rsum);
    if(tree[rt].lsum==tree[rt<<1].len)
    {
        tree[rt].lsum+=tree[rt<<1|1].lsum;
    }
    if(tree[rt].rsum==tree[rt<<1|1].len)
    {
        tree[rt].rsum+=tree[rt<<1].rsum;
    }
}
void build(int l,int r,int rt)
{
    tree[rt].l=l;
    tree[rt].r=r;
    tree[rt].len=tree[rt].lsum=tree[rt].rsum=tree[rt].sum=r-l+1;
    if(l==r)
    {
        return;
    }
    int m=(l+r)>>1;
    build(lson);
    build(rson);
}
void update(int pos,int n,int rt)
{
    if(tree[rt].l==tree[rt].r)
    {
        tree[rt].lsum=tree[rt].rsum=tree[rt].sum=n;
        return;
    }
    int m=(tree[rt].l+tree[rt].r)>>1;
    if(pos<=m)
        update(pos,n,rt<<1);
    else update(pos,n,rt<<1|1);
    pushup(rt);
}
int query(int pos,int rt)
{
    if(tree[rt].l==tree[rt].r)
        return tree[rt].sum;
    int m=(tree[rt].l+tree[rt].r)>>1;
    if(pos<=m)
    {
        int ans1=query(pos,rt<<1),ans2=0;
        if(pos>=tree[rt<<1].r-tree[rt<<1].rsum+1)
            ans2=tree[rt<<1].rsum+tree[rt<<1|1].lsum;
        return max(ans1,ans2);
    }
    else
    {
        int ans1=query(pos,rt<<1|1),ans2=0;
        if(pos<=tree[rt<<1|1].l+tree[rt<<1|1].lsum-1)
            ans2=tree[rt<<1].rsum+tree[rt<<1|1].lsum;
        return max(ans1,ans2);
    }
}
int main()
{
    int n,m,x;
    char str[2];
    while(~scanf("%d%d",&n,&m))
    {
        build(1,n,1);
        stack<int>s;
        while(m--)
        {
            scanf("%s",str);
            if(str[0]=='R')
            {
                if(s.size())
                {
                    x=s.top();
                    s.pop();
                    update(x,1,1);
                }
            }
            else
            {
                scanf("%d",&x);
                if(str[0]=='D')
                {
                    update(x,0,1);
                    s.push(x);
                }
                else if(str[0]=='Q')
                {
                    int ans=query(x,1);
                    printf("%d\n",ans);
                }
            }
        }
    }
    return 0;
}

(if判断语句尾加了个分号,导致错误,查错查了一个多小时,引以为戒。。。)





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值