HDU - 1540 I - Tunnel Warfare 线段树 + (二分 or 区间合并)

Tunnel Warfare

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


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 9D 3D 6D 5Q 4Q 5RQ 4RQ 4
 

Sample Output
 
 
1024
 

Source
 

Recommend
LL

 


题解:


线段树维护区间和

修改和复原分别置1置0

对于查询操作 二分查询x端点向左向右能达到的最大长度 

单次查询时间复杂度: O(logm*logm) 二分logm * check(线段树查询区间和) logm


代码:

#include<bits/stdc++.h>
using namespace std;
const int maxn = 5e4+10;
int n,q;
stack<int> sta;
struct SegTree
{
    int sum[maxn<<2];
    void push_up(int rt) {
        sum[rt] = sum[rt<<1] + sum[rt<<1|1];
    }
    void build(int l,int r,int rt) {
        if(l == r) {
            sum[rt] = 1;
            return;
        }
        int mid = (l+r) >> 1;
        build(l,mid,rt<<1);
        build(mid+1,r,rt<<1|1);
        push_up(rt);
    }
    void update(int pos,int val,int l,int r,int rt) {
        if(l == r) {
            sum[rt] = val;
            return;
        }
        int mid = (l+r) >> 1;
        if(pos <= mid) update(pos,val,l,mid,rt<<1);
        else update(pos,val,mid+1,r,rt<<1|1);
        push_up(rt);
    }
    int query(int ql,int qr,int l,int r,int rt) {
        
        if(ql == l && qr == r) {
            return sum[rt];
        }
        int mid = (l + r) >> 1;
        if(qr <= mid) return query(ql,qr,l,mid,rt<<1);
        if(ql > mid) return query(ql,qr,mid+1,r,rt<<1|1);
        return query(ql,mid,l,mid,rt<<1) + query(mid+1,qr,mid+1,r,rt<<1|1);
    }
}seg;
bool check(int l,int r)
{
    int tot = seg.query(l,r,1,n,1);
    return tot == r - l + 1;
}
int main()
{
    while(~scanf("%d%d",&n,&q))
    {
        while(!sta.empty()) sta.pop();
        seg.build(1,n,1);
        char op;
        int x;
        while(q--)
        {
            scanf("%*c%c",&op);
            if(op == 'D') {
                scanf("%d",&x);
                sta.push(x);
                seg.update(x,0,1,n,1);
            }
            else if(op == 'Q') {
                scanf("%d",&x);
                int l = 1,r = x;
                while(l <= r) {
                    int mid = (l + r) >> 1;
                    if(check(mid,x)) r = mid - 1;
                    else l = mid + 1;
                }
                int ans = x - l + 1;
                l = x,r = n;
                while(l <= r) {
                    int mid = (l + r) >> 1;
                    if(check(x,mid)) l = mid + 1;
                    else r = mid - 1;
                }
                ans += r - x + 1;
                if(check(x,x)) ans--;
                printf("%d\n",ans);
            }
            else 
            {
                if(!sta.empty()) {
                    seg.update(sta.top(),1,1,n,1);
                    sta.pop();
                }
            }
        }
    }
    return 0;
}


线段树区间合并


lsum[rt] 记录区间左端点向右能达到的最大长度

rsum[rt] 记录区间右端点向左能达到的最大长度

于是对于操作2 查询包括x的最大连续子区间

我们只需要求出[1,x]的rsum 和 [x,n]的lsum

最后如果和>0,减1即可,因为x多计算了1次

代码:

#include<bits/stdc++.h>
using namespace std;
const int maxn = 5e4+10;
stack<int> sta;
int n,q;
struct SegTree
{
    int sum[maxn<<2],lsum[maxn<<2],rsum[maxn<<2],cnt[maxn<<2];
    void init() {
        memset(sum,0,sizeof(sum));
        memset(lsum,0,sizeof(lsum));
        memset(rsum,0,sizeof(rsum));
        memset(cnt,0,sizeof(cnt));
    }
    void push_up(int rt) {
        cnt[rt] = cnt[rt<<1] + cnt[rt<<1|1];
        lsum[rt] = lsum[rt<<1];
        rsum[rt] = rsum[rt<<1|1];
        if(lsum[rt] == cnt[rt<<1]) lsum[rt] += lsum[rt<<1|1];
        if(rsum[rt] == cnt[rt<<1|1]) rsum[rt] += rsum[rt<<1];
        sum[rt] = max(max(sum[rt<<1],sum[rt<<1|1]),rsum[rt<<1]+lsum[rt<<1|1]);
    }
    void build(int l,int r,int rt) {
        if(l == r) {
            cnt[rt] = sum[rt] = lsum[rt] = rsum[rt] = 1;
            return ;
        }
        int mid = (l + r)>>1;
        build(l,mid,rt<<1);
        build(mid+1,r,rt<<1|1);
        push_up(rt);
    }
    void update(int pos,int val,int l,int r,int rt) {
        if(l == r) {
            if(val) { /// 置满
                sum[rt] = lsum[rt] = rsum[rt] = 0;
            }
            else {
                sum[rt] = lsum[rt] = rsum[rt] = 1;
            }
            return ;
        }
        int mid = (l+r)>>1;
        if(pos <= mid) update(pos,val,l,mid,rt<<1);
        else update(pos,val,mid+1,r,rt<<1|1);
        push_up(rt);
    }
    int query_left(int ql,int qr,int l,int r,int rt) {
        if(ql == l && qr == r) {
            return rsum[rt];
        }
        int mid = (l+r)>>1;
        if(qr <= mid) return query_left(ql,qr,l,mid,rt<<1);
        if(ql > mid) return query_left(ql,qr,mid+1,r,rt<<1|1);
        int res = query_left(mid+1,qr,mid+1,r,rt<<1|1);
        if(res == qr - mid) res += query_left(ql,mid,l,mid,rt<<1);
        return res;
    }
    int query_right(int ql,int qr,int l,int r,int rt) {
        if(ql == l && qr == r) {
            return lsum[rt];
        }
        int mid = (l+r)>>1;
        if(qr <= mid) return query_right(ql,qr,l,mid,rt<<1);
        if(ql > mid) return query_right(ql,qr,mid+1,r,rt<<1|1);
        int res = query_right(ql,mid,l,mid,rt<<1);
        if(res == mid - ql + 1) res += query_right(mid+1,qr,mid+1,r,rt<<1|1);
        return res;
    }
}seg;
inline void init() {
    while(!sta.empty()) sta.pop();
}
int main()
{
    while(~scanf("%d%d",&n,&q))
    {
        init();
        //seg.init();
        seg.build(1,n,1);
        char op[10];
        int x;
        while(q--)
        {
            scanf("%s",op);
            if(op[0] == 'D') {
                scanf("%d",&x);
                seg.update(x,1,1,n,1);
                sta.push(x);
            }
            else if(op[0] == 'Q') {
                scanf("%d",&x);
                int ans = seg.query_left(1,x,1,n,1) + seg.query_right(x,n,1,n,1);
                printf("%d\n",ans?ans-1:0);
            }
            else {
                if(!sta.empty()) {
                    seg.update(sta.top(),0,1,n,1);
                    sta.pop();
                }
            }
        }
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值