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>
#define ll long long
#define vl vector<ll>
#define pb push_back
using namespace std;
const int big=1e9+7;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
// we will construct parent array by using minimum stack
ll n,q,i,cs,x,idx;
cin>>n>>q;
vl d(n+1),c(n+1),r(q+1),parent(n+1);
stack<ll> s;
for(i=1;i<=n;i++){
cin>>d[i]>>c[i];
while(s.size()>0&&d[i]>d[s.top()]){
parent[s.top()]=i;
s.pop();
}
s.push(i);
}
while(s.size()>0){
parent[s.top()]=0;
s.pop();
}
/*
for(i=1;i<=n;i++){
cout<<parent[i]<<' ';
}
cout<<endl;
*/
for(i=1;i<=q;i++){
cin>>idx>>x;
for(cs=idx;x>0&&cs!=0;cs=parent[cs]){
if(x-c[cs]<=0){
cout<<cs<<endl;break;
}
else{
x-=c[cs];
}
//cout<<cs<<' ';
}
if(cs==0){
cout<<0<<endl;
}
}
}
/*
6 5
4 10
6 8
3 5
4 14
10 9
4 20
1 25
6 30
5 8
3 13
2 8
*/
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |