Submission #1020204

# Submission time Handle Problem Language Result Execution time Memory
1020204 2024-07-11T16:51:20 Z toast12 Fountain (eJOI20_fountain) C++14
100 / 100
315 ms 30392 KB
#include <bits/stdc++.h>
using namespace std;

using ll = long long;

vector<vector<int>> jump;
vector<vector<ll>> weight;

ll find(int u, int k) {
    int v = u;
    ll res = 0;
    for (int e = 0; e <= 20; e++) {
        if (k & (1 << e)) {
            res += weight[e][v];
            v = jump[e][v];
        }
    }
    return res;
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(NULL);
    int n, q;
    cin >> n >> q;
    vector<int> d(n+1), c(n+1);
    c[0] = 1e9;
    for (int i = 1; i <= n; i++) {
        cin >> d[i] >> c[i];
    } 
    // d, c, i
    stack<int> s;
    vector<int> next(n+1);
    for (int i = 1; i <= n; i++) {
        while (!s.empty() && d[i] > d[s.top()]) {
            next[s.top()] = i;
            s.pop();
        }
        s.push(i);
    }
    jump.resize(20, vector<int>(n+1, -1));
    for (int i = 1; i <= n; i++) {
        jump[0][i] = next[i];
    }
    for (int e = 1; e < 20; e++) {
        for (int i = 1; i <= n; i++) {
            if (jump[e-1][i] != -1)
                jump[e][i] = jump[e-1][jump[e-1][i]];
        }
    }
    weight.resize(20, vector<ll>(n+1));
    for (int i = 1; i <= n; i++) {
        weight[0][i] = c[next[i]];
    }
    for (int e = 1; e < 20; e++) {
        for (int i = 1; i <= n; i++) {
            weight[e][i] = weight[e-1][i];
            if (jump[e-1][i] != -1)
                weight[e][i] += weight[e-1][jump[e-1][i]];
        }
    }
    while (q--) {
        int r, v;
        cin >> r >> v;
        if (v <= c[r]) {
            cout << r << '\n';
            continue;
        }
        v -= c[r];
        int lo = 0, hi = 0;
        for (int e = 0; e < 20; e++) {
            if (weight[e][r] >= v) {
                lo = 1 << (e-1);
                hi = 1 << e;
                break;
            }
        }
        while (lo < hi) {
            int mid = (lo+hi)/2;
            ll x = find(r, mid);
            if (x >= v) {
                hi = mid;
            }
            else {
                lo = mid+1;
            }
        }
        int u = r;
        for (int e = 0; e < 20; e++) {
            if (hi & (1 << e))
                u = jump[e][u];
        }
        cout << u << '\n';
    }
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 604 KB Output is correct
3 Correct 1 ms 600 KB Output is correct
4 Correct 1 ms 860 KB Output is correct
5 Correct 2 ms 748 KB Output is correct
6 Correct 1 ms 728 KB Output is correct
7 Correct 1 ms 600 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 201 ms 27380 KB Output is correct
2 Correct 213 ms 25932 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 604 KB Output is correct
3 Correct 1 ms 600 KB Output is correct
4 Correct 1 ms 860 KB Output is correct
5 Correct 2 ms 748 KB Output is correct
6 Correct 1 ms 728 KB Output is correct
7 Correct 1 ms 600 KB Output is correct
8 Correct 201 ms 27380 KB Output is correct
9 Correct 213 ms 25932 KB Output is correct
10 Correct 1 ms 604 KB Output is correct
11 Correct 106 ms 16472 KB Output is correct
12 Correct 304 ms 30392 KB Output is correct
13 Correct 315 ms 29880 KB Output is correct
14 Correct 90 ms 29116 KB Output is correct
15 Correct 67 ms 30000 KB Output is correct