#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 klk=dp[r][k];
if(klk==1e18){
cout<<0;
cout<<endl;
continue;
}
klk--;
//cout<<klk<<" ";
while(klk--){
//cout<<P[r]<<" ";
r=P[r];
}
cout<<r;
cout<<endl;
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Incorrect |
3 ms |
4376 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
193 ms |
524288 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Incorrect |
3 ms |
4376 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |