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 ll long long
#define db double
#define pairll pair<ll,ll>
#define lpairll pair<ll,pairll>
#define repp(i,a,b) for (ll i = a; i <= b; i++)
#define repz(i,a,b) for (ll i = a; i < b; i++)
#define repm(i,a,b) for (ll i = a; i >= b; i--)
#define fr first
#define sc second
#define mp make_pair
#define pb push_back
const ll N = 1e5+5, MOD = 1e9+7, M = 2e2+5, LOG = 30;
ll tc = 1, n, m, dr[N], vr[N];
ll x, y, k, pr[N];
string s, s1, s2, ye = "YES", no = "NO";
vector<ll> tt, v;
pairll nx[N][31];
void input(){
  cin >> n >> m;
  repp(i,1,n) cin >> dr[i] >> vr[i];
}
void build(){
  repp(i,1,n){
    while(v.size() && dr[v.back()] < dr[i]){
      nx[v.back()][0] = mp(i,vr[v.back()]);
      //cout << v.back() << " " << i << endl;
      v.pop_back();
    }
    v.pb(i);
  }
  nx[0][0] = mp(0,0);
  while(v.size()){
    nx[v.back()][0] = mp(0,vr[v.back()]);
    v.pop_back();
  }
  // repp(i,1,n){
  //   cout << i << " " << "0" << " " << nx[i][0].fr << " " << nx[i][0].sc << endl;
  // }
  repp(bt,1,LOG){
    repp(i,1,n){
      //nx[i][bt] = nx[nx[i][bt]][bt];
      ll loc = nx[nx[i][bt-1].fr][bt-1].fr;
      ll cost = nx[i][bt-1].sc+nx[nx[i][bt-1].fr][bt-1].sc;
      nx[i][bt] = mp(loc,cost);
      //cout << i << " " << bt << " " << loc << " " << cost << endl;
    }
  }
}
void solve(){
  build();
  while(m--){
    cin >> x >> y;
    ll op = x;
    repm(bt,LOG,0){
      ll idx = nx[x][bt].fr, cost = nx[x][bt].sc;
      if (bt <= 30){
        //cout << bt << " " << x << " " << y << " " << idx << " " << cost << endl;
      }
      if (cost < y){
        x = idx;
        y -= cost;
        op = idx;
      }
    }
    cout << op << endl;
  }
}
int main(){
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
  //cin >> tc;
  while(tc--){
    input();
    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... |