Submission #838820

# Submission time Handle Problem Language Result Execution time Memory
838820 2023-08-27T19:29:31 Z VMaksimoski008 Fountain (eJOI20_fountain) C++17
100 / 100
413 ms 47776 KB
#include <bits/stdc++.h>
#define all(x) x.begin(), x.end()

using namespace std;

using ll = long long;
using pii = pair<int, int>;

const int LOG = 20;

vector<vector<int> > up, graph;
vector<vector<ll> > sum;
vector<pii> v;

void dfs(int u, int p) {
    for(int i=1; i<LOG; i++) {
        up[u][i] = up[ up[u][i-1] ][i-1];
        sum[u][i] = 1ll * (sum[u][i-1] + sum[ up[u][i-1] ][i-1]);
    }

    for(int &next : graph[u]) {
        if(next == p) continue;
         up[next][0] = u;
         sum[next][0] = v[u].second;
         dfs(next, u);
    }
}

int jmp(int a, int b) {
    int currNode = a;
    int currLeft = b;

    currLeft -= v[a].second;

    for(int i=LOG-1; i>=0; i--) {
        if(sum[currNode][i] <= currLeft) {
            currLeft -= sum[currNode][i];
            currNode = up[currNode][i];
        }
    }

    if(currLeft <= 0) return currNode;
    return up[currNode][0];
}

int32_t main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    int n, q;
    cin >> n >> q;
    graph.resize(n+1);
    up.resize(n+1, vector<int>(LOG, 0));
    sum.resize(n+1, vector<ll>(LOG, 0));

    v.resize(n+1);
    v[0] = {1500000000, 1500000000};
    for(int i=1; i<=n; i++) {
        cin >> v[i].first >> v[i].second;
    }

    stack<pii> curr;
    for(int i=1; i<=n; i++) {
        while(!curr.empty() && curr.top().second < v[i].first) {
            graph[i].push_back(curr.top().first);
            graph[curr.top().first].push_back(i);
            curr.pop();
        }
        curr.push({ i, v[i].first });
    }

    while(!curr.empty()) {
        graph[0].push_back(curr.top().first);
        graph[curr.top().first].push_back(0);
        curr.pop();
    }

    dfs(0, 0);

    while(q--) {
        int a, b;
        cin >> a >> b;

        cout << jmp(a, b) << '\n';
    }

    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 260 KB Output is correct
2 Correct 1 ms 468 KB Output is correct
3 Correct 1 ms 468 KB Output is correct
4 Correct 1 ms 596 KB Output is correct
5 Correct 2 ms 724 KB Output is correct
6 Correct 1 ms 596 KB Output is correct
7 Correct 1 ms 596 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 247 ms 41232 KB Output is correct
2 Correct 274 ms 41212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 260 KB Output is correct
2 Correct 1 ms 468 KB Output is correct
3 Correct 1 ms 468 KB Output is correct
4 Correct 1 ms 596 KB Output is correct
5 Correct 2 ms 724 KB Output is correct
6 Correct 1 ms 596 KB Output is correct
7 Correct 1 ms 596 KB Output is correct
8 Correct 247 ms 41232 KB Output is correct
9 Correct 274 ms 41212 KB Output is correct
10 Correct 2 ms 728 KB Output is correct
11 Correct 82 ms 23892 KB Output is correct
12 Correct 413 ms 47776 KB Output is correct
13 Correct 221 ms 43684 KB Output is correct
14 Correct 174 ms 41960 KB Output is correct
15 Correct 143 ms 42720 KB Output is correct