This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define int long long
int rad[500001],cap[500001];
array <int,2> sum[500001][21];
vector <int> v;
signed main()
{
int n,q;
cin>>n>>q;
for(int i=1;i<=n;i++) cin>>rad[i]>>cap[i];
for(int i=n;i>=1;i--){
while(v.size()!=0&&rad[v.back()]<=rad[i]) v.pop_back();
if(v.size()!=0) sum[i][0]={cap[i],v.back()};
else sum[i][0]={cap[i],0};
v.push_back(i);
}
for(int k=1;k<=20;k++){
for(int i=1;i<=n;i++){
sum[i][k][1]=sum[sum[i][k-1][1]][k-1][1];
sum[i][k][0]=sum[i][k-1][0]+sum[sum[i][k-1][1]][k-1][0];
}
}
while(q--){
int i,val;
cin>>i>>val;
for(int k=20;k>=0;k--){
if(val-sum[i][k][0]>0){
val-=sum[i][k][0];
i=sum[i][k][1];
}
}
cout<<i<<endl;
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |