Submission #465511

#TimeUsernameProblemLanguageResultExecution timeMemory
465511Sench2006Fountain (eJOI20_fountain)C++14
100 / 100
331 ms25696 KiB
#include <bits/stdc++.h>
#include <unordered_map>
using namespace std;

#define ll long long
#define ar array

#define f first
#define s second
#define mpr make_pair
#define pb push_back
#define lb lower_bound
#define ub upper_bound

#define FOR(i, a, b) for(auto i = a; i < b; i++)
#define FORD(i, a, b) for(auto i = a - 1; i >= b; i--)
#define trav(x, v) for(auto x : v)

#define fastio ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define fileio freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);

// --------------- SOLUTION --------------- //

const int mxN = 1e5+5;
int n, q;
pair <int, int> in[mxN];
pair <int, int> ch[mxN][25];
vector <pair<int, int>> st;

void solve() {
    cin >> n >> q;
    FOR(i, 1, n + 1) {
        cin >> in[i].f >> in[i].s;
    }
    FORD(i, n + 1, 1) {
        while (!st.empty() && st.back().f <= in[i].f) st.pop_back();
        if (st.empty()) ch[i][0] = {0, in[i].s};
        else ch[i][0] = {st.back().s, in[i].s};
        st.pb({in[i].f, i});
    }
    FOR(j, 1, 20) {
        FOR(i, 1, n + 1) {
            ch[i][j] = {ch[ch[i][j - 1].f][j - 1].f, ch[i][j - 1].s + ch[ch[i][j - 1].f][j - 1].s};
        }
    }
    while (q--) {
        int r, v;
        cin >> r >> v;
        FORD(i, 20, 0) {
            if (v > ch[r][i].s) {
                v -= ch[r][i].s;
                r = ch[r][i].f;
            }
        }
        cout << r << "\n";
    }
}

int main() {
    fastio;
    // fileio;
    int tests = 1;
    // cin >> tests;
    while (tests--) {
        solve();
    }

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