本文主要是介绍AcWing 245. 你能回答这些问题吗,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
#include <bits/stdc++.h>
using namespace std;
const int N = 5e5 + 10;
int n, m;
int a[N];
struct Node {
int l, r;
int sum, lmax, rmax, tmax;
//sum,
//lmax最大前缀和
//rmax最大后缀和
//tmax最大连续子段和
}tr[N * 4];
void pushup(Node &U, Node &L, Node &R)
{
U.sum = L.sum + R.sum;
U.lmax = max(L.lmax, L.sum + R.lmax);
U.rmax = max(R.rmax, R.sum + L.rmax);
U.tmax = max(max(L.tmax, R.tmax), L.rmax + R.lmax);
}
void pushup(int u)
{
pushup(tr[u], tr[u << 1], tr[u << 1 | 1]);
}
void build(int u, int l, int r)
{
if (l == r) {tr[u] = {l, r, a[l], a[l], a[l], a[l]}; return;}
tr[u] = {l, r};
int mid = l + r >> 1;
build(u << 1, l, mid);
build(u << 1 | 1, mid + 1, r);
pushup(u);
}
void modify(int u, int x, int y)
{
if (tr[u].l == x && tr[u].r == x) {tr[u] = {x, x, y, y, y, y}; return;}
int mid = tr[u].l + tr[u].r >> 1;
if (x <= mid) modify(u << 1, x, y);
if (x > mid) modify(u << 1 | 1, x, y);
pushup(u);
}
Node query(int u, int l, int r)
{
if(tr[u].l >= l && tr[u].r <= r) return tr[u];
int mid = tr[u].l + tr[u].r >> 1;
if(r <= mid) return query(u << 1, l, r);
else if(l > mid) return query(u << 1 | 1, l, r);
else
{
auto left = query(u << 1, l, r);
auto right = query(u << 1 | 1, l, r);
Node res;
pushup(res, left, right);
return res;
}
}
int main() {
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i ++ ) scanf("%d", &a[i]);
build(1, 1, n);
while (m -- ) {
int type, x, y;
scanf("%d%d%d", &type, &x, &y);
if (type == 2)
modify(1, x, y);
else {
if (x > y) swap(x, y);
printf("%d\n", query(1, x, y).tmax);
}
}
return 0;
}
这篇关于AcWing 245. 你能回答这些问题吗的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!