poj 2892 hdu 1540Tunnel Warfare(poj 线段树 区间合并)

Tunnel Warfare
Time Limit: 1000MS Memory Limit: 131072K
Total Submissions: 8021 Accepted: 3325

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

Hint

An illustration of the sample input:

      OOOOOOO

D 3   OOXOOOO

D 6   OOXOOXO

D 5   OOXOXXO

R     OOXOOXO

R     OOXOOOO
#include <stdio.h>
#include <algorithm>
#include <stack>
#define size 55000
#define lson l, m, rt << 1
#define rson m+1, r, rt<<1|1
using namespace std;
int lmax[size<<2], rmax[size<<2];//从左端点连续,从右端点连续
void pushup(int rt, int len)
{
    lmax[rt] = lmax[rt << 1];//先让左连续等于左孩子的左连续
    if(lmax[rt] == (len - (len >> 1))) lmax[rt] += lmax[rt << 1 | 1];//如果左孩子是满的即为连续的,则最长连续为左边的连续加上右孩子的左连续
    rmax[rt] = rmax[rt << 1|1];
    if(rmax[rt] == (len >> 1)) rmax[rt] += rmax[rt<<1];
}
void build(int l, int r, int rt)
{
    if( l == r )
    {
        lmax[rt] = rmax[rt] = 1;return;
    }
    int m = ( l + r) >> 1;
    build(lson);
    build(rson);
    pushup(rt, r - l + 1);

}
void updata( int p, int v, int l, int r, int rt)
{
    if( l == r)
    {
        lmax[rt] = rmax[rt] = v;return;
    }
    int m = ( l + r) >>1;
    if( p <= m) updata(p, v, lson);
    else updata(p, v, rson);
    pushup(rt, r-l+1);
}
int qurey_pre(int L, int R, int l, int r, int rt)
{
    if( L <= l && r <= R)
        return lmax[rt];
    int m = ( l + r) >> 1;
    int lans = 0, rans = 0;
    if(L <= m)  lans = qurey_pre(L, R, lson);//
    if(R > m)   rans = qurey_pre(L, R, rson);
    if(R <= m) return lans;//查询的区间可分为三类,一类位于现在所处区间的左孩子
    if(L > m)  return rans;//另一类位于右孩子
    if(lans == m - L + 1) return lans + rans;//第三类较复杂,即为横跨左右孩子,这时候又分为两种情况  第一种为  左孩子是连续的 即此时最长的连续还应该加上右孩子的左连续
    return lans;//第二类 左孩子不是连续的 直接返回左孩子的左连续
}
int qurey_suf(int L, int R, int l, int r, int rt)
{
    if( L <= l && r <= R)
    {
        return rmax[rt];
    }
    int m = ( l + r) >> 1;   int lans = 0, rans = 0;
    if( L <= m)  lans = qurey_suf(L, R, lson);
    if( R > m)    rans = qurey_suf(L, R, rson);
    if( R <= m) return lans;
    if( L > m)  return rans;
    if( rans == R - m) return rans + lans;
    return rans;
}
int main()
{
    int n,m;
    char ch[2];
    int a;
    while(~scanf("%d%d",&n,&m))
    {
        stack<int>s;
        build(1, n, 1);

        while(m--)
        {
            scanf("%s",ch);
            if(ch[0] == 'D')
            {
                scanf("%d", &a);s.push(a);
                updata(a, 0, 1, n, 1);
            }
            else if(ch[0] == 'R')
            {
                if(s.empty()) continue;
                int x = s.top();  //printf("%d\n",x);
                s.pop();
                updata(x,1,1,n,1);


            }
            else if(ch[0] == 'Q')
            {   int x;
                scanf("%d", &x);
                int LL = qurey_suf(1,x,1,n,1);
                int RR = qurey_pre(x,n,1,n,1);
                if( LL + RR == 0) printf("0\n");
                else printf("%d\n", LL+RR-1);
            }
           // for( int i = 1; i <= 20; i++)
          // printf("%d:%d--%d\n",i,lmax[i],rmax[i]);
//             for( int i = 1; i <= 20; i++)
//             printf("%d:%d--%d\n",i,lmax[i],rmax[i]);
        }
    }
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值