#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define vl vector<ll>
#define endl "\n"
#define INF 0x3F3F3F3F
using namespace std;
int main(){
ll n,q,r,x,i;
cin>>n>>q;
vl d(n+1),c(n+1),pre(n+2),parent(n+1);
pre[0]=0;
bool as=1;
stack<ll> s;
for(i=1;i<=n;i++){
cin>>d[i]>>c[i];
if(i>1){if(d[i]<=d[i-1]){as=0;}}
pre[i]=pre[i-1]+c[i];
while(s.size()>0&&d[i]>d[s.top()]){
parent[s.top()]=i;
s.pop();
}
s.push(i);
}
if(as){
while(q--){
cin>>r>>x;
ll nw=x+pre[r-1];
ll idx=upper_bound(pre.begin(),pre.end(),nw)-pre.begin();
if(pre[idx-1]==nw){
cout<<idx-1<<endl;
}
else if(idx==(n+1)){
cout<<0<<endl;
}
else{
cout<<idx<<endl;
}
}
}
else{
ll idx,cs;
while(s.size()>0){
parent[s.top()]=0;
s.pop();
}
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;
}
}
}
}
/*
5 10
3 5
5 7
7 9
9 11
11 13
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
2 ms |
212 KB |
Output is correct |
4 |
Correct |
3 ms |
212 KB |
Output is correct |
5 |
Correct |
6 ms |
336 KB |
Output is correct |
6 |
Correct |
5 ms |
212 KB |
Output is correct |
7 |
Correct |
4 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
279 ms |
3920 KB |
Output is correct |
2 |
Correct |
313 ms |
3808 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
2 ms |
212 KB |
Output is correct |
4 |
Correct |
3 ms |
212 KB |
Output is correct |
5 |
Correct |
6 ms |
336 KB |
Output is correct |
6 |
Correct |
5 ms |
212 KB |
Output is correct |
7 |
Correct |
4 ms |
212 KB |
Output is correct |
8 |
Correct |
279 ms |
3920 KB |
Output is correct |
9 |
Correct |
313 ms |
3808 KB |
Output is correct |
10 |
Correct |
5 ms |
340 KB |
Output is correct |
11 |
Correct |
842 ms |
4424 KB |
Output is correct |
12 |
Incorrect |
421 ms |
8744 KB |
Output isn't correct |
13 |
Halted |
0 ms |
0 KB |
- |