이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <iostream>
#include <algorithm>
#include <vector>
#include <set>
#include <cstring>
#include <queue>
#include <map>
#include <cmath>
#include <iomanip>
#include <stack>
using namespace std;
#define ff first
#define sc second
#define pb push_back
#define ll long long
#define pll pair<ll, ll>
#define pii pair <int, int>
#define ull unsigned long long
// #define int long long
// #define int unsigned long long
const ll inf = 1e9 + 7;
const ll weirdMod = 998244353;
int n, q;
ll sum[100005][17];
int par[100005][17];
int binDrop(int v, int sm) {
    for (int i = 16; i >= 0; i--) {
        if (par[v][i] != -1 && sum[v][i] < sm) {
            sm -= sum[v][i]; v = par[v][i];
        }
    }
    return v;
}
void solve() {
    cin >> n >> q;
    vector<pii> arr(n + 1);
    memset(par, -1, sizeof par);
    for (int i = 1; i <= n; i++)
        cin >> arr[i].ff >> arr[i].sc;
    stack<int> stk;
    for (int i = 1; i <= n; i++) {
        while (!stk.empty() && arr[stk.top()].ff < arr[i].ff) {
            par[stk.top()][0] = i; sum[stk.top()][0] = arr[stk.top()].sc; stk.pop();
        }
        stk.push(i);
    }
    while (!stk.empty()) {
        par[stk.top()][0] = 0;
        sum[stk.top()][0] = arr[stk.top()].sc;
        stk.pop();
    }
    for (int j = 1; j <= 16; j++) {
        for (int i = 1; i <= n; i++) {
            if (par[i][j - 1] != -1 && par[par[i][j - 1]][j - 1] != -1) {
                par[i][j] = par[par[i][j - 1]][j - 1];
                sum[i][j] = sum[i][j - 1] + sum[par[i][j - 1]][j - 1];
            }
        }
    }
    int d, v;
    while (q--) {
        cin >> d >> v;
        cout << binDrop(d, v) << '\n';
    }
}
int32_t main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr); cout.tie(nullptr);
    
    int t = 1;
    // cin >> t;
    while (t--) {
        solve();
        cout << '\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... |