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>
#define int int64_t
using namespace std;
vector<vector<int>>graph, up, cap;
vector<int>c, d;
int timer = 0;
int l = 31;
void lift(int v, int par){
up[v][0] = par;
cap[v][0] = c[v];
for(int i = 1; i<=l; ++i){
up[v][i] = up[up[v][i-1]][i-1];
cap[v][i] = cap[v][i-1] + cap[up[v][i-1]][i-1];
}
for(auto u: graph[v]){
if(u != par) lift(u, v);
}
}
int query(int u, int x){
int cur = 0;
for(int i = l; i>=0; --i){
if(cap[u][i] + cur < x){
cur += cap[u][i];
u = up[u][i];
}
}
return u;
}
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n, q; cin>>n>>q;
up.assign(n+1, vector<int>(l+1));
cap.assign(n+1, vector<int>(l+1));
c.assign(n+1, 0);
d.assign(n+1, 0);
for(int i = 1; i<=n; ++i){
int a, b; cin>>a>>b;
c[i] = b;
d[i] = a;
}
stack<pair<int, int>>s;
graph.assign(n+1, vector<int>());
vector<int>p(n+1);
for(int i = 1; i<=n; ++i){
while(!s.empty() && s.top().first < d[i]){
p[s.top().second] = i;
s.pop();
}
s.push({d[i], i});
}
while(!s.empty()){
p[s.top().second] = 0;
s.pop();
}
for(int i = 1; i<=n; ++i){
graph[p[i]].push_back(i);
}
lift(0, 0);
for(int i = 0; i<q; ++i){
int a, b; cin>>a>>b;
cout<<query(a, b)<<'\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... |