Submission #942072

#TimeUsernameProblemLanguageResultExecution timeMemory
942072PanndaAnts and Sugar (JOI22_sugar)C++17
6 / 100
4104 ms126220 KiB
#include <bits/stdc++.h>
using namespace std;

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

    const int CL = 0, CR = 1e9 + 1;
    const long long INF = 1e18;

    struct Node {
        int ln = 0, rn = 0;
        long long ant = 0, sugar = 0, sugar2 = 0;
        long long lazy_sugar = 0, lazy_sugar2 = 0;
        vector<vector<long long>> mn = { { 0, 0 }, { 0, 0 } };
        Node operator+(Node b) {
            Node a = *this;
            Node res;
            for (int i = 0; i < 2; i++) {
                for (int j = 0; j < 2; j++) {
                    res.mn[i][j] = INF;
                    res.mn[i][j] = min(res.mn[i][j], a.mn[i][0] + b.mn[0][j]);
                    res.mn[i][j] = min(res.mn[i][j], a.mn[i][0] + b.mn[1][j]);
                    res.mn[i][j] = min(res.mn[i][j], a.mn[i][1] + b.mn[0][j]);
                    res.mn[i][j] = min(res.mn[i][j], a.mn[i][1] + b.mn[1][j] - a.sugar2);
                }
            }
            res.sugar2 = b.sugar2;
            return res;
        }
    };

    vector<Node> nodes(2);
    nodes.reserve(10000000);

    auto apply = [&](int &idx, long long delta_sugar, long long delta_sugar2) {
        if (idx == 0) {
            idx = nodes.size();
            nodes.emplace_back();
        }
        nodes[idx].sugar += delta_sugar;
        nodes[idx].sugar2 += delta_sugar2;
        nodes[idx].lazy_sugar += delta_sugar;
        nodes[idx].lazy_sugar2 += delta_sugar2;
        nodes[idx].mn[0][0] = min(0LL, nodes[idx].mn[0][0] + delta_sugar);
        nodes[idx].mn[0][1] += delta_sugar;
        nodes[idx].mn[1][0] += delta_sugar;
        nodes[idx].mn[1][1] += delta_sugar;
    };

    auto down = [&](int &idx) {
        if (idx == 0) {
            idx = nodes.size();
            nodes.emplace_back();
        } else {
            Node goat = nodes[idx];
            apply(goat.ln, goat.lazy_sugar, goat.lazy_sugar2);
            apply(goat.rn, goat.lazy_sugar, goat.lazy_sugar2);
            goat.lazy_sugar = 0;
            goat.lazy_sugar2 = 0;
            nodes[idx] = goat;
        }
    };

    auto addAnt = [&](int x, int delta) -> void {
        auto dfs = [&](auto self, int &idx, int l, int r) -> void {
            if (l + 1 == r) {
                if (idx == 0) {
                    idx = nodes.size();
                    nodes.emplace_back();
                }
                nodes[idx].ant += delta;
                nodes[idx].mn[0][0] = 0;
                nodes[idx].mn[0][1] = nodes[idx].mn[1][0] = INF;
                nodes[idx].mn[1][1] = - nodes[idx].ant + nodes[idx].sugar;
            } else {
                down(idx);
                int m = (l + r) >> 1;
                Node goat = nodes[idx];
                if (x < m) self(self, goat.ln, l, m);
                else self(self, goat.rn, m, r);
                int ln = goat.ln, rn = goat.rn;
                goat = nodes[goat.ln] + nodes[goat.rn];
                goat.ln = ln;
                goat.rn = rn;
                nodes[idx] = goat;
            }
//            cout << idx << ' ' << l << ' ' << r << ": " << nodes[idx].ln << ' ' << nodes[idx].rn << ' ' << nodes[idx].ant << ' ' << nodes[idx].sugar << '\n' << "       " <<
//                                  nodes[idx].mn[0][0] << ' ' << nodes[idx].mn[0][1] << ' ' << nodes[idx].mn[1][0] << ' ' << nodes[idx].mn[1][1] << '\n';
        };
        int goat = 1;
        dfs(dfs, goat, CL, CR);
    };

    auto addSugar = [&](int ql, int qr, long long delta_sugar, long long delta_sugar2) -> void {
        auto dfs = [&](auto self, int &idx, int l, int r) -> void {
            if (r <= ql || qr <= l) return;
            if (ql <= l && r <= qr) return apply(idx, delta_sugar, delta_sugar2);
            down(idx);
            int m = (l + r) >> 1;
            Node goat = nodes[idx];
            self(self, goat.ln, l, m);
            self(self, goat.rn, m, r);
            int ln = goat.ln, rn = goat.rn;
            goat = nodes[goat.ln] + nodes[goat.rn];
            goat.ln = ln;
            goat.rn = rn;
            nodes[idx] = goat;
        };
        int goat = 1;
        dfs(dfs, goat, CL, CR);
    };

    auto query = [&]() -> long long {
        long long res = INF;
        for (int i = 0; i < 2; i++) {
            for (int j = 0; j < 2; j++) {
                res = min(res, nodes[1].mn[i][j]);
            }
        }
        return res;
    };

    int q, radius;
    cin >> q >> radius;
    long long total = 0;
    while (q--) {
        int type, x, c;
        cin >> type >> x >> c;
        if (type == 1) {
            addAnt(x, +c);
            total += c;
        }
        if (type == 2) {
            addSugar(x - radius, x + radius + 1, +c, 0);
            addSugar(x - radius, x + radius, 0, +c);
        }
        cout << total + query() << '\n';
    }
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...