gjiajia @ 2024-08-06 13:50:50
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 6 * 1e6, MIN = -1e9;
struct nm
{
int sum, lmax, rmax, rt;
}num[N];
int n, q;
int a[N];
inline int read()
{
char c;
int sign = 1;
while((c = getchar()) > '9' || c < '0')
{
if(c == '-')
sign = -1;
}
int res = c - '0';
while((c = getchar()) >= '0' && c <= '9')
res = res * 10 + c - '0';
return res * sign;
}
void update(int u)
{
num[u].lmax = max(num[u << 1].lmax, num[u << 1].sum + num[u << 1 | 1].lmax);
num[u].rmax = max(num[u << 1 | 1].rmax, num[u << 1 | 1].sum + num[u << 1].rmax);
num[u].sum = num[u << 1].sum + num[u << 1 | 1].sum;
num[u].rt = max(num[u << 1].rt, num[u << 1 | 1].rt);
num[u].rt = max(num[u].rt, num[u << 1].rmax + num[u << 1 | 1].lmax);
}
void build(int u, int l, int r)
{
if(l == r)
{
num[u].sum = num[u].lmax = num[u].rmax = num[u].rt = a[l];
return;
}
int mid = l + r >> 1;
build(u << 1, l, mid);
build(u << 1 | 1, mid + 1, r);
update(u);
}
nm query(int u, int l, int r, int x, int y)
{
if(l >= x && r <= y) return num[u];
//if(l > y || r < x) return MIN;
int mid = l + r >> 1;
nm ret;
if(mid >= y) return query(u << 1, l, mid, x, y);
if(mid + 1 <= x) return query(u << 1 | 1, mid + 1, r, x, y);
nm ls = query(u << 1, l, mid, x, y);
nm rs = query(u << 1 | 1, mid + 1, r, x, y);
ret.sum = ls.sum + rs.sum;
ret.lmax = max(ls.lmax, ls.sum + rs.lmax);
ret.rmax = max(rs.rmax, rs.sum + ls.rmax);
ret.rt = max(ret.lmax, ret.rmax);
ret.rt = max(ret.rt, ls.rmax + rs.lmax);
return ret;
}
void ch(int u, int l, int r, int x, int y)
{
if(l == r && l == x)
{
num[u].sum = num[u].lmax = num[u].rmax = num[u].rt = y;
return;
}
int mid = l + r >> 1;
if(x <= mid) ch(u << 1, l, mid, x, y);
else ch(u << 1 | 1, mid + 1, r, x, y);
update(u);
}
signed main()
{
//freopen("P4513_2.in", "r", stdin);
n = read();
q = read();
for(int i = 1; i <= n; i++) a[i] = read();
//memset(lmax, -0x3f3f3f, szieof)
build(1, 1, n);
for(int i = 1; i <= q; i++)
{
int opt = read();
int x = read(), y = read();
if(opt == 2) ch(1, 1, n, x, y);
if(opt == 1)
{
if(x > y) swap(x, y);
cout << query(1, 1, n, x, y).rt << endl;
}
}
return 0;
}
by 5t0_0r2 @ 2024-08-06 14:08:23
@gjiajia
这句话是什么玩意:
ret.rt = max(ret.lmax, ret.rmax);
应为:
ret.rt = max(ls.rt, rs.rt);
by gjiajia @ 2024-08-06 14:21:57
@5t0_0r2 %%%
by gjiajia @ 2024-08-06 14:22:40
@5t0_0r2 谢谢,此贴结