Algorithm competition advanced guide ---0x43 (line segment tree) Can you answer these questions?

Topic

Insert picture description here

Sample output

5 3
1 2 -3 4 5
1 2 3
2 2 -1
1 3 2

Input sample

2
-1

answer

  1. Single-point modification, interval query, classic line segment tree problem, generally single-point modification does not use lazy mark (pushdown operation), the problem will be simpler
  1. When we do the problem of the line segment tree, we generally determine the attributes of the node first, and then do it. Then, what are the attributes of the node, the most basic is the left and right endpoints of the interval l, r. There is also the largest continuous field and lmax required in the question. With these three, we have to consider whether we can update the lmax of the parent node from the child node. Obviously this is not possible. Looking at the picture, for the case that does not cross the interval, we only need to maximize the lmax of the left and right sons. , But for the inter-interval, the lmax of the parent node should be the largest suffix of the left son and the largest prefix sum of the right son
    Insert picture description here
  1. Then our node attributes should increase permax, sufmax, then for the newly added attributes, can we update through the child nodes, we also need to divide the situation, as shown in the figure, we have added the attribute sum (interval sum) , The sum of the intervals of the parent node is equal to the sum of the intervals of the left and right sons, so we don’t need to add new attributes. Of course, the maximum suffix is ​​also the same. It’s not explained here. At this point, the attributes of our nodes are all updated. Up.
    Insert picture description here
  1. As long as the attributes in the node are determined, we can directly set the template for the rest, see the code for details

Code

#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>

using namespace std;
const int N = 5e5 + 10;

int n, m;
int w[N];

struct Node {
    
    
    int l, r;       //左右端点
    int tmax;      //最大连续子段和
    int premax;   //最大前缀和
    int sufmax;  //最大后缀和
    int sum;    //区间和
} tr[N * 4];

//更新节点信息的函数
void pushup(Node &u, Node &l, Node &r) {
    
    
    u.sum = l.sum + r.sum;
    u.premax = max(l.premax, l.sum + r.premax);
    u.sufmax = max(r.sufmax, r.sum + l.sufmax);
    u.tmax = max(max(l.tmax, r.tmax), l.sufmax + r.premax);
}

//由子节点更新父节点的信息
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, w[r], w[r], w[r], w[r]};
    else {
    
    
        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 v) {
    
    
    if (tr[u].l == x && tr[u].r == x) tr[u] = {
    
    x, x, v, v, v, v};
    else {
    
    
        int mid = (tr[u].l + tr[u].r) >> 1;
        if (x <= mid) modify(u << 1, x, v);
        else modify(u << 1 | 1, x, v);
        pushup(u);
    }
}


//区间查询
Node query(int u, int l, int r) {
    
    
    if (l <= tr[u].l && tr[u].r <= r) return tr[u];
    else {
    
    
        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 {
    
    
            Node res;
            auto left = query(u << 1, l, r);
            auto right = query(u << 1 | 1, l, r);
            pushup(res, left, right);
            return res;
        }
    }
}

int main() {
    
    

    cin >> n >> m;
    for (int i = 1; i <= n; i++) scanf("%d", &w[i]);

    build(1, 1, n);

    while (m--) {
    
    
        int op, x, y;
        cin >> op >> x >> y;
        if (op == 1) {
    
    
            if (x > y) swap(x, y);
            cout << query(1, x, y).tmax << endl;
        } else {
    
    
            modify(1, x, y);
        }
    }
    return 0;
}

Guess you like

Origin blog.csdn.net/qq_44791484/article/details/113827997