Submission #1279311

#TimeUsernameProblemLanguageResultExecution timeMemory
1279311stathiskonsFountain (eJOI20_fountain)C++20
100 / 100
123 ms16596 KiB
// 

#include <bits/stdc++.h>
using namespace std;

using ll = long long;

void solve(void){
    int n, q;
    cin >> n >> q;

    vector<int> diam(n + 1);
    vector<int> cap(n + 1);
    for(int i = 1 ; i <= n ; i++) cin >> diam[i] >> cap[i];

    constexpr int LG = 18;
    vector<array<int, LG>> nxt(n + 1);
    vector<array<int, LG>> cost(n + 1);


    {
        stack<pair<int, int>> s;
        s.push({INT_MAX, 0});
        for(int i = n ; i >= 1 ; i--) {
            while(!s.empty() && s.top().first <= diam[i]) s.pop();
            
            nxt[i][0] = s.top().second;
            cost[i][0] = cap[i];
            
            s.push({diam[i], i});
        }
    }

    for(int j = 1 ; j < LG ; j++) {
        for(int i = 1 ; i <= n ; i++) {
            nxt[i][j] = nxt[ nxt[i][j-1] ][j-1];
            
            cost[i][j] = cost[i][j-1] + cost[ nxt[i][j-1] ][j-1];
        }
    }

    while(q--) {
        int i, L;
        cin >> i >> L;

        for(int j = LG - 1 ; j >= 0 ; j--) {
            if(cost[i][j] < L) {
                L -= cost[i][j];

                i = nxt[i][j];
            }
        }

        cout << i << "\n";
    }
}


int main(void) {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

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