Submission #730717

#TimeUsernameProblemLanguageResultExecution timeMemory
730717murad_2005Fountain (eJOI20_fountain)C++14
100 / 100
365 ms20360 KiB
#include<bits/stdc++.h>
#define ll long long
#define ld long double
#define pii pair<int, int>
#define piii pair<int, pii>
#define pllll pair<ll, ll>
#define plli pair<ll, int>
#define vi vector<int>
#define vvi vector<vector<int>>
#define pb push_back
#define pf push_front
#define eb emplace_back
#define all(v) v.begin(), v.end()
#define size(v) v.size()
#define INF 2e9
#define f first
#define s second
#define ln "\n"

using namespace std;

const int up = 1e5 + 3;
const int LOG = 18;

int d[up], c[up];
int parent[LOG][up], volume[LOG][up];

int LCA(int R, int V){
    for(int i = LOG - 1; i >= 0; --i){
        if(volume[i][R] < V){
            V -= volume[i][R];
            R = parent[i][R];
        }
    }
    return R;
}

void solve(){
    int n, q;
    cin >> n >> q;
    for(int i = 1; i <= n; ++i){
        cin >> d[i] >> c[i];
    }
    d[n + 1] = INF;
    stack<int>st;
    for(int i = 1; i <= n + 1; ++i){
        while(!st.empty() && (d[st.top()] < d[i])){
            parent[0][st.top()] = i;
            volume[0][st.top()] = c[st.top()];
            st.pop();
        }
        st.push(i);
    }
    for(int i = 1; i < LOG; ++i){
        for(int v = 1; v <= n; ++v){
            parent[i][v] = parent[i - 1][parent[i - 1][v]];
            volume[i][v] = volume[i - 1][v] + volume[i - 1][parent[i - 1][v]];
        }
    }

    while(q--){
        int R, V;
        cin >> R >> V;
        int res = LCA(R, V);
        cout << (res = (res == n + 1) ? 0 : res) << "\n";
    }
}

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int t;
    t = 1;
    // cin >> t;
    while(t--){
        solve();
    }
    return 0;
}

#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...