Submission #1340875

#TimeUsernameProblemLanguageResultExecution timeMemory
1340875kawhietFountain (eJOI20_fountain)C++20
100 / 100
217 ms39936 KiB
#include <bits/stdc++.h>
using namespace std;

#define int long long

constexpr int LOG = 18; 

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int n, q;
    cin >> n >> q;
    vector<int> d(n + 1), c(n + 1);
    for (int i = 1; i <= n; i++) {
        cin >> d[i] >> c[i];
    }
    vector<vector<int>> up(n + 1, vector<int>(LOG, 0));
    vector<vector<int>> s(n + 1, vector<int>(LOG, 0));
    stack<int> st;
    for (int i = n; i >= 1; i--) {
        while (!st.empty() && d[st.top()] <= d[i]) {
            st.pop();
        }
        if (!st.empty()) {
            up[i][0] = st.top();
        } else {
            up[i][0] = 0; 
        }
        s[i][0] = c[i];
        st.push(i);
    }
    for (int j = 1; j < LOG; j++) {
        for (int i = 1; i <= n; i++) {
            int k = up[i][j - 1];
            up[i][j] = up[k][j - 1];
            s[i][j] = s[i][j - 1] + s[k][j - 1];
        }
    }
    while (q--) {
        int r, v;
        cin >> r >> v;
        int k = r;
        for (int j = LOG - 1; j >= 0; j--) {
            if (up[k][j] != 0 && v > s[k][j]) {
                v -= s[k][j];
                k = up[k][j];
            }
        }
        if (v > s[k][0]) {
            cout << 0 << "\n"; 
        } else {
            cout << k << "\n"; 
        }
    }
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...