제출 #1329465

#제출 시각아이디문제언어결과실행 시간메모리
1329465mfmme23Fountain (eJOI20_fountain)C++20
30 / 100
1595 ms2824 KiB
#include <bits/stdc++.h>
using namespace std;

const int MAXN = 100005;

int N, Q;
long long D[MAXN], C[MAXN];
int nextGreater[MAXN];

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];

    // Next Greater Diameter
    stack<int> st;
    for (int i = N; i >= 1; i--) {
        while (!st.empty() && D[st.top()] <= D[i])
            st.pop();

        nextGreater[i] = st.empty() ? 0 : st.top();
        st.push(i);
    }

    // Queries
    while (Q--) {
        int R;
        long long V;
        cin >> R >> V;

        int cur = R;

        while (cur != 0 && V > 0) {

            long long take = min(V, C[cur]);
            V -= take;

            if (V > 0)
                cur = nextGreater[cur];
            else
                break;
        }

        cout << cur << "\n";
    }

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