Submission #1258085

#TimeUsernameProblemLanguageResultExecution timeMemory
1258085mardaFountain (eJOI20_fountain)C++20
100 / 100
379 ms33776 KiB
#include<bits/stdc++.h>

#define endl "\n"
#define int long long int
#define pb push_back
#define mp make_pair
#define MOD 998244353
#define mid (l+r+1)/2
#define ai2 array<int,2>

using namespace std;

void solve() {

    unsigned int n,q;
    cin >> n >> q;

    ai2* arr = new ai2[n];
    for(int i = 0; i < n; i++) cin >> arr[i][0] >> arr[i][1];

    int* parent = new int[n];
    stack<ai2> st;

    for(int i = 0; i < n; i++) {

        if(st.empty()) st.push({arr[i][0],i});
        else {
            if(arr[i][0] > st.top()[0]) {
                parent[st.top()[1]] = i;
                st.pop();
                i--;
            }
            else st.push({arr[i][0],i});
        }

    }

    while(!st.empty()) {
        parent[st.top()[1]] = -1;
        st.pop();
    }

    //for(int i = 0; i < n; i++) cout << parent[i] << " ";
    //cout << endl;

    ai2 ancestor[n][64-__builtin_clzll(n)+1];

    for(int i = 0; 1<<i < n; i++) {
        for(int j = 0; j < n; j++) {
            ancestor[j][i] = {-1,-1};
        }
    }

    for(int i = 0; (1<<i) < n; i++) {

        for(int j = 0; j < n; j++) {

            if(i == 0) {
                if(parent[j] == -1) ancestor[j][i] = {-1,-1};
                else ancestor[j][i] = {arr[j][1],parent[j]};
            }
            else {

                if(ancestor[j][i-1][1] == -1) ancestor[j][i] = {-1,-1};
                else if(ancestor[ancestor[j][i-1][1]][i-1][1] == -1) ancestor[j][i] = {-1,-1};
                else {
                    ancestor[j][i] = {ancestor[ancestor[j][i-1][1]][i-1][0]+ancestor[j][i-1][0], ancestor[ancestor[j][i-1][1]][i-1][1]};
                }

            }

        }

    }

    /*for(int i = 0; (1<<i) < n; i++) {

        for(int j = 0; j < n; j++) {

            cout << ancestor[j][i][0] << "," << ancestor[j][i][1] << ";";

        }

        cout << endl;

    }*/

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

        while(b > arr[a][1] && a != -1) {

            int l = 0, r = 64-__builtin_clzll(n)-1;

            while(l != r) {
                if(ancestor[a][mid][0] == -1) r = mid-1;
                else if(ancestor[a][mid][0] < b) l = mid;
                else r = mid-1;
            }

            //cout << a << " " << b << " " << l << " " << ancestor[a][l][1] << endl;
            b -= ancestor[a][l][0];
            a = ancestor[a][l][1];
            //cout << a << " " << b << " " << arr[a][1] << endl;
        }

        if(a == -1) cout << 0 << endl;
        else cout << a+1 << endl;

    }

}

int32_t main() {

    cin.tie(0); cout.tie(0);
    //ios::sync_with_stdio(false);
    
    int 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...