#include<bits/stdc++.h>
using namespace std;
int capacity[100005], diameter[100005], jump[100005][20], sum[100005][20];
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
int n, q;
cin>>n>>q;
for(int i = 1; i <= n; i++) cin>>diameter[i]>>capacity[i];
n++; diameter[n] = capacity[n] = 1e9;
stack<int> last;
for(int i = n; i >= 1; i--){
while(last.size() > 0 && diameter[last.top()] <= diameter[i]) last.pop();
if(last.size() > 0) jump[i][0] = last.top();
else jump[i][0] = 0;
sum[i][0] = capacity[i];
last.push(i);
//cout<<"A"<<i<<" "<<jump[i][0]<<endl;
}
//Preprocess
for(int j = 1; j <= 19; j++){
for(int i = 1; i <= n; i++){
jump[i][j] = jump[jump[i][j-1]][j-1];
sum[i][j] = sum[i][j-1] + sum[jump[i][j-1]][j-1];
}
}
for(int test = 0; test < q; test++){
int v, r;
cin>>r>>v;
for(int j = 19; j >= 0; j--) if(sum[r][j] < v){
v -= sum[r][j]; r = jump[r][j];
//cout<<"A"<<r<<" "<<v<<endl;
}
//cout<<"A"<<r<<" "<<n<<endl;
if(r == n) cout<<0<<'\n';
else cout<<r<<'\n';
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |