Submission #1353966

#TimeUsernameProblemLanguageResultExecution timeMemory
1353966waygonzFoehn Phenomena (JOI17_foehn_phenomena)C++20
100 / 100
59 ms7152 KiB
#include <bits/stdc++.h>
#define int long long

using namespace std;

int n, q, s, t;
int sum = 0;

struct FenwickTree {
    int N;
    vector<int> fw;
    void update(int x, int v) {
        x++;
        while (x <= N) fw[x] += v, x += x & -x;
    }
    int query(int x) {
        x++;
        int res = 0;
        while (x > 0) res += fw[x], x -= x & -x;
        return res;
    }
    FenwickTree(int sz) {
        N = sz;
        fw.assign(N+1, 0);
    }
};

int32_t main() {
    ios_base::sync_with_stdio(0); cin.tie(0);
    cin >> n >> q >> s >> t;
    s = -s;
    struct FenwickTree T = FenwickTree(n+1);
    vector<int> a(n+1);
    for (int i = 0; i <= n; i++) {
        cin >> a[i];
        T.update(i, a[i]), T.update(i+1, -a[i]);
        if (i == 0) continue;
        if (a[i-1] < a[i]) sum += (a[i] - a[i-1]) * s;
        else sum += (a[i-1] - a[i]) * t;
    }
    while (q--) {
        int l, r, k;
        cin >> l >> r >> k;
        if (l > 0) {
            int x = T.query(l-1);
            int y = T.query(l);
            if (x < y) sum -= s * (y - x);
            else sum -= t * (x - y);
            if (x < y + k) sum += s * (y + k - x);
            else sum += t * (x - y - k);
        }
        if (r < n) {
            int x = T.query(r);
            int y = T.query(r+1);
            if (x < y) sum -= s * (y - x);
            else sum -= t * (x - y);
            if (x + k < y) sum += s * (y - x - k);
            else sum += t * (x + k - y);
        }
        T.update(l, k);
        T.update(r+1, -k);
        cout << sum << '\n';
    }
}
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...