AcWing245
#define ll long long int
#define tp t[p]
#define tl t[p << 1]
#define tr t[p << 1 | 1]
const int inf = 0x3f3f3f3f;
const int N = 5e5 + 3, M = N * 4;
int n, m;
int a[N];
struct node
{
int l, r;
int sum, lm, rm, date;
node()
{
l = r = 0;
sum = 0;
lm = rm = date = -inf;
}
node(int a, int b, int c, int d, int e, int f)
{
l = a; r = b; sum = c, lm = d, rm = e, date = f;
}
}t[N * 4];
void seg(int p)
{
tp.sum = tl.sum + tr.sum;
tp.lm = max(tl.lm, tl.sum + tr.lm);
tp.rm = max(tr.rm, tl.rm + tr.sum);
tp.date = max({ tr.date, tl.date, tl.rm + tr.lm });
}
void build(int p, int l, int r)
{
tp.l = l, tp.r = r;
if (l == r) { tp.date = tp.lm = tp.rm = tp.sum = a[l]; return; }
int mid = l + r >> 1;
build(p << 1, l, mid);
build(p << 1 | 1, mid + 1, r);
seg(p);
}
void change(int p, int x, ll v)
{
if (tp.l == tp.r) { tp.date = tp.lm = tp.rm = tp.sum = v; return; }
int mid = tp.l + tp.r >> 1;
if (x <= mid) change(p << 1, x, v);
else change(p << 1 | 1, x, v);
seg(p);
}
node ask(int p, int l, int r)
{
if (l <= tp.l && r >= tp.r) return tp;
int mid = tp.l + tp.r >> 1;
node a, b, c;
c = { l, r, 0, -inf, -inf, -inf };
if (l <= mid) a = ask(p << 1, l, r);
if (r > mid) b = ask(p << 1 | 1, l, r);
c.sum = a.sum + b.sum;
c.lm = max(a.lm, a.sum + b.lm);
c.rm = max(a.rm + b.sum, b.rm);
c.date = max({ a.date, b.date, a.rm + b.lm });
return c;
}
int main()
{
IOS;
int n, m; cin >> n >> m;
for (int i = 1; i <= n; i++) cin >> a[i];
build(1, 1, n);
int k, x, y;
while (m--)
{
cin >> k >> x >> y;
if (k == 1)
{
if (x > y) swap(x, y);
cout << ask(1, x, y).date << endl;
}
else change(1, x, y);
}
return 0;
}
|