Vijos1659 河蟹王国 (线段树 区间更新 区间最值)

题意分析

很裸的一道题目

注意更新的时候对应的节点的mx也要更新。

代码总览

#include<bits/stdc++.h>
using namespace std;
const int nmax = 100001;
const int INF = 0x3f3f3f3f;
typedef long long ll;
typedef struct{
    int l, r;
    ll lazy,mx;
    int mid() {return(l+r)>>1;}
}Tree;
Tree tree[nmax<<2];
ll num[nmax];
int n,m;
void pushup(int rt){
    tree[rt].mx = max(tree[rt<<1].mx,tree[rt<<1|1].mx);
}
void pushdown(int rt){
    if(tree[rt].lazy){
        tree[rt<<1].lazy += tree[rt].lazy;
        tree[rt<<1|1].lazy += tree[rt].lazy;
        tree[rt<<1].mx += tree[rt].lazy;
        tree[rt<<1|1].mx += tree[rt].lazy;
        tree[rt].lazy = 0;
    }
}
void build(int l, int  r, int rt){
    tree[rt].l = l, tree[rt].r = r;
    tree[rt].lazy = tree[rt].mx = 0;
    if(tree[rt].l == tree[rt].r) {
        tree[rt].mx = num[tree[rt].l];
        return;
    }
    build(l,tree[rt].mid(),rt<<1);
    build(tree[rt].mid() +1, r , rt<<1|1);
    pushup(rt);
}
void update(ll const & v,int l ,int r,int rt){
    if(tree[rt].l >= l && tree[rt].r <= r){
        tree[rt].lazy += v;
        tree[rt].mx += v;
        return;
    }
    pushdown(rt);
    if(r <= tree[rt].mid()) update(v,l,r,rt<<1);
    else if( l > tree[rt].mid()) update(v,l,r,rt<<1|1);
    else update(v,l,tree[rt].mid(),rt<<1), update(v,tree[rt].mid()+1 ,r, rt<<1|1);
    pushup(rt);
}
ll query(int l, int r, int rt){
    if(tree[rt].l == l && tree[rt].r == r) return tree[rt].mx;
    pushdown(rt);
    if(r <= tree[rt].mid()) return query(l,r,rt<<1);
    else if(l > tree[rt].mid()) return query(l,r,rt<<1|1);
    else return max(query(l,tree[rt].mid(),rt<<1), query(tree[rt].mid()+1,r,rt<<1|1));
}
int main(){
    scanf("%d",&n);
    for(int i = 1;i<=n;++i) scanf("%lld",&num[i]);
    build(1,n,1);
    scanf("%d",&m);
    int k,a,b;
    for(int i = 0;i<m;++i){
        scanf("%d%d%d",&k,&a,&b);
        if(k == 1){
            ll v; scanf("%lld",&v);
            update(v,a,b,1);
        }else{
            printf("%lld\n",query(a,b,1));
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值