#include <bits/stdc++.h>
#define int long long
using namespace std;
void solve() {
int n,q; cin >> n >> q;
map<int,int> go;
int a[n+5],b[n+5];
for(int i=1;i<=n;i++){
cin >> a[i] >> b[i];
}
for(int i=1;i<=n;i++){
if(go[i]!=0){
continue;
}
for(int j=i+1;j<=n;j++){
if(a[j]>a[i]){
go[i]=j;
break;
}
}
}
for(int i=1;i<=n;i++){
if(go[i]==0){
go[i]=n+1;
}
}
while(q--){
int x,y; cin >> x >> y;
int cur=y,curnode=x;
while(curnode<=n){
if(cur<=b[curnode]){
break;
}
else{
cur-=b[curnode];
curnode=go[curnode];
}
}
if(curnode==n+1){
curnode=0;
}
cout << curnode << endl;
}
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
solve();
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... |