Submission #1347224

#TimeUsernameProblemLanguageResultExecution timeMemory
1347224killerzaluuFountain (eJOI20_fountain)C++20
100 / 100
172 ms24296 KiB
#include <bits/stdc++.h>
using namespace std;

const int N = 100000 + 5;
const int LG = 18;

int n, q;
int d[N], c[N], to[N];
int up[LG][N];
long long sum[LG][N];

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    cin >> n >> q;

    for (int i = 1; i <= n; i++) {
        cin >> d[i] >> c[i];
    }

    stack<int> st;
    for (int i = n; i >= 1; i--) {
        while (!st.empty() && d[st.top()] <= d[i]) st.pop();
        if (st.empty()) to[i] = 0;
        else to[i] = st.top();
        st.push(i);
    }

    for (int i = 1; i <= n; i++) {
        up[0][i] = to[i];
        sum[0][i] = c[i];
    }

    for (int k = 1; k < LG; k++) {
        for (int i = 1; i <= n; i++) {
            int j = up[k - 1][i];
            up[k][i] = up[k - 1][j];
            sum[k][i] = sum[k - 1][i] + sum[k - 1][j];
        }
    }

    while (q--) {
        int r;
        long long v;
        cin >> r >> v;

        int cur = r;
        for (int k = LG - 1; k >= 0; k--) {
            if (cur != 0 && sum[k][cur] < v) {
                v -= sum[k][cur];
                cur = up[k][cur];
            }
        }

        cout << cur << '\n';
    }

    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...