Submission #1223479

#TimeUsernameProblemLanguageResultExecution timeMemory
1223479SpyrosAlivRainforest Jumps (APIO21_jumps)C++20
0 / 100
105 ms48372 KiB
#include <bits/stdc++.h>
using namespace std;

const int INF = 1e9;
const int LOG = 25;
int n;
vector<int> h;
vector<int> toL, toR;
vector<vector<int>> g, revG;
vector<int> deg;
vector<int> lead;
vector<vector<int>> jump, dp;

void init(int N, vector<int> H) {
    n = N;
    h = {0};
    toL.assign(n+1, 0);
    toR.assign(n+1, 0);
    for (auto x: H) h.push_back(x);
    stack<int> st;
    for (int i = 1; i <= n; i++) {
        while (!st.empty() && h[st.top()] <= h[i]) st.pop();
        if (st.empty()) toL[i] = 0;
        else toL[i] = st.top();
        st.push(i);
    }
    while (!st.empty()) st.pop();
    for (int i = n; i >= 1; i--) {
        while (!st.empty() && h[st.top()] <= h[i]) st.pop();
        if (st.empty()) toR[i] = n+1;
        else toR[i] = st.top();
        st.push(i);
    }
    lead.assign(n+1, -1);
    for (int i = 1; i <= n; i++) {
        if (toL[i] < 1 && toR[i] > n) {
            lead[i] = -1;
        }
        else {
            if (toR[i] > n) lead[i] = toL[i];
            else if (toL[i] < 1) lead[i] = toR[i];
            else {
                if (h[toL[i]] > h[toR[i]]) lead[i] = toL[i];
                else lead[i] = toR[i];
            }
        }
    }
    vector<pair<int, int>> vals;
    for (int i = 1; i <= n; i++) {
        vals.push_back({h[i], i});
    }
    sort(vals.rbegin(), vals.rend());
    jump.assign(n+1, vector<int>(LOG, -1));
    dp.assign(n+1, vector<int>(LOG, 0));
    for (auto [curr, pos]: vals) {
        jump[pos][0] = lead[pos];
        dp[pos][0] = 1;
        for (int i = 1; i < LOG; i++) {
            if (jump[pos][i-1] == -1) {
                jump[pos][i] = -1;
                dp[pos][i] = -1;
                continue;
            }
            jump[pos][i] = jump[jump[pos][i-1]][i-1];
            dp[pos][i] = dp[dp[pos][i-1]][i-1] + 1;
        }
    }
}

int minimum_jumps(int a, int b, int c, int d) {
    a++;
    b++;
    c++;
    d++;
    assert(a == b && c == d);
    if (h[a] > h[d]) return -1;
    int tot = 0;
    int curr = a;
    while (curr >= 1 && curr <= n) {
        if (jump[curr][0] == -1 || h[jump[curr][0]] >= h[d]) break;
        for (int j = LOG-1; j >= 0; j--) {
            if (jump[curr][j] == -1) continue;
            if (h[jump[curr][j]] < h[d]) {
                curr = jump[curr][j];
                tot += dp[curr][j];
                break;
            }
        }
    }
    if (toR[curr] == d || toL[curr] == d) return tot+1;
    else return -1;
}
#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...