#include <bits/stdc++.h>
using namespace std;
#define ii pair<long long,long long>
vector<vector<long long> >adj;
const long long N = 1e5 + 5;
const long long K = 18; /// K = log2N
long long d[N],c[N];
long long lift[N][K];
long long sum[N][K];
void dfs(long long idx,long long p){
lift[idx][0] = p;
sum[idx][0] = c[idx];
for(long long j = 1;j < K;j++){
lift[idx][j] = lift[lift[idx][j-1]][j-1];
sum[idx][j] = sum[idx][j-1] + sum[lift[idx][j-1]][j-1];
}
for(auto x:adj[idx]){
if(x != p){
dfs(x,idx);
}
}
}
int main(void){
ios_base::sync_with_stdio(0);
cin.tie(0);
long long n,q;
cin>>n>>q;
adj.assign(n+5,vector<long long>());
for(long long i = 1;i <= n;i++){
cin>>d[i]>>c[i];
}
stack<ii>s;
for(long long i = n;i >= 1;i--){
while(!s.empty() && s.top().first < d[i]){
s.pop();
}
if(s.empty()){
adj[i].push_back(0);
adj[0].push_back(i);
}
else{
adj[i].push_back(s.top().second);
adj[s.top().second].push_back(i);
}
s.push(ii(d[i],i));
}
dfs(0,0);
while(q--){
long long r,v;
cin>>r>>v;
for(long long j = K-1;j >= 0;j--){
if(sum[r][j] < v){
v -= sum[r][j];
r = lift[r][j];
}
}
cout<<r<<endl;
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4440 KB |
Output is correct |
2 |
Incorrect |
2 ms |
4440 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
352 ms |
42068 KB |
Output is correct |
2 |
Correct |
322 ms |
41080 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4440 KB |
Output is correct |
2 |
Incorrect |
2 ms |
4440 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |