#include <bits/stdc++.h>
#define int long long
#define mod 1000000007
#define endl '\n'
using namespace std;
signed main(){
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
int n,q;
cin>>n>>q;
pair<int,int>pr[n];
int pre[n];
bool bl=true;
for(int i=0;i<n;i++){
cin>>pr[i].first>>pr[i].second;
if(i==0)pre[i]=pr[i].second;
else pre[i]=pre[i-1]+pr[i].second;
if(i>0&&pr[i].first<=pr[i-1].first)bl=false;
}
while(q--){
int r,v;
cin>>r>>v;
r--;
if(bl){
int k=lower_bound(pre,pre+n,v+(r==0?0:pre[r-1]))-pre;
cout<<(k+1)%(n+1)<<endl;
}
else{
for(int i=r;i<n;i++){
if(pr[i].first>pr[r].first||i==r){v-=pr[i].second;r=i;}
if(v<=0){cout<<i+1<<endl;break;}
if(i==n-1)cout<<0<<endl;
}
}
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
204 KB |
Output is correct |
2 |
Correct |
2 ms |
204 KB |
Output is correct |
3 |
Correct |
2 ms |
204 KB |
Output is correct |
4 |
Correct |
2 ms |
332 KB |
Output is correct |
5 |
Correct |
3 ms |
332 KB |
Output is correct |
6 |
Correct |
3 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
79 ms |
3200 KB |
Output is correct |
2 |
Correct |
119 ms |
3132 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
204 KB |
Output is correct |
2 |
Correct |
2 ms |
204 KB |
Output is correct |
3 |
Correct |
2 ms |
204 KB |
Output is correct |
4 |
Correct |
2 ms |
332 KB |
Output is correct |
5 |
Correct |
3 ms |
332 KB |
Output is correct |
6 |
Correct |
3 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
79 ms |
3200 KB |
Output is correct |
9 |
Correct |
119 ms |
3132 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Execution timed out |
1587 ms |
1676 KB |
Time limit exceeded |
12 |
Halted |
0 ms |
0 KB |
- |