이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define ff first
#define ss second
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define int long long
///* __ __ __ */
///* ====== _ /| /| __ _ / | | /| | @ | | | | / /| |\ | / | | @ | / */
///* \- || |_| |_ / |/ | | | |_ |- | |--| /-| | | \ \ |==| |- /=| | \ | | |--| | |- */
///* || | | |_ / | |__| _| |_ \__ | | / | |__ | __| | | | \ / | | \| \__ | | | | \ */
///*
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef vector<pii> vii;
typedef vector<vi> vv;
const int N = 3e5;
vi g[N];
vii v(N);
int up[20][N];
int pr[N];
void dfs(int pos, int pre){
pr[pos] += v[pos].ss;
up[0][pos] = pre;
for(int i = 1; i < 18; i++){
up[i][pos] = up[i-1][up[i-1][pos]];
}
for(int to:g[pos]){
if(to == pre)continue;
pr[to] = pr[pos];
dfs(to, pos);
}
}
int isnp;
bool upper(int x, int sum){
if(pr[isnp] - pr[up[0][x]] >= sum)return true;
return false;
}
int lca(int pos, int sum){
if(pr[pos] - pr[up[0][pos]] >= sum)return pos;
for(int i = 17; i >= 0; i--){
if(!upper(up[i][pos], sum)){
pos = up[i][pos];
}
}
return up[0][pos];
}
void solve(){
int n, q;
cin >> n >> q;
for(int i = 1; i <= n; i++){
cin >> v[i].ff >> v[i].ss;
}
stack<int> s;
for(int i = n; i > 0; i--){
while(!s.empty() && v[s.top()].ff <= v[i].ff)s.pop();
if(s.empty()){
g[0].pb(i);
}else{
g[s.top()].pb(i);
}
s.push(i);
}
dfs(0, -1);
while(q--){
int x, sum;
cin >> x >> sum;
isnp = x;
cout << lca(x, sum) << '\n';
}
}
main(){
ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
int test = 1;
//~ cin >> test;
while(test--){
solve();
}
}
컴파일 시 표준 에러 (stderr) 메시지
fountain.cpp:73:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
73 | main(){
| ^~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |