HDU 1540 线段树求最大连续区间和问题

Tunnel Warfare

 

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个村庄用地道连成一条直线,鬼子有时破坏掉一个村庄,这时八路军要修好这个据点(这个据点是最近才被破坏的)。现在要求询问任意一个村庄,

得出这个村庄现在与几个村庄相连,包括它本身。

#include<iostream>
#include<stack>
#include<cstdio>
#include<queue>
using namespace std;
#define lson id<<1
#define rson id<<1|1
#define abs(a) (a)>=0?(a):(-a)
const int N = 5e4+10;
const int INF = 0x3f3f3f3f;
//求区间最大连续和的问题,要用到最大前缀,最大后缀,最大连续区间和
struct node{
    int l,r,suf,pre,all;//区间的左右端点,最大后缀长度,最大前缀长度,最大连续长度
}a[N<<2];
int ans;
void pushup(int id){
    a[id].all=max(a[lson].all,a[rson].all);//最大连续区间和就是在左子树的最大连续区间、右子树的最大连续区间和
    a[id].all=max(a[id].all,a[lson].suf+a[rson].pre);//还有左子树的最大后缀长度+右子树的最大前缀长度三者中的最大值

    //更改父节点前缀长度和后缀长度
    a[id].pre=a[lson].pre;
    a[id].suf=a[rson].suf;
    if(a[lson].pre==a[lson].r-a[lson].l+1) //如果左子树的最大前缀长度就是左子树的整个区间长度(+1的原因根据题意需要因为区间[1,2]有两个村庄,那么答案就是2-1+1)
        a[id].pre+=a[rson].pre;//那么父节点的前缀长度就加上右子树的前缀区间长度
    if(a[rson].suf==a[rson].r-a[rson].l+1)//同上
            a[id].suf+=a[lson].suf;
}
void build(int id,int l,int r){
    a[id].l=l;
    a[id].r=r;
    a[id].pre=a[id].suf=a[id].all=r-l+1;
    if(l==r){
        return ;
    }
    int mid=(l+r)>>1;
    build(lson,l,mid);
    build(rson,mid+1,r);
//    pushup(id);
}
void update(int id,int x,int val){
    if(a[id].l==a[id].r){
        a[id].pre=a[id].suf=a[id].all=val;
        return ;
    }
    int mid=(a[id].l+a[id].r)>>1;
    if(x<=mid) update(lson,x,val);
    else update(rson,x,val);
    pushup(id);
}
int query(int id,int x){
    if((a[id].l==a[id].r)||(a[id].r-a[id].l+1==a[id].all)||a[id].all==0)
        return a[id].all;
    int mid=(a[id].l+a[id].r)>>1;

   if(x<=mid){//缩减区间范围
     if(x>=a[lson].r-a[lson].suf+1)//当当前查询的坐标在左子树的后缀的右边界时,也就是当前查询的坐标还有可能在右子树是否存在与左区间相连的
        return query(lson,x)+query(rson,mid+1);//村庄,这里要注意的是查询右区间时,查询的是mid+1,因为这是线段树的性质决定的,mid不在右区间
                                            //所以只能是mid+1,这样就看看右区间是否有与x相连的村庄
    else//如果时小于左子树的右边界,那就只能在左子树中找答案了
        return query(lson,x);
    }
    else{
        if(x<=a[rson].l+a[rson].pre-1)
            return query(rson,x)+query(lson,mid);
        else
            return query(rson,x);
    }
}
stack<int>s;
int main()
{
    #ifndef ONLINE_JUDGE
    freopen("in.txt","r",stdin);
    #endif // ONLINE_JUDGE
    int n,q;

    while(scanf("%d%d",&n,&q)!=EOF){
        build(1,1,n);
        while(!s.empty()) s.pop();
        char str[2];int x=0;
        while(q--){
        scanf("%s",str);
        if(str[0]=='D'){
                scanf("%d",&x);
                s.push(x);
            update(1,x,0);//0代表不连通,也就是毁坏操作
        }else if(str[0]=='R'){
            if(s.size()){
            int u=s.top();
            s.pop();
//            printf("……%d\n",u);
            update(1,u,1);
            }
        }else{
            scanf("%d",&x);
           printf("%d\n",query(1,x));
        }
        }
    }
    return 0;
}

至于求最大区间和的白书(Page 201)上还有一道关于动态最大连续和问题 LA3938

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值