Submission #553580

#TimeUsernameProblemLanguageResultExecution timeMemory
553580Jarif_RahmanRainforest Jumps (APIO21_jumps)C++17
23 / 100
4059 ms37928 KiB
#include <bits/stdc++.h>
#define pb push_back
#define f first
#define sc second
using namespace std;
typedef long long int ll;
typedef string str;

const int K = 21;
const int inf = 1e9;

int n;
vector<int> h;
vector<int> jump[2];
vector<int> anc[2][K];

template<int k>
int get_anc(int nd, int h){
    for(int i = 0; i < K; i++){
        if(h%2 == 1) nd = anc[k][i][nd];
        h/=2;
    }
    return nd;
}

template<int k>
int binlift(int nd, int l){
    if(h[nd] == l) return 0;
    if(h[nd] > l) return -inf;
    for(int i = K-1; i >= 0; i--) if(h[anc[k][i][nd]] < l)
        return binlift<k>(anc[k][i][nd], l)+(1<<i);
    if(h[anc[k][0][nd]] == l) return 1;
    return 0;
}

void init(int N, vector<int> H){
    for(int &x: H) x--;
    n = N, h = H;
    h.pb(n);
    fill(jump, jump+2, vector<int>(n, n));
    fill(anc[0], anc[0]+K, vector<int>(n+1, n));
    fill(anc[1], anc[1]+K, vector<int>(n+1, n));

    stack<int> st;
    for(int i = n-1; i >= 0; i--){
        while(!st.empty() && h[i] > h[st.top()]){
            jump[0][st.top()] = i;
            st.pop();
        }
        st.push(i);
    }
    while(!st.empty()) st.pop();

    for(int i = 0; i < n; i++){
        while(!st.empty() && h[i] > h[st.top()]){
            jump[1][st.top()] = i;
            st.pop();
        }
        st.push(i);
    }
    while(!st.empty()) st.pop();

    for(int i = 0; i < n; i++){
        anc[0][0][i] = h[jump[0][i]] < h[jump[1][i]] ? jump[0][i] : jump[1][i];
        anc[1][0][i] = h[jump[0][i]] > h[jump[1][i]] ? jump[0][i] : jump[1][i];
        if(anc[1][0][i] == n) anc[1][0][i] = anc[0][0][i];
    }

    for(int p = 1; p < K; p++) for(int i = 0; i <= n; i++)
        anc[0][p][i] = anc[0][p-1][anc[0][p-1][i]],
        anc[1][p][i] = anc[1][p-1][anc[1][p-1][i]];
}

int get_dis(int A, int C){
    int ans = binlift<1>(A, h[C]);
    if(ans < 0) return -1;
    int nd = get_anc<1>(A, ans);
    int x = binlift<0>(nd, h[C]);
    if(x < 0) return -1;
    nd = get_anc<0>(nd, x);
    ans+=x;
    if(nd == C) return ans;
    return -1;
}

int minimum_jumps(int A, int B, int C, int D){
    int ans = inf;
    for(int i = C; i <= D; i++){
        int mx = -1, in = -1;
        for(int j = A; j <= B; j++){
            if(h[j] > h[i]) continue;
            if(h[j] > mx) mx = h[j], in = j;
        }
        if(in == -1) continue;
        int x = get_dis(in, i);
        if(x != -1) ans = min(ans, x);
    }
    if(ans == inf) ans = -1;
    return ans;
}
#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...