AcWing 245 你能回答这些问题吗? (线段树)
题目链接:https://www.acwing.com/problem/content/description/246/
经典题,动态维护最大字段和
线段树节点内除了 \(sum\) 外,还要维护左起最大子段和,右起最大子段和,最大子段和
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<cmath>
#include<stack>
#include<queue>
using namespace std;
typedef long long ll;
const int maxn = 500010;
const int INF = 1000000007;
int n, m;
int a[maxn];
struct SEG{
int sum, lsum, rsum, msum;
SEG (int sum = 0, int lsum = -INF, int rsum = -INF, int msum = -INF): sum(sum), lsum(lsum), rsum(rsum), msum(msum){}
}t[maxn << 2];
void pushup(int i){
t[i].sum = t[i << 1].sum + t[i << 1 | 1].sum;
t[i].lsum = max(t[i << 1].lsum, t[i << 1].sum + t[i << 1 | 1].lsum);
t[i].rsum = max(t[i << 1 | 1].rsum, t[i << 1].rsum + t[i << 1 | 1].sum);
t[i].msum = max(max(t[i << 1].msum, t[i << 1 | 1].msum),t[i << 1].rsum + t[i << 1 | 1].lsum);
}
void build(int i, int l, int r){
if(l == r){
t[i].sum = t[i].msum = t[i].lsum = t[i].rsum = a[l];
return;
}
int mid = (l + r) >> 1;
build(i << 1, l, mid); build(i << 1|1, mid + 1, r);
pushup(i);
}
void modify(int i, int l, int r, int k, int p){
if(l == r){
t[i].lsum = t[i].rsum = t[i].msum = t[i].sum = k;
return;
}
int mid = (l + r) >> 1;
if(p <= mid) modify(i << 1, l, mid, k, p);
else modify(i << 1 | 1, mid + 1, r, k, p);
pushup(i);
}
SEG query(int i, int l, int r, int x, int y){
if(x <= l && r <= y){
return t[i];
}
int mid = (l + r) >> 1;
SEG lt, rt, at;
if(x <= mid) lt = query(i << 1, l, mid, x, y);
if(y > mid) rt = query(i << 1 | 1, mid + 1, r, x, y);
int ls = max(lt.lsum, lt.sum + rt.lsum);
int rs = max(rt.rsum, rt.sum + lt.rsum);
int ms = max(max(lt.msum, rt.msum), lt.rsum + rt.lsum);
int s = lt.sum + rt.sum;
return SEG(s, ls, rs, ms);
}
ll read(){ ll s=0,f=1; char ch=getchar(); while(ch<'0' || ch>'9'){ if(ch=='-') f=-1; ch=getchar(); } while(ch>='0' && ch<='9'){ s=s*10+ch-'0'; ch=getchar(); } return s*f; }
int main(){
n = read(), m = read();
for(int i = 1; i <= n; ++i) a[i] = read();
build(1, 1, n);
int op, x, y;
for(int i = 1; i <= m; ++i){
op = read(), x = read(), y = read();
if(op == 1){
if(x > y) swap(x, y);
SEG res = query(1, 1, n, x, y);
printf("%d\n", res.msum);
}else{
modify(1, 1, n, y, x);
}
}
return 0;
}