SP1716 GSS3 - Can you answer these queries III segment tree

Topic Portal: SP1043 GSS1 - of Can you answer the I THESE Queries

Better reading experience

And maximum dynamic maintenance sub-segment

Pre-knowledge

Static maintenance sub-segments and maximum : SP1043 GSS1 - of Can you answer the I THESE Queries

Transfer solution to a problem

answer:

Providing segment tree structure pointer written:

Set \ (L \) is the left end point interval, \ (R & lt \) to the right end of the interval;

\ (LS \) is to \ (L \) a maximum sub-segment and a left end point, \ (RS \) is to \ (R & lt \) a maximum sub-segment and the right end point;

\ (sum \) for the period and, \ (Val \) is the largest segment and sub-sections and.

\ (ch [0] \) left his son, \ (CH [1] \) is the right son.

Consider maintenance:

o->sum = ch[0]->sum + ch[1]->sum;
o->ls = max(ch[0]->ls, ch[0]->sum + ch[1]->ls);
o->rs = max(ch[1]->rs, ch[1]->sum + ch[0]->rs);
o->val = max(max(ch[0]->val, ch[1]->val), max(max(o->ls, o->rs), ch[0]->rs + ch[1]->ls);

This will take into account all the circumstances of the transfer;

When asked, if asked about two sub-intervals across the range, we use \ (ls, rs, sum \ ) to update the current interval \ (Val \) ;

So we return structure pointer.

Title to modify a single point, consider how to write up () function.

Maintenance found the same way and achievements.

void up() {
    sum = ch[0]->sum + ch[1]->sum;
    ls = Max(ch[0]->ls, ch[0]->sum + ch[1]->ls);
    rs = Max(ch[1]->rs, ch[1]->sum + ch[0]->rs);
    val = Max(Max(ch[0]->val, ch[1]->val), Max(Max(ls, rs), ch[0]->rs + ch[1]->ls));
}

Fucks to direct.

code:

#include <iostream>
#include <cstdio>
using namespace std;
const int N = 5e4 + 5;
int read() {
    int x = 0, f = 1; char ch;
    while(! isdigit(ch = getchar())) (ch == '-') && (f = -f);
    for(x = ch^48; isdigit(ch = getchar()); x = (x<<3) + (x<<1) + (ch^48));
    return x * f;
}
int n, m;
template <class T> T Max(T a, T b) { return a > b ? a : b; }
struct Segment {
    struct node {
        int l, r, ls, rs, sum, val;
        node *ch[2];
        node(int l, int r, int ls, int rs, int sum, int val) : l(l), r(r), ls(ls), rs(rs), sum(sum), val(val) {
            ch[0] = ch[1] = NULL;
        }
        inline int mid() { return (l + r) >> 1; }
        inline void up() {
            sum = ch[0]->sum + ch[1]->sum;
            ls = Max(ch[0]->ls, ch[0]->sum + ch[1]->ls);
            rs = Max(ch[1]->rs, ch[1]->sum + ch[0]->rs);
            val = Max(Max(ch[0]->val, ch[1]->val), Max(Max(ls, rs), ch[0]->rs + ch[1]->ls));
        }
    } *root;
    void build(node *&o, int l, int r) {
        o = new node(l, r, 0, 0, 0, 0);
        if(l == r) {
            o->ls = o->rs = o->sum = o->val = read();
            return;
        }
        build(o->ch[0], l, o->mid());
        build(o->ch[1], o->mid() + 1, r);
        o->up();
    }
    void change(node *o, int x, int v) {
        if(o->l == x && o->r == x) {
            o->ls = o->rs = o->sum = o->val = v;
            return;
        }
        if(x <= o->mid()) change(o->ch[0], x, v);
        if(x > o->mid()) change(o->ch[1], x, v);
        o->up();
    }
    node *query(node *o, int l, int r) {
        if(l <= o->l && o->r <= r) return o;
        if(r <= o->mid()) return query(o->ch[0], l, r);
        if(l > o->mid()) return query(o->ch[1], l, r);
        node *res = new node(l, r, 0, 0, 0, 0);
        node *t1 = query(o->ch[0], l, r), *t2 = query(o->ch[1], l, r);
        res->sum = t1->sum + t2->sum;
        res->ls = Max(t1->ls, t1->sum + t2->ls);
        res->rs = Max(t2->rs, t2->sum + t1->rs);
        res->val = Max(Max(t1->val, t2->val), Max(Max(res->ls, res->rs), t1->rs + t2->ls));
        return res;
    }
} tr;
int main() {
    n = read();
    tr.build(tr.root, 1, n);
    m = read();
    for(int i = 1, opt, l, r; i <= m; ++ i) {
        opt = read(); l = read(); r = read();
        if(opt & 1) printf("%d\n", tr.query(tr.root, l, r)->val);
        else tr.change(tr.root, l, r);
    }
    return 0;
}

Guess you like

Origin www.cnblogs.com/Paranoid-LS/p/11593000.html