Submission #1223492

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

const int INF = 1e9;
const int LOG = 30;
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, jumpR, jumpL, dpR, dpL;

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;
        }
    }
    jumpL.assign(n+1, vector<int>(LOG, -1));
    dpL.assign(n+1, vector<int>(LOG, 0));
    jumpR.assign(n+1, vector<int>(LOG, -1));
    dpR.assign(n+1, vector<int>(LOG, 0));
    for (int i = n; i >= 1; i--) {
        if (toR[i] > n) continue;
        jumpR[i][0] = toR[i];
        dpR[i][0] = 1;
        for (int j = 1; j < LOG; j++) {
            if (jumpR[i][j-1] == -1) {
                jumpR[i][j] = -1;
                dpR[i][j] = -1;
                continue;
            }
            jumpR[i][j] = jumpR[jumpR[i][j-1]][j-1];
            dpR[i][j] = dpR[dpR[i][j-1]][j-1] + 1;
        }
    }
    for (int i = 1; i <= n; i++) {
        if (toL[i] < 1) continue;
        jumpL[i][0] = toL[i];
        dpL[i][0] = 1;
        for (int j = 1; j < LOG; j++) {
            if (jumpL[i][j-1] == -1) {
                jumpL[i][j] = -1;
                dpL[i][j] = -1;
                continue;
            }
            jumpL[i][j] = jumpL[jumpL[i][j-1]][j-1];
            dpL[i][j] = dpL[dpL[i][j-1]][j-1] + 1;
        }
    }
}

int minimum_jumps(int a, int b, int c, int d) {
    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]) {
                tot += dp[curr][j];
                curr = jump[curr][j];
                break;
            }
        }
    }
    if (jump[curr][0] == -1) return -1;
    if (h[toL[curr]] > d && h[toR[curr]] > d) return -1;
    // now only left, or only right
    if (d > a) {
        curr = a;
        while (curr != d) {
            for (int j = LOG-1; j >= 0; j--) {
                if (jumpR[curr][j] != -1 && h[jumpR[curr][j]] <= d) {
                    tot += dpR[curr][j];
                    curr = jumpR[curr][j];
                }
            }
        }
    }
    else {
        curr = a;
        while (curr != d) {
            for (int j = LOG-1; j >= 0; j--) {
                if (jumpL[curr][j] != -1 && h[jumpL[curr][j]] <= d) {
                    tot += dpL[curr][j];
                    curr = jumpL[curr][j];
                }
            }
        }
    }
    return tot;
}
#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...