「SP1716」GSS3 - Can you answer these queries III

Portal
Luogu

Problem-solving ideas

The maximum range of the sub-segments and board title.
Consider do with tree line.
For a segment tree contains a node interval and the maximum sub-segment, and there are two cases, and does not contain comprise the midpoint.
Not included directly transferred from the left and right subtrees.
In the case included:
We maintain two values for each node: the beginning is the largest sub-segment of the left end point and, at the end is the right end point and the largest sub-segment.
Then the situation contains the midpoint of two things can be transferred with it.
So these two things, how to maintain it. . .
They also have included not included with the midpoint of the range and just remember this node can be, specific methods above.
So I get this question.

Details Notes

  • Gugu Gu

Reference Code

#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cctype>
#include <cmath>
#include <ctime>
#define rg register
using namespace std;
template < typename T > inline void read(T& s) {
    s = 0; int f = 0; char c = getchar();
    while (!isdigit(c)) f |= (c == '-'), c = getchar();
    while (isdigit(c)) s = s * 10 + (c ^ 48), c = getchar();
    s = f ? -s : s;
}

const int _ = 50010;

int n, q, a[_];
struct node { int sum, L, R, mx; }t[_ << 2];

inline int lc(int rt) { return rt << 1; }

inline int rc(int rt) { return rt << 1 | 1; }

inline void pushup(int rt) {
    t[rt].sum = t[lc(rt)].sum + t[rc(rt)].sum;
    t[rt].L = max(t[lc(rt)].L, t[lc(rt)].sum + t[rc(rt)].L);
    t[rt].R = max(t[rc(rt)].R, t[rc(rt)].sum + t[lc(rt)].R);
    t[rt].mx = max(t[lc(rt)].R + t[rc(rt)].L, max(t[lc(rt)].mx, t[rc(rt)].mx));
}

inline void build(int rt = 1, int l = 1, int r = n) {
    if (l == r) { t[rt] = (node) { a[l], a[l], a[l], a[l] }; return; }
    int mid = (l + r) >> 1;
    build(lc(rt), l, mid), build(rc(rt), mid + 1, r), pushup(rt);
}

inline void update(int id, int v, int rt = 1, int l = 1, int r = n) {
    if (l == r) { t[rt] = (node) { v, v, v, v }; return; }
    int mid = (l + r) >> 1;
    if (id <= mid) update(id, v, lc(rt), l, mid);
    else update(id, v, rc(rt), mid + 1, r);
    pushup(rt);
}

inline node query(int ql, int qr, int rt = 1, int l = 1, int r = n) {
    if (ql <= l && r <= qr) return t[rt];
    int mid = (l + r) >> 1;
    if (qr <= mid) return query(ql, qr, lc(rt), l, mid);
    if (ql > mid) return query(ql, qr, rc(rt), mid + 1, r);
    node ls = query(ql, mid, lc(rt), l, mid);
    node rs = query(mid + 1, qr, rc(rt), mid + 1, r);
    node res = { 0, 0, 0, 0 };
    res.sum = ls.sum + rs.sum;
    res.L = max(ls.L, ls.sum + rs.L);
    res.R = max(rs.R, rs.sum + ls.R);
    res.mx = max(ls.R + rs.L, max(ls.mx, rs.mx));
    return res;
}

int main() {
#ifndef ONLINE_JUDGE
    freopen("in.in", "r", stdin);
#endif
    read(n);
    for (rg int i = 1; i <= n; ++i) read(a[i]);
    build();
    read(q);
    for (int f, x, y; q--; ) {
        read(f), read(x), read(y);
        if (!f) update(x, y);
        else printf("%d\n", query(x, y).mx);
    }
    return 0;
}

End Sahua \ (qwq \)

Guess you like

Origin www.cnblogs.com/zsbzsb/p/11746528.html