Submission #1322654

#TimeUsernameProblemLanguageResultExecution timeMemory
1322654Hamed_GhaffariFountain (eJOI20_fountain)C++20
100 / 100
130 ms23064 KiB
#include <bits/stdc++.h>
using namespace std;

using ll = long long;

const int MXN = 1e5+5;
const int LOG = 18;

int n, q, d[MXN], R[MXN][LOG];
ll sum[MXN][LOG];

int32_t main() {
    cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(0);
    cin >> n >> q;
    for(int i=1; i<=n; i++) cin >> d[i] >> sum[i][0];
    for(int i=n; i>=1; i--) 
        for(R[i][0]=i+1; R[i][0]<=n && d[R[i][0]]<=d[i]; R[i][0]=R[R[i][0]][0]);
    for(int i=n; i>=1; i--) {
        if(R[i][0]==n+1) R[i][0] = 0;
        for(int j=1; j<LOG; j++)
            R[i][j] = R[R[i][j-1]][j-1],
            sum[i][j] = sum[i][j-1] + sum[R[i][j-1]][j-1];
    }
    while(q--) {
        int r, v;
        cin >> r >> v;
        for(int i=LOG-1; i>=0; i--)
            if(sum[r][i]<v) {
                v -= sum[r][i];
                r = R[r][i];
            }
        cout << r << '\n';
    }
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...