计蒜客 黑白石头(线段树)

本文介绍了一种高效算法,用于在给定区间内计算最大值,并同时处理两端元素,特别适用于需要频繁更新区间数据的情况。通过结构化数组和递归构建,实现快速查询与更新操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

 

 

/*
维护区间内最大值和两端的结果
两种颜色一起计算会方便很多
*/
#include<bits/stdc++.h>

using namespace std;

const int maxn=1e5+10;

struct node{
    int l,r;
    int lw,rw;
    int lb,rb;
    int maxw,maxb;
    bool change;
}tree[maxn<<2];

void pushup(int p){
    //左边白色
    tree[p].lw=tree[p<<1].lw;
    if(tree[p].lw==tree[p<<1].r-tree[p<<1].l+1){
        tree[p].lw+=tree[p<<1|1].lw;
    }
    //左边黑色
    tree[p].lb=tree[p<<1].lb;
    if(tree[p].lb==tree[p<<1].r-tree[p<<1].l+1){
        tree[p].lb+=tree[p<<1|1].lb;
    }

    //右边白色
    tree[p].rw=tree[p<<1|1].rw;
    if(tree[p].rw==tree[p<<1|1].r-tree[p<<1|1].l+1){
        tree[p].rw+=tree[p<<1].rw;
    }

    //右边黑色
    tree[p].rb=tree[p<<1|1].rb;
    if(tree[p].rb==tree[p<<1|1].r-tree[p<<1|1].l+1){
        tree[p].rb+=tree[p<<1].rb;
    }

    tree[p].maxw=max(max(tree[p<<1].maxw,tree[p<<1|1].maxw),tree[p<<1].rw+tree[p<<1|1].lw);
    tree[p].maxb=max(max(tree[p<<1].maxb,tree[p<<1|1].maxb),tree[p<<1].rb+tree[p<<1|1].lb);
}

void build(int p,int l,int r){
    tree[p].l=l;
    tree[p].r=r;
    tree[p].change=false;
    if(l==r){
        int t;
        scanf("%d",&t);
        tree[p].maxb=tree[p].lb=tree[p].rb=t==1;
        tree[p].maxw=tree[p].lw=tree[p].rw=t==0;
        return;
    }
    int m=l+r>>1;
    build(p<<1,l,m);
    build(p<<1|1,m+1,r);
    pushup(p);
}

void pushdown(int p){
    if(tree[p].change){
        swap(tree[p<<1].maxw,tree[p<<1].maxb);
        swap(tree[p<<1].lw,tree[p<<1].lb);
        swap(tree[p<<1].rw,tree[p<<1].rb);

        swap(tree[p<<1|1].maxw,tree[p<<1|1].maxb);
        swap(tree[p<<1|1].lw,tree[p<<1|1].lb);
        swap(tree[p<<1|1].rw,tree[p<<1|1].rb);

        tree[p<<1].change=!tree[p<<1].change;
        tree[p<<1|1].change=!tree[p<<1|1].change;
        tree[p].change=false;
    }
}

void update(int p,int l,int r){
    if(l<=tree[p].l&&tree[p].r<=r){
        swap(tree[p].lw,tree[p].lb);
        swap(tree[p].rw,tree[p].rb);
        swap(tree[p].maxw,tree[p].maxb);
        tree[p].change=!tree[p].change;
        return;
    }
    pushdown(p);
    int m=tree[p].l+tree[p].r >> 1;
    if(l<=m) update(p<<1,l,r);
    if(r>m) update(p<<1|1,l,r);
    pushup(p);
}

int query(int p,int l,int r){
    if(l<=tree[p].l&&tree[p].r<=r){
        return tree[p].maxb;
    }
    pushdown(p);
    int m=tree[p].l+tree[p].r >> 1;
    int res=0;
    if(l<=m) res=max(res,query(p<<1,l,r));
    if(r>m) res=max(res,query(p<<1|1,l,r));
    if(l<=m&&r>m) res=max(res,min(m-l+1,tree[p<<1].rb)+min(r-m,tree[p<<1|1].lb));
    return res;
}

int main(){
    int n,m,op,l,r;
    scanf("%d",&n);
    build(1,1,n);
    scanf("%d",&m);
    while(m--){
        scanf("%d%d%d",&op,&l,&r);
        if(op==1){
            update(1,l,r);
        }else printf("%d\n",query(1,l,r));
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值