이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
int main()
{
    int n, q;
    cin>>n>>q;
    int L=log2(n)+1;
    int d[n+1], c[n+1];
    c[0]=d[0]=1e9+1;
    pair<int, long long>up[n+1][L+1];
    for(int i=1; i<=n; i++)
    {
        cin>>d[i]>>c[i];
    }
    stack<pair<long long, long long>>s;
    s.push({c[0], 0});
    for(int i=n; i; i--)
    {
        while(d[i]>=s.top().first)
        {
            s.pop();
        }
        up[i][0].first=s.top().second;
        up[i][0].second=c[s.top().second];
        for(int j=1; j<=L; j++)
        {
            up[i][j].first=up[up[i][j-1].first][j-1].first;
            up[i][j].second=up[i][j-1].second+up[up[i][j-1].first][j-1].second;
        }
        s.push({d[i], i});
    }
    while(q--)
    {
        int idx;
        long long val;
        cin>>idx>>val;
        val-=c[idx];
        if(val<=0)
        {
            cout<<idx<<"\n";
            continue;
        }
        for(int i=L; i>=0; i--)
        {
            if(up[idx][i].second<val)
            {
                val-=up[idx][i].second;
                idx=up[idx][i].first;
            }
        }
        cout<<up[idx][0].first<<"\n";
    }
    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... |