#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 |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
640 KB |
Output is correct |
3 |
Correct |
2 ms |
716 KB |
Output is correct |
4 |
Correct |
2 ms |
844 KB |
Output is correct |
5 |
Correct |
2 ms |
972 KB |
Output is correct |
6 |
Correct |
2 ms |
972 KB |
Output is correct |
7 |
Correct |
2 ms |
844 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
436 ms |
68020 KB |
Output is correct |
2 |
Correct |
480 ms |
62676 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
640 KB |
Output is correct |
3 |
Correct |
2 ms |
716 KB |
Output is correct |
4 |
Correct |
2 ms |
844 KB |
Output is correct |
5 |
Correct |
2 ms |
972 KB |
Output is correct |
6 |
Correct |
2 ms |
972 KB |
Output is correct |
7 |
Correct |
2 ms |
844 KB |
Output is correct |
8 |
Correct |
436 ms |
68020 KB |
Output is correct |
9 |
Correct |
480 ms |
62676 KB |
Output is correct |
10 |
Correct |
2 ms |
972 KB |
Output is correct |
11 |
Correct |
206 ms |
37304 KB |
Output is correct |
12 |
Correct |
627 ms |
71860 KB |
Output is correct |
13 |
Correct |
459 ms |
67100 KB |
Output is correct |
14 |
Correct |
302 ms |
66160 KB |
Output is correct |
15 |
Correct |
249 ms |
65716 KB |
Output is correct |