이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define pii pair<int,int>
using namespace __gnu_pbds;
using namespace std;
#define pb push_back
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define int long long
#define f first
#define s second
#define pii pair<int,int>
template<class T>bool umax(T &a,T b){if(a<b){a=b;return true;}return false;}
template<class T>bool umin(T &a,T b){if(b<a){a=b;return true;}return false;}
typedef tree<int, null_type, less_equal<int>, rb_tree_tag,
	tree_order_statistics_node_update> ordered_set;
const int mod= 1e17 +7;
const int N=1e5*4;
int binpow (int a, int n) {
	if (n == 0)
		return 1;
	if (n % 2 == 1)
		return binpow (a, n-1) * a;
	else {
		int b = binpow (a, n/2);
		return b * b;
	}
}
vector<pii>v;
int n, q, timer;
int tin[N], tout[N];
int sum[N];
int up[20][N];
vector <int> g[N];
void dfs(int v, int pr) {
    tin[v] = ++timer;
    up[0][v] = pr;
    for (int i = 1; i <= 17; i++) {
        up[i][v] = up[i - 1][ up[i - 1][v] ];
    }
    for (int to : g[v]) {
        if (to == pr) continue;
        dfs(to, v);
    }
    tout[v] = timer;
}
int ind;
bool upper(int a, int b) {
    //true up
    if(sum[ind] - sum[up[0][a]] >= b)return true;
    else return false;
    
}
int lca(int a, int b) {
    
    if(sum[a]-sum[up[0][a]]>=b)return a;
    
    for (int i = 17; i >= 0; i--) {
        if (!upper(up[i][a], b)) {
            a = up[i][a];
        }
    }
    return up[0][a];
}
void dfs1(int x,int pr){
	sum[x] += v[x].s;
	
	for(auto to:g[x]){
		if(to==pr)continue;
	
		sum[to] = sum[x];
		
		dfs1(to,x);
		
	}
}
	
	
	
	
void solve(){
	
	cin>>n>>q;
	v.pb({0,mod});
	for(int i = 1;i <= n;i++){
		int a,b;
		cin>>a>>b;
		v.pb({a,b});
	}
	stack<int>s;
	for(int i=n;i>0;--i)
	{	
		while(!s.empty() and v[s.top()].f<=v[i].f)
		{
			s.pop();
		}
		if(s.empty())
		{
			g[0].pb(i);
		}
		else
		{
			g[s.top()].pb(i);
		}
		s.push(i);
	}
	
	dfs(0,0);
	dfs1(0,-1);
	
	
	while(q--){
		int a,b;
		cin>>a>>b;
		//~ cout<<sum[a]<<" "<<sum[up[0][a]]<<" ";
		ind = a;
		cout<<lca(a,b)<<"\n";
	}
	
	
	
	
}
 signed main()
{
//	freopen("seq.in", "r", stdin);
//  freopen("seq.out", "w", stdout);
	ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL);
	int tt=1;//cin>>tt;
	while(tt--)solve();
}
	
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... |