이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <iostream>
#include <stack>
using namespace std;
using PII = pair<int, int>;
static constexpr int NMAX = (int)(1e5 + 1), LOGMAX = 17;
int N, Q;
int D[NMAX], C[NMAX];
PII Next[LOGMAX][NMAX];
static inline void Read()
{
    ios_base ::sync_with_stdio(false);
    cin.tie(nullptr);
    cin >> N >> Q;
    for (int i = 1; i <= N; ++i)
        cin >> D[i] >> C[i], Next[0][i] = {0, 0};
    return;
}
static inline void Get_level_0()
{
    stack<int> S;
    for (int i = 1; i <= N; ++i)
    {
        while (!S.empty() && D[i] > D[S.top()])
            Next[0][S.top()].first = i, Next[0][S.top()].second += C[i], S.pop();
        S.push(i);
    }
    return;
}
static inline void Precompute()
{
    for (int i = 1; (1 << i) <= N; ++i)
        for (int j = 1; j <= N; ++j)
            Next[i][j] = {Next[i - 1][Next[i - 1][j].first].first, Next[i - 1][j].second + Next[i - 1][Next[i - 1][j].first].second};
    return;
}
static inline void Test_Case()
{
    int node = 0, target = 0;
    cin >> node >> target;
    if (C[node] >= target)
    {
        cout << node << '\n';
        return;
    }
    if (!Next[0][node].first)
    {
        cout << 0 << '\n';
        return;
    }
    target -= C[node];
    for (int i = 16; i >= 0; --i)
        if (Next[i][node].first)
            if (target - Next[i][node].second >= 0)
                target -= Next[i][node].second, node = Next[i][node].first;
    if (target == 0)
        cout << node;
    else
        cout << Next[0][node].first;
    cout << '\n';
    return;
}
static inline void Solve()
{
    for (int q = 1; q <= Q; ++q)
        Test_Case();
    return;
}
int main()
{
    Read();
    Get_level_0();
    Precompute();
    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... |