Submission #869728

#TimeUsernameProblemLanguageResultExecution timeMemory
869728riariti새로운 문제 (POI11_met)C++17
74 / 100
2392 ms58928 KiB
#include <bits/stdc++.h>

#ifdef local
#include "../../local.hpp"
#else
#define dbg(...)
#endif

using i64 = long long;

int n, m;

std::vector<int> tree;
void add(int x, i64 val) {
    while (x <= m) {
        tree[x] += val;
        x += (x & -x);
    }
}
i64 sum(int x) {
    i64 s = 0;
    while (x > 0) {
        s += tree[x];
        x -= (x & -x);
    }
    return s;
}

int32_t main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr), std::cout.tie(nullptr);

#ifdef local
    freopen("program.in", "r", stdin);
    freopen("program.out", "w", stdout);
    freopen("program.err", "w", stderr);
#endif

    std::cin >> n >> m;
    std::vector<int> o(m + 1);
    for (int i = 1; i <= m; i++) {
        std::cin >> o[i];
    }
    std::vector<int> p(n + 1);
    for (int i = 1; i <= n; i++) {
        std::cin >> p[i];
    }
    int k;
    std::cin >> k;
    std::vector<int> l(k + 1), r(k + 1), a(k + 1);
    for (int i = 1; i <= k; i++) {
        std::cin >> l[i] >> r[i] >> a[i];
    }

    dbg(n, m);
    dbg(o);
    dbg(p);
    dbg(k);
    dbg(l, r, a);

    std::map<int, std::vector<int>> ow;
    for (int i = 1; i <= m; i++) {
        ow[o[i]].push_back(i);
    }
    std::vector<int> lo(n + 1, 0 + 1), hi(n + 1, k + 1);
    dbg(lo, hi);

    tree.assign(m + 1, 0);
    dbg(tree);

    bool chk = true;
    std::map<int, std::vector<int>> wrk;
    while (chk) {
        chk = false;

        tree.assign(m + 1, 0);
        dbg(tree);

        for (int i = 1; i <= n; i++) {
            if (lo[i] == hi[i]) {
                continue;
            }

            int m = lo[i] + (hi[i] - lo[i]) / 2;
            dbg(m);
            wrk[m].push_back(i);
        }

        for (int q = 1; q <= k; q++) {
            if (l[q] <= r[q]) {
                add(l[q], a[q]);
                add(r[q] + 1, -a[q]);
            } else {
                add(1, a[q]);
                add(r[q] + 1, -a[q]);
                add(l[q], a[q]);
            }

            dbg(tree);

            while (!wrk[q].empty()) {
                chk = true;

                auto x = wrk[q].back();
                wrk[q].pop_back();

                i64 su = 0;
                for (auto pls : ow[x]) {
                    su += sum(pls);

                    if (su >= p[x]) {
                        break;
                    }
                }
                dbg(su);

                if (su >= p[x]) {
                    hi[x] = q;
                } else {
                    lo[x] = q + 1;
                }
            }
        }
    }

    for (int i = 1; i <= n; i++) {
        if (lo[i] <= k) {
            std::cout << lo[i] << "\n";
        } else {
            std::cout << "NIE\n";
        }
    }

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