이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define int long long
const int N = 1e5+3;
const int inf=1e9+10;
int p[N][17];
int sz[N][17];
signed main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int n, q;
    cin >> n>> q;
    pair<int, int> a[n+1];
    for(int i=1; i<= n; i++)cin >> a[i].F >> a[i].S;
    stack<pair<int, int>> st;
    st.push(mp(inf, 0));
    for(int i=n; i>=1; i--){
        while(st.top().F<=a[i].F){
            st.pop();
        }
        p[i][0]=st.top().S;
        sz[i][0]=a[i].S;
        st.push(mp(a[i].F, i));
    }
    for(int j=0; j<17; j++){
        sz[0][j]=inf;
    }
    for(int i=n; i>=1; i--){
        for(int j=1; j<17; j++){
            p[i][j]=p[p[i][j-1]][j-1];
            if(p[i][j]==0){
                sz[i][j]=inf;
            }
            else{
                sz[i][j]=sz[i][j-1]+sz[p[i][j-1]][j-1];
            }
        }
    }
    while(q--){
        int x, val;
        cin >> x >> val;
        int ans=x;
        for(int i=16; i>=0; i--){
            if(sz[ans][i]<val){
                val-=sz[ans][i];
                ans=p[ans][i];
            }
        }
        if(a[ans].S>=val){
            cout << ans << '\n';
        }
        else cout << p[ans][0] << '\n';
    }
}
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... |