This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define ff first
#define ss second
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define int long long
///*                                                    __                    __                        __                    */
///*        ======     _      /| /|  __   _            /   |  |   /|  |   @  |    |  |  | /   /| |\  | /   |  |  @ | /        */
///* \-       ||  |_| |_     / |/ | |  | |_  |-        |   |--|  /-|  |   |  \ \  |==|  |-   /=| | \ | |   |--|  | |-         */
///*          ||  | | |_    /     | |__|  _| |_        \__ |  | /  |  |__ |  __|  |  |  | \ /  | |  \| \__ |  |  | | \        */
///*  
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef vector<pii> vii;
typedef vector<vi> vv;
const int N = 3e5;
vi g[N];
vii v(N);
int up[20][N];
int pr[N];
void dfs(int pos, int pre){
	pr[pos] += v[pos].ss;
	up[0][pos] = pre;
	for(int i = 1; i < 18; i++){
		up[i][pos] = up[i-1][up[i-1][pos]];
	}
	for(int to:g[pos]){
		if(to == pre)continue;
		pr[to] = pr[pos];
		dfs(to, pos);
	}
}
int isnp;
bool upper(int x, int sum){
	if(pr[isnp] - pr[up[0][x]] >= sum)return true;
	return false;
}
int lca(int pos, int sum){
	if(pr[pos] - pr[up[0][pos]] >= sum)return pos;
	for(int i = 17; i >= 0; i--){
		if(!upper(up[i][pos], sum)){
			pos = up[i][pos];
		}
	}
	return up[0][pos];
}
void solve(){
	int n, q;
	cin >> n >> q;
	for(int i = 1; i <= n; i++){
		cin >> v[i].ff >> v[i].ss;
	}
	stack<int> s;
	for(int i = n; i > 0; i--){
		while(!s.empty() && v[s.top()].ff <= v[i].ff)s.pop();
		if(s.empty()){
			g[0].pb(i);
		}else{
			g[s.top()].pb(i);
		}
		s.push(i);
	}
	dfs(0, -1);
	while(q--){
		int x, sum;
		cin >> x >> sum;
		isnp = x;
		cout << lca(x, sum) << '\n';
	}
}
main(){
	ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
	int test = 1;
	//~ cin >> test;
	while(test--){
		solve();
	}
}
Compilation message (stderr)
fountain.cpp:73:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   73 | main(){
      | ^~~~| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... |