제출 #1345647

#제출 시각아이디문제언어결과실행 시간메모리
1345647tsengang밀림 점프 (APIO21_jumps)C++20
0 / 100
527 ms1114112 KiB
#include <bits/stdc++.h>
#define ll int
#define ff first
#define ss second
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define ertunt return
#define rall(x) x.rbegin(),x.rend()
using namespace std;
const ll N = 200005;
vector<ll>adj[N];
ll n;
ll p[20][N], dep[N];
struct segtree{
    ll n;
    vector<ll>d;
    vector<ll>a;
    segtree(){}
    segtree(ll _n, vector<ll>b){
        n = _n;
        d.resize(4*n);
        swap(a,b);
        build(1,1,n);
    }
    void build(ll v,ll l,ll r){
        if(l == r){
            d[v] = l;
            ertunt;
        }
        ll m = (l+r)>>1;
        build(v*2,l,m);
        build(v*2+1,m+1,r);
        if(a[d[v*2]] > a[d[v*2+1]]) d[v] = d[v*2];
        else d[v] = d[v*2+1];
    }
    ll query(ll v,ll l,ll r,ll L,ll R){
        if(R < l || r < L) ertunt -1;
        if(L <= l && r <= R) ertunt d[v];
        ll m = (l+r)>>1;
        ll x = query(v*2,l,m,L,R);
        ll y = query(v*2+1,m+1,r,L,R);
        if(x == -1) ertunt y;
        if(y == -1) ertunt x;
        if(a[x] > a[y]) ertunt x;
        ertunt y;
    }
};
segtree gang;
void dfs(ll u,ll par){
    p[0][u] = par;
    for(auto v: adj[u]){
        if(v == par) continue;
        dep[v] = dep[u] + 1;
        dfs(v,u);
    }
}
ll lca(ll u,ll v){
    if(dep[u] < dep[v]) swap(u,v);
    ll k = dep[u] - dep[v];
    for(ll i=0;i<20;i++){
        if(k&(1<<i)) u = p[i][u];
    }
    if(u==v) ertunt u;
    for(ll i=19;i>=0;i--){
        if(p[i][u] != p[i][v]){
            u = p[i][u];
            v = p[i][v];
        }
    }
    ertunt p[0][u];
}
ll dist(ll u,ll v){
    ll w = lca(u,v);
    ertunt dep[u] + dep[v] - 2*dep[w];
}
void init(int _n, vector<int> h){
    n = _n;
    h.insert(h.begin(), 0);
    gang = segtree(n,h);
    stack<ll>st;
    for(ll i = 1; i <= n; i++){
        while(!st.empty() && h[st.top()] < h[i]) st.pop();
        if(!st.empty()){
            adj[i].pb(st.top());
            adj[st.top()].pb(i);
        }
        st.push(i);
    }
    while(!st.empty()) st.pop();
    for(ll i = n; i >= 1; i--){
        while(!st.empty() && h[st.top()] < h[i]) st.pop();
        if(!st.empty()){
            adj[i].pb(st.top());
            adj[st.top()].pb(i);
        }
        st.push(i);
    }
    dep[1] = 0;
    dfs(1,0);
    for(ll j=1;j<20;j++){
        for(ll i=1;i<=n;i++){
            p[j][i] = p[j-1][p[j-1][i]];
        }
    }
}
int minimum_jumps(int A, int B, int C, int D){
    ll a=A,b=B,c=C,d=D;
    ll id = gang.query(1,1,n,c,d);
    if(id < min(a,b) || id > max(a,b)) ertunt -1;
    ertunt dist(a,id);
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...