Submission #895807

# Submission time Handle Problem Language Result Execution time Memory
895807 2023-12-30T21:31:36 Z tvladm2009 Fountain (eJOI20_fountain) C++17
100 / 100
308 ms 40744 KB
#include <bits/stdc++.h>
#define int ll

using namespace std;

typedef long long ll;
const int LOG = 20;

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n, q;
    cin >> n >> q;
    vector<int> d(n + 2), c(n + 2);
    for (int i = 1; i <= n; ++i) cin >> d[i] >> c[i];
    vector<int> l(n + 1), stk;
    for (int i = n; i >= 1; --i) {
        while (!stk.empty() && d[i] >= d[stk.back()]) {
            stk.pop_back();
        }
        if (stk.empty()) {
            l[i] = 0;
        } else {
            l[i] = stk.back();
        }
        stk.push_back(i);
    }
    vector<vector<int>> anc(LOG, vector<int>(n + 1));
    vector<vector<int>> sum(LOG, vector<int>(n + 1, 0LL));
    for (int i = 1; i <= n; ++i) {
        anc[0][i] = l[i];
        sum[0][i] = c[l[i]];
    }
    for (int h = 1; h < LOG; ++h) {
        for (int i = 1; i <= n; ++i) {
            anc[h][i] = anc[h - 1][anc[h - 1][i]];
            sum[h][i] = sum[h - 1][i] + sum[h - 1][anc[h - 1][i]];
        }
    }
    while (q--) {
        int r, cap;
        cin >> r >> cap;
        if (c[r] >= cap) {
            cout << r << "\n";
            continue;
        }
        cap -= c[r];
        ll cur = 0;
        for (int k = LOG - 1; k >= 0; --k) {
            if (anc[k][r] != 0 && cur + sum[k][r] < cap) {
                cur += sum[k][r];
                r = anc[k][r];
            }
        }
        if (l[r] == n + 1) {
            cout << "0\n";
            continue;
        }
        cout << l[r] << "\n";
    }
    return 0;
}
/**
6 5
4 10
6 8
3 5
4 14
10 9
4 20
1 25
6 30
5 8
3 13
2 8
**/
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 ms 604 KB Output is correct
3 Correct 1 ms 604 KB Output is correct
4 Correct 1 ms 604 KB Output is correct
5 Correct 1 ms 848 KB Output is correct
6 Correct 1 ms 860 KB Output is correct
7 Correct 1 ms 604 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 179 ms 37592 KB Output is correct
2 Correct 189 ms 35064 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 ms 604 KB Output is correct
3 Correct 1 ms 604 KB Output is correct
4 Correct 1 ms 604 KB Output is correct
5 Correct 1 ms 848 KB Output is correct
6 Correct 1 ms 860 KB Output is correct
7 Correct 1 ms 604 KB Output is correct
8 Correct 179 ms 37592 KB Output is correct
9 Correct 189 ms 35064 KB Output is correct
10 Correct 1 ms 856 KB Output is correct
11 Correct 60 ms 21904 KB Output is correct
12 Correct 308 ms 40744 KB Output is correct
13 Correct 186 ms 39760 KB Output is correct
14 Correct 146 ms 39236 KB Output is correct
15 Correct 88 ms 39320 KB Output is correct