题意分析
很裸的一道题目
注意更新的时候对应的节点的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;
}
787

被折叠的 条评论
为什么被折叠?



