Submission #746188

#TimeUsernameProblemLanguageResultExecution timeMemory
746188piOOEConstellation 3 (JOI20_constellation3)C++17
100 / 100
183 ms30544 KiB
#include <bits/stdc++.h>

using namespace std;
using ll = long long;

struct DSU {
    vector<int> fa, sz;

    explicit DSU(int n) : fa(n), sz(n, 1) {
        iota(fa.begin(), fa.end(), 0);
    }
    DSU() = default;

    int leader(int x) {
        return fa[x] == x ? x : fa[x] = leader(fa[x]);
    }
};

template<typename T>
struct Fenwick {
    int n;
    vector<T> a;

    Fenwick() = default;

    explicit Fenwick(int n) : n(n), a(n + 1) {}

    void modify(int x, T v) {
        for (int i = x + 1; i <= n; i += i & -i) {
            a[i] += v;
        }
    }

    void modify(int l, int r, T v) {
        if (l >= r) return;
        modify(l, v), modify(r, -v);
    }

    T sum(int x) {
        T ans = 0;
        for (int i = x + 1; i > 0; i -= i & -i) {
            ans += a[i];
        }
        return ans;
    }
};

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int n;
    cin >> n;

    vector<int> a(n);
    vector<vector<int>> buildings(n + 1);
    vector<vector<pair<int, int>>> queries(n + 1);

    for (int i = 0; i < n; ++i) {
        cin >> a[i];
        buildings[a[i]].push_back(i + 1);
    }

    int m;
    cin >> m;

    for (int i = 0; i < m; ++i) {
        int x, y, c;
        cin >> x >> y >> c;
        queries[y].emplace_back(x, c);
    }

    DSU l(n + 2), r(n + 2);
    Fenwick<ll> fn(n + 2);

    ll ans = 0;

    for (int y = 1; y <= n; ++y) {
        for (auto [x, c] : queries[y]) {
            ans += c;
            ll diff = c - fn.sum(x);
            if (diff > 0) {
                ans -= diff;
                fn.modify(l.leader(x) + 1, r.leader(x), diff);
            }
        }

        for (int i : buildings[y]) {
            l.fa[i] = i - 1;
            r.fa[i] = i + 1;
        }
    }

    cout << ans << '\n';

    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...