이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pll pair<ll,ll>
#define pii pair<int,int>
#define fs first
#define sc second
#define tlll tuple<ll,ll,ll>
const int mxn = 1e5+10;
int N,Q;
vector<int> tree[mxn];
ll cap[mxn],dia[mxn];
vector<int> st;
int par[mxn][20];
ll sum[mxn][20];
void dfs(int now){
	sum[now][0] = cap[now];
	for(int i = 1;i<20;i++){
		par[now][i] = par[par[now][i-1]][i-1];
		sum[now][i] = sum[now][i-1]+sum[par[now][i-1]][i-1];
	}
	for(auto nxt:tree[now]){
		par[nxt][0] = now;
		dfs(nxt);
	}
	return;
}
ll calc(int now,int cnt){
	for(int i = 19;i>=0;i--){
		if(sum[now][i]<cnt){
			cnt -= sum[now][i];
			now = par[now][i];
		}
	}
	if(now == N+1)return 0;
	else return now;
}
int main(){
	ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	cin>>N>>Q;
	cap[N+1] = dia[N+1] = 1e9+10;
	for(int i = 1;i<=N;i++)cin>>dia[i]>>cap[i];
	for(int i = 1;i<=N+1;i++){
		while(!st.empty()&&dia[st.back()]<dia[i]){
			tree[i].push_back(st.back());
			st.pop_back();
		}
		st.push_back(i);
	}
	par[N+1][0] = N+1;
	dfs(N+1);
	while(Q--){
		int a,b;
		cin>>a>>b;
		cout<<calc(a,b)<<'\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... |