Tunnel Warfare【线段树区间合并】

Tunnel Warfare

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


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
 

Source
 

Recommend
LL
 

思路:

ls:区间左端点连续的个数

rs:区间右端点连续的个数

ms:区间最大连续的个数

可知:左子树的rs与右子树的ls可以合并为当前子树内一段连续的区间。

左子树的ls为当前子树的ls,右子树的rs为当前子树的rs。

如果左子树全部连续,当前ls需要左子树的节点数+右子树的ls。右子树亦然。

查询时如果当前区间全部连续或为空,则可以直接返回。

否则需要查找q所在的区间(必须在一个区间内部,不能在左连续或右连续,这样才能计算其所在连续区间的大小)。这样只需要找到一个区间,q在其左子树的右连续,或在其右子树的左连续,那么直接返回左子树的rs+右子树的ls即可。

代码:

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<stack>
using namespace std;
#define inf 0x3f3f3f3f
#define MAXN 50005
#define ll long long
int n,m;
stack<int> st;
struct node
{
    int ls,rs,ms;
}p[MAXN*4];
void build(int v,int L,int R)
{
    if(L==R)
    {
        p[v].ls=p[v].rs=p[v].ms=1;
        return;
    }
    int mid=(L+R)/2;
    build(v*2,L,mid);
    build(v*2+1,mid+1,R);
    p[v].ls=p[v].rs=p[v].ms=R-L+1;
}
void updata(int v,int L,int R,int q,int d)
{
    if(L==R)
    {
        p[v].ls=p[v].rs=p[v].ms=d;
        return;
    }
    int mid=(L+R)/2;
    if(q<=mid) updata(v*2,L,mid,q,d);
    else updata(v*2+1,mid+1,R,q,d);
    if(p[v*2].ls==mid-L+1)//若左子树全部连续
        p[v].ls=p[v*2].ls+p[v*2+1].ls;//那么左连续个数为左子树的节点数加上右子树的左连续
    else
        p[v].ls=p[v*2].ls;//否则为左子树的左连续
    if(p[v*2+1].rs==R-mid)
        p[v].rs=p[v*2].rs+p[v*2+1].rs;
    else
        p[v].rs=p[v*2+1].rs;
    p[v].ms=max(max(p[v*2].ms,p[v*2+1].ms),p[v*2].rs+p[v*2+1].ls);
}
int query(int v,int L,int R,int q)
{
    if(L==R || p[v].ls==R-L+1 || p[v].ms==0)
        return p[v].ms;
    int mid=(L+R)/2;
    if(q<=mid)
    {
        if(q>=mid-p[v*2].rs+1)
            return p[v*2].rs+p[v*2+1].ls;
        return query(v*2,L,mid,q);
    }
    else
    {
        if(q<=mid+p[v*2+1].ls)
            return p[v*2].rs+p[v*2+1].ls;
        return query(v*2+1,mid+1,R,q);
    }
}
int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        build(1,1,n);
        while(!st.empty()) st.pop();
        while(m--)
        {
            char op[2];
            int u;
            scanf("%s",op);
            if(op[0]=='D')
            {
                scanf("%d",&u);
                st.push(u);
                updata(1,1,n,u,0);
            }
            else if(op[0]=='R')
            {
                u=st.top(); st.pop();
                updata(1,1,n,u,1);
            }
            else if(op[0]=='Q')
            {
                scanf("%d",&u);
                printf("%d\n",query(1,1,n,u));
            }
        }
    }
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值