이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
const int N = 1e6 + 1;
const int mod = 1e9 + 7;
template <typename T>
bool umx(T& a, T b) {
    return (a < b ? a = b, 1 : 0);
}
template <typename T>
bool umn(T& a, T b) {
    return (a > b ? a = b, 1 : 0);
}
int to[20][N];
ll s[20][N];
void solve () {
    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<int> bigr(n + 1);
    vector<int> ve;
    vector<ll> pref (n + 1);
    for (int i = 1; i <= n; ++i) {
        while ((int)ve.size() && d[ve.back()] < d[i]) {
            bigr[ve.back()] = i;
            ve.pop_back();
        }
        ve.push_back(i);
    }
    for (int i = n; i >= 1; --i) {
        to[0][i] = bigr[i];
        s[0][i] = c[bigr[i]];
        for (int l = 1; l < 20; ++l) {
            to[l][i] = to[l - 1][to[l - 1][i]];
            s[l][i] = s[l - 1][i] + s[l - 1][to[l - 1][i]];
        }
    }
    while (q--) {
        int r, v;
        cin >> r >> v;
        if (v <= c[r]) {
            cout << r << "\n";
            continue;
        }
        v -= c[r];
        for (int l = 19; l >= 0; --l) {
            if (s[l][r] < v) {
                v -= s[l][r];
                r = to[l][r];
            }
        }
        cout << to[0][r] << "\n";
    }
}
signed main() {
#ifdef LOCAL
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#else
    //    freopen ("input.txt", "r", stdin);
//    freopen ("output.txt", "w", stdout);
#endif
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    int T = 1;
    while (T--) solve();
    return 0;
}
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... |