This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<iostream>
#include<stdio.h>
#include<stdint.h>
#include<iomanip>
#include<algorithm>
#include<utility>
#include<vector>
#include<stack>
#include<queue>
#include<set>
#include<map>
#include<deque>
#include<math.h>
#include<assert.h>
#include<string.h>
using namespace std;
const int64_t INF = (int64_t) 1e18 + 100;
const int64_t mINF = (int64_t) 1e6 * 2 + 100;
const int64_t MOD = 1e9 + 7;
const int64_t MOD2 = 998244353;
const int nbit = 31;
const int ndig = 10;
const int nchar = 26;
const int D = 4;
int dr[D] = {-1, 0, 1, 0};
int dc[D] = {0, 1, 0, -1};
const int Dk = 8;
int drk[Dk] = {2, 2, -2, -2, 1, 1, -1, -1};
int dck[Dk] = {1, -1, 1, -1, 2, -2, 2, -2};
struct Node
{
    int l; int r;
    int64_t ar; int64_t ab;
    int64_t lb; int64_t lr;
    Node(int l_, int r_, int64_t ar_, int64_t ab_, int64_t lb_, int64_t lr_) : l(l_), r(r_), ar(ar_), ab(ab_), lb(lb_), lr(lr_) {}
};
struct Solution
{
    int n;
    int q;
    int k;
    vector<int64_t> a;
    vector<int64_t> d;
    vector<Node> tree;
    Solution() {}
    void solve()
    {
        cin >> n >> q;
        a.resize(n, 0);
        d.resize(n, 0);
        for(int i = 0; i < n; i++)
        {
            cin >> a[i];
        }
        k = n;
        while(__builtin_popcount(k) != 1) k++;
        tree.resize(2 * k, Node(0, 0, 0, 0, 0, 0));
        for(int i = 1; i < n; i++)
        {
            d[i] = a[i] - a[i - 1];
            tree[k + i] = Node(d[i], d[i], 0, 0, 0, Abs(d[i]));
        }
        for(int i = k - 1; i >= 1; i--)
        {
            tree[i] = combine(tree[i * 2], tree[i * 2 + 1]);
        }
        for(int i = 0; i < q; i++)
        {
            int l;
            int r;
            int x;
            cin >> l >> r >> x;
            l--; r--;
            if(l != 0) update(l, x);
            if(r + 1 < n) update(r + 1, -x);
            int64_t ans = max({tree[1].ab, tree[1].ar, tree[1].lb, tree[1].lr});
            cout << ans << "\n";
        }
    }
    void update(int i, int val)
    {
        tree[k + i].l += val;
        tree[k + i].r += val;
        tree[k + i].lr = Abs(tree[k + i].l);
        for(int j = (k + i) / 2; j >= 1; j /= 2)
        {
            tree[j] = combine(tree[j * 2], tree[j * 2 + 1]);
        }
    }
    Node combine(Node& left, Node& right)
    {
        Node now(left.l, right.r, 0, 0, 0, 0);
        if(1LL * left.r * right.l >= 0)
        {
            now.lr = left.lr + right.lr;
            now.ar = left.ar + right.lr;
            now.lb = left.lr + right.lb;
            now.ab = left.ar + right.lb;
        }
        else
        {
            now.lr = max(left.lr + right.ar, left.lb + right.lr);
            now.ar = max(left.ar + right.ar, left.ab + right.lr);
            now.lb = max(left.lr + right.ab, left.lb + right.lb);
            now.ab = max(left.ar + right.ab, left.ab + right.lb);
        }
        return now;
    }
    void modadd(int& t1, int t2)
    {
        t1 += t2;
        if(t1 >= MOD) t1-= MOD;
        return;
    }
    void modsub(int& t1, int t2)
    {
        t1 -= t2;
        if(t1 < 0) t1 += MOD;
        return;
    }
    int modmul(int t1, int t2)
    {
        int64_t res = 1LL * t1 * t2;
        return res % MOD;
    }
    int Abs(int tmp)
    {
        if(tmp < 0) return -tmp;
        return tmp;
    }
    int MASK(int j)
    {
        return (1 << j);
    }
    int BIT(int tmp, int j)
    {
        int x = tmp & MASK(j);
        if(x != 0) return 1;
        return 0;
    }
};
void __setup()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL); cout.tie(NULL);
//    freopen("in.txt", "r", stdin);
//    freopen("out.txt", "w", stdout);
}
int main()
{
    __setup();
    int t = 1;
//    cin >> t;
    for(int i = 1; i <= t; i++)
    {
        Solution().solve();
    }
    return 0;
}
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... |