Tunnel Warfare(线段树区间合并问题)

Tunnel Warfare
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 10310 Accepted Submission(s): 4047

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

这道题就是一个区间合并的问题 前几天刚做了一个区间内求一个最大连续区间题 这道题就是那道题的变形 这里有一个大坑就是题目中并没有说明是一组样例!!!!!!!

#include<iostream>
#include<string.h>
#include<algorithm>
#include<stdio.h>
#include <stack>

using namespace std;

#define MAXN 50007 * 4

struct Node
{
    int left,right;
    int ls,rs,ans;//ls 是代表以这个节点为开始的最大连续序列的个数 rs 是代表以这个节点未结束的最大连续序列的个数 ans 为代表区间 l,r 最大连续的个数
}node[MAXN];
int n,m;
void build(int l,int r,int rt)
{
    node[rt].left = l;
    node[rt].right = r;
    if(l==r)
    {
        node[rt].ans = node[rt].ls = node[rt].rs = 1;
        return;
    }
    int mid = (l+r)/2;
    build(l,mid,rt*2);
    build(mid+1,r,rt*2+1);
    node[rt].ls = node[rt*2].ls;
    if(node[rt*2].ans == (node[rt*2].right - node[rt*2].left +1))//如果左子树都连续 那么此时需要在加上右子树的ls
        node[rt].ls +=node[rt*2+1].ls;
    node[rt].rs = node[rt*2+1].rs;
    if(node[rt*2+1].ans == (node[rt*2+1].right - node[rt*2+1].left + 1))//同理可以得出rs
        node[rt].rs += node[rt*2].rs;
    //分为三种情况 第一种情况就是取左子树的最大连续序列和 第二种情况就是取右子树的最大连续序列和
    //第三种情况就是取左子树的右区间和 + 右子树的左区间和
    node[rt].ans = max(node[rt*2].ans ,node[rt*2+1].ans);
    node[rt].ans = max(node[rt].ans,node[rt*2].rs + node[rt*2+1].ls);
}
void update(int rt,int indx,int c)
{
    if(indx == node[rt].left && node[rt].right == node[rt].left)
    {
        node[rt].ans = node[rt].ls = node[rt].rs = c;
        return;
    }
    int mid = (node[rt].left + node[rt].right)/2;
    if(indx<= mid)
    {
        update(rt*2,indx,c);
    }
    else
        update(rt*2+1,indx,c);
    //更新父节点
    node[rt].ls = node[rt*2].ls;
    if(node[rt*2].ans == (node[rt*2].right - node[rt*2].left +1))//如果左子树都连续 那么此时需要在加上右子树的ls
        node[rt].ls +=node[rt*2+1].ls;
    node[rt].rs = node[rt*2+1].rs;
    if(node[rt*2+1].ans == (node[rt*2+1].right - node[rt*2+1].left + 1))//同理可以得出rs
        node[rt].rs += node[rt*2].rs;
    //分为三种情况 第一种情况就是取左子树的最大连续序列和 第二种情况就是取右子树的最大连续序列和
    //第三种情况就是取左子树的右区间和 + 右子树的左区间和
    node[rt].ans = max(node[rt*2].ans ,node[rt*2+1].ans);
    node[rt].ans = max(node[rt].ans,node[rt*2].rs + node[rt*2+1].ls);
}
int query(int rt,int num)
{
    if(node[rt].left == num && num == node[rt].right|| node[rt].ans == (node[rt].right - node[rt].left +1))
        return node[rt].ans;
    int mid = (node[rt].left + node[rt].right)/2;
    if(num<= mid)//查询左子树
    {
        if(num >= node[rt*2].right - node[rt*2].rs + 1)//此时需要加上右子树的左端点的ans 因为 就是当前节点在right - rs +1 的范围内 举个例子就是 如果要查询2 而rs = 2 r = 3 n = 5 待查询的2 就在这个右连续区间
                return query(rt*2,num) + query(rt*2+1,mid+1);
        else
            return query(rt*2,num);
    }
    else
    {
        if(num <= node[rt*2+1].left + node[rt*2+1].ls -1)//如果num位于右子树左端点的左连续区间内 那么此时就需要加上 左子树的右端点的 ans 
        {
            return query(rt*2+1,num) + query(rt*2,mid);
        }
        else
            return query(rt*2+1,num);
    }
}
int main()
{

    while(scanf("%d%d",&n,&m)!=EOF)
    {
        build(1,n,1);
        stack<int> pp;
        while(m--)
        {
            char op[2];
            int num;
            scanf("%s",op);
            if(op[0]=='D')
            {
                scanf("%d",&num);
                update(1,num,0);
                pp.push(num);
            }
            else if(op[0]=='Q')
            {
                scanf("%d",&num);
                printf("%d\n",query(1,num));
            }
            else
            {
                if(pp.size())
                {
                    update(1,pp.top(),1);
                    pp.pop();
                }
            }
        }
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值