Submission #1316156

#TimeUsernameProblemLanguageResultExecution timeMemory
1316156nurdaeeFountain (eJOI20_fountain)C++20
100 / 100
212 ms36796 KiB
#include <bits/stdc++.h>

using namespace std;

#define nur ios::sync_with_stdio(false);cin.tie(nullptr);

#define all(v) v.begin(),v.end()

#define int long long 

#define pb push_back

const int N = 2e5, inf = 1e18;

void solve(){
    int n , q;
    cin >> n >> q;
    vector<int> v, d;
    v.push_back(0);
    d.push_back(0);
    for(int i = 0; i < n; i++){
        int r , vq;
        cin >> r >> vq;
        v.pb(r);
        d.pb(vq);
    }
    v[n + 1] = inf;
    stack<int> st;
    pair<int , int> up[n + 3][21]{};
    for( int j = 0; j < 21; ++j )
        up[n + 1][j] = {n + 1, inf};
    st.push(n + 1);
    for(int i = n; i >= 1; i--){
        while(!st.empty() && v[st.top()] <= v[i] ){
            st.pop();
        }
        up[i][0] = {st.top() , d[st.top()]};
        st.push(i);
    }
    for(int j = 1; j <= 20; j++){
        for(int i = 1; i <= n; i++){
            int vb = up[i][j - 1].first;
            up[i][j] = {up[vb][j - 1].first , min(inf, up[i][j - 1].second + up[vb][j - 1].second)};
        }
    }
    while(q--){
        int id , l;
        cin >> id >> l;
        if( l <= d[id] )
        {
            cout <<id <<'\n';
            continue;
        }
        l -= d[id];
        for(int i = 20; i >= 0; i--){
            if ( l > up[id][i].second ){
                l -= up[id][i].second;
                id = up[id][i].first;
            }
        }
        cout <<up[id][0].first % (n + 1) <<'\n';
    }
} 


signed main() {
    nur 
    int t = 1;
    // cin >> t;
    while(t--) solve();
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...