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 <iostream>
#include <vector>
#include <stack>
using namespace std;
int bin_lift(int a,vector<vector<pair<int,long long>>>& p,int w){
for(int i=17;i>=0;i--){
if(p[a][i].second<w){
w-=p[a][i].second;
a=p[a][i].first;
}
}
//cout<<w<<' ';
return a;
}
void dfs(int a,int par,int w,vector<vector<pair<int,int>>>& g,vector<vector<pair<int,long long>>>& p){
//cout<<a<<' '<<par<<'\n';
p[a][0]={par,w};
for(int i=1;i<18;i++)p[a][i]={p[p[a][i-1].first][i-1].first,p[a][i-1].second+p[p[a][i-1].first][i-1].second};
for(auto x:g[a]){
dfs(x.first,a,x.second,g,p);
}
}
int main(){
int n,q,x,v;
cin>>n>>q;
vector<pair<int,int>> f(n);
vector<vector<pair<int,int>>> g(n+1,vector<pair<int,int>>());
vector<vector<pair<int,long long>>> par(n+1,vector<pair<int,long long>>(18));
for(int i=0;i<n;i++){
cin>>x>>v;
f[i]={x,v};
}
stack<pair<int,int>> st;
st.push({1e9+7,0});
for(int i=n-1;i>=0;i--){
while(st.top().first<=f[i].first)st.pop();
g[st.top().second].push_back({i+1,f[i].second});
st.push({f[i].first,i+1});
}
dfs(0,0,1e9+7,g,par);
/*for(int i=0;i<=n;i++){
cout<<i<<'\n';
for(int j=0;j<18;j++)cout<<par[i][j].first<<' ';
cout<<endl;
}*/
while(q--){
cin>>x>>v;
cout<<bin_lift(x,par,v)<<'\n';
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |