Submission #853390

# Submission time Handle Problem Language Result Execution time Memory
853390 2023-09-24T09:18:40 Z overwatch9 Meteors (POI11_met) C++17
74 / 100
6000 ms 65536 KB
#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
using namespace std;
using ull = unsigned long long;
struct stree {
    #define lc v*2
    #define rc v*2+1

    vector <ull> tree;
    stree() {}
    stree (int s) {
        tree.resize(4*s);
    }

    void pushdown(int v) {
        // since we're doing point query, we don't need to maintain
        // a separate pd variable
        if (tree[v] == 0)
            return;
        tree[lc] += tree[v];
        tree[rc] += tree[v];
        tree[v] = 0;
    }
    void update(int v, int tl, int tr, int l, int r, ull val) {
        if (tl > r || tr < l)
            return;
        if (tl >= l && tr <= r) {
            tree[v] += val;
            return;
        }
        pushdown(v);
        int tm = (tl + tr) / 2;
        update(lc, tl, tm, l, r, val);
        update(rc, tm+1, tr, l, r, val);
    } 
    ull query(int v, int tl, int tr, int p) {
        if (tl > p || tr < p)
            return 0;
        if (tl == tr)
            return tree[v];
        pushdown(v);
        int tm = (tl + tr) / 2;
        return max(query(lc, tl, tm, p), query(rc, tm+1, tr, p));
    }

};
const int maxnmk = 3 * 1e5 + 1;
int n, m, k;
int owners[maxnmk], req[maxnmk], ans[maxnmk];
pair <pair <int, int>, ull> updates[maxnmk];
queue <pair <int, int>> q;
vector <vector <int>> subsets, stations;
int prev_mid = -1;
stree tree;
void search(int lo, int hi) {
    int mid = (lo + hi) / 2;
    if (prev_mid < mid) {
        for (int i = prev_mid+1; i <= mid; i++) {
            int l = updates[i].first.first;
            int r = updates[i].first.second;
            ull val = updates[i].second;
            if (l <= r)
                tree.update(1, 0, m, l, r, val);
            else {
                tree.update(1, 0, m, l, m-1, val);
                tree.update(1, 0, m, 0, r, val);
            }
        }
    } else {
        for (int i = mid+1; i <= prev_mid; i++) {
            int l = updates[i].first.first;
            int r = updates[i].first.second;
            ull val = updates[i].second;
            if (l <= r)
                tree.update(1, 0, m, l, r, -val);
            else {
                tree.update(1, 0, m, l, m-1, -val);
                tree.update(1, 0, m, 0, r, -val);
            }
        }
    }
    // check if any members have met their requirements
    bool added_lo = false, added_hi = false;
    for (auto i : subsets[mid]) {
        ull tot = 0;
        for (auto j : stations[i])
            tot += tree.query(1, 0, m, j);
        if (tot >= req[i]) {
            if (ans[i] == -1)
                ans[i] = mid;
            else
                ans[i] = min(ans[i], mid);
            if (lo != mid) {
                added_lo = true;
                subsets[(lo + mid) / 2].push_back(i);
            }
        } else {
            if (mid != hi) {
                added_hi = true;
                subsets[(mid+1 + hi) / 2].push_back(i);
            }
        }
    }
    if (added_lo)
        q.push({lo, mid});
    if (added_hi)
        q.push({mid+1, hi});
    prev_mid = mid;
}
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin >> n >> m;
    fill(ans, ans + n, -1);
    stations.resize(n);
    for (int i = 0; i < m; i++) {
        cin >> owners[i];
        owners[i]--;
        stations[owners[i]].push_back(i);
    }
    for (int i = 0; i < n; i++)
        cin >> req[i];
    cin >> k;
    for (int i = 0; i < k; i++) {
        int l, r;
        ull val;
        cin >> l >> r >> val;
        l--; r--;
        updates[i].first.first = l;
        updates[i].first.second = r;
        updates[i].second = val;
    }
    q.push({0, k-1});
    subsets.resize(k);
    for (int i = 0; i < n; i++)
        subsets[(k-1)/2].push_back(i);
    tree = stree(m+1);
    while (!q.empty()) {
        int l = q.front().first;
        int r = q.front().second;
        q.pop();
        search(l, r);
    }
    for (int i = 0; i < n; i++) {
        if (ans[i] == -1)
            cout << "NIE\n";
        else
            cout << ans[i]+1 << '\n';
    }
}

Compilation message

met.cpp: In function 'void search(int, int)':
met.cpp:90:17: warning: comparison of integer expressions of different signedness: 'ull' {aka 'long long unsigned int'} and 'int' [-Wsign-compare]
   90 |         if (tot >= req[i]) {
      |             ~~~~^~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 4 ms 2392 KB Output is correct
2 Correct 5 ms 2648 KB Output is correct
3 Correct 5 ms 2392 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 2392 KB Output is correct
2 Correct 4 ms 2392 KB Output is correct
3 Correct 5 ms 2648 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 445 ms 8324 KB Output is correct
2 Correct 593 ms 10776 KB Output is correct
3 Correct 554 ms 10564 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 530 ms 9508 KB Output is correct
2 Correct 571 ms 9908 KB Output is correct
3 Correct 645 ms 10844 KB Output is correct
4 Correct 98 ms 6400 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 210 ms 8784 KB Output is correct
2 Correct 322 ms 11516 KB Output is correct
3 Correct 104 ms 5972 KB Output is correct
4 Correct 525 ms 10800 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 210 ms 8016 KB Output is correct
2 Correct 509 ms 9552 KB Output is correct
3 Correct 344 ms 8272 KB Output is correct
4 Correct 604 ms 12108 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1646 ms 39112 KB Output is correct
2 Correct 643 ms 25704 KB Output is correct
3 Correct 841 ms 12880 KB Output is correct
4 Execution timed out 6032 ms 61756 KB Time limit exceeded
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1781 ms 37808 KB Output is correct
2 Correct 1242 ms 25560 KB Output is correct
3 Correct 70 ms 11400 KB Output is correct
4 Execution timed out 6025 ms 65536 KB Time limit exceeded
5 Halted 0 ms 0 KB -