#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 |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
2 ms |
340 KB |
Output is correct |
4 |
Correct |
3 ms |
340 KB |
Output is correct |
5 |
Correct |
5 ms |
340 KB |
Output is correct |
6 |
Correct |
4 ms |
340 KB |
Output is correct |
7 |
Correct |
3 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1556 ms |
5736 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
2 ms |
340 KB |
Output is correct |
4 |
Correct |
3 ms |
340 KB |
Output is correct |
5 |
Correct |
5 ms |
340 KB |
Output is correct |
6 |
Correct |
4 ms |
340 KB |
Output is correct |
7 |
Correct |
3 ms |
340 KB |
Output is correct |
8 |
Execution timed out |
1556 ms |
5736 KB |
Time limit exceeded |
9 |
Halted |
0 ms |
0 KB |
- |