#include <bits/stdc++.h>
#define int long long
using namespace std;
signed main(){
int n,q;
cin>>n>>q;
int koliko[n+1];
int staje[n+1];
for(int i=1;i<=n;i++)cin>>koliko[i]>>staje[i];
/*
while(q--){
int r,k;
cin>>r>>k;
vector<int>R;
int z=r;
R.push_back(r);
for(int i=z;i<n;i=z){
//cout<<i<<" ";
int ok=0;
for(int j=i+1;j<=n;j++){
if(koliko[i]<koliko[j]){
ok=1;
z=j;
R.push_back(j);
break;
}
}
if(!ok)break;
}
int ans=0;
for(auto it:R){
k-=staje[it];
if(k<=0){
ans=it;
break;
}
}
cout<<ans;
cout<<endl;
}
*/
int P[n+1];
for(int i=0;i<=n;i++)P[i]=0;
stack<int>s;
for(int i=n;i>=1;i--){
while(s.size()&&koliko[s.top()]<=koliko[i])s.pop();
if(s.size())P[i]=s.top();
s.push(i);
}
int dp[n+1][1001];
for(int i=0;i<=n;i++)for(int j=0;j<=1000;j++)dp[i][j]=1e18;
for(int i=1;i<=n;i++){
for(int j=1;j<=staje[i];j++)dp[i][j]=1;
}
for(int i=n-1;i>=1;i--){
if(!P[i])continue;
// dp[i][j]=dp[P[i]][j-staje[i]]+1;
for(int j=staje[i];j<=1000;j++){
dp[i][j]=min(dp[i][j],dp[P[i]][j-staje[i]]+1);
//cout<<i<<" "<<j<<" "<<P[i]<<" "<<j-staje[i];
//cout<<endl;
}
}
while(q--){
int r,k;
cin>>r>>k;
int ans=0;
while(1&&r){
k-=staje[r];
if(k<=0){
ans=r;
break;
}
r=P[r];
}
cout<<ans;
cout<<endl;
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
3 ms |
4308 KB |
Output is correct |
3 |
Correct |
4 ms |
5716 KB |
Output is correct |
4 |
Correct |
6 ms |
7892 KB |
Output is correct |
5 |
Correct |
8 ms |
8272 KB |
Output is correct |
6 |
Correct |
7 ms |
8148 KB |
Output is correct |
7 |
Correct |
7 ms |
8148 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
183 ms |
524288 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
3 ms |
4308 KB |
Output is correct |
3 |
Correct |
4 ms |
5716 KB |
Output is correct |
4 |
Correct |
6 ms |
7892 KB |
Output is correct |
5 |
Correct |
8 ms |
8272 KB |
Output is correct |
6 |
Correct |
7 ms |
8148 KB |
Output is correct |
7 |
Correct |
7 ms |
8148 KB |
Output is correct |
8 |
Runtime error |
183 ms |
524288 KB |
Execution killed with signal 9 |
9 |
Halted |
0 ms |
0 KB |
- |