이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define endl "\n"
#define all(aa) aa.begin(), aa.end()
int main(){
int n, q;
cin>>n>>q;
vector<pair<int, int>> a(n);
for(auto &[d, c]:a) cin>>d>>c;
vector<vector<pair<int, int>>> par(n+1, vector<pair<int, int>>(20));
stack<int> s;
for(int i = n; i > 0; i--){
while(s.size() && a[s.top()-1].first<=a[i-1].first) s.pop();
par[i][0]={(s.empty() ? 0:s.top()), a[i-1].second};
s.push(i);
}
for(int i = 1; i < 20; i++) for(int v = 1; v <= n; v++)
par[v][i]={par[par[v][i-1].first][i-1].first, par[par[v][i-1].first][i-1].second+par[v][i-1].second};
while(q--){
int v, d;
cin>>v>>d;
int l=0, r=n+1;
while(l<r){
int mid=(l+r)/2, cur=0, dist=0, nd=v;
for(int i = 19; i >= 0; i--) if(cur+(1<<i)<=mid){
cur+=(1<<i);
dist+=par[nd][i].second;
nd=par[nd][i].first;
}
if(dist<d) l=mid+1;
else r=mid;
}
l--;
for(int i = 19; i >= 0; i--) if(l-(1<<i)>=0)
l-=(1<<i), v=par[v][i].first;
cout<<v<<endl;
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |