Submission #561335

#TimeUsernameProblemLanguageResultExecution timeMemory
561335piOOERainforest Jumps (APIO21_jumps)C++17
37 / 100
4054 ms32296 KiB
#include "jumps.h"

#include <bits/stdc++.h>

using namespace std;

#define all(x) begin(x), end(x)

const int logn = 18, N = 200001;

int h[N], jump[logn][N], R[N], L[N], spt[logn][N], n;
const int infI = 1e9 + 7;

bool okk = true;

int Min(int i, int j) {
    return h[i] < h[j] ? i : j;
}

int Max(int i, int j) {
    return h[i] > h[j] ? i : j;
}

int get_max(int l, int r) {
    if (r <= l) return -1;
    int sz = __lg(r - l);
    return Max(spt[sz][l], spt[sz][r - (1 << sz)]);
}

void init(int nn, vector<int> H) {
    n = nn;
    for (int i = 0; i < n; ++i) {
        h[i] = H[i];
        okk &= (H[i] == i + 1);
    }
    vector<int> st;
    for (int i = 0; i < n; ++i) {
        while (!st.empty() && h[st.back()] < h[i]) {
            R[st.back()] = i;
            st.pop_back();
        }
        if (st.empty()) {
            L[i] = -1;
        } else {
            L[i] = st.back();
        }
        st.push_back(i);
    }
    for (int i: st) {
        R[i] = n;
    }
    st.clear();
    for (int i = 0; i < n; ++i) {
        jump[0][i] = R[i];
        spt[0][i] = i;
    }
    jump[0][n] = n;
    for (int j = 1; j < logn; ++j) {
        for (int i = 0; i <= n; ++i) {
            jump[j][i] = jump[j - 1][jump[j - 1][i]];
            if (i + (1 << j) <= n) {
                spt[j][i] = Max(spt[j - 1][i], spt[j - 1][i + (1 << (j - 1))]);
            }
        }
    }
}

int minimum_jumps(int A, int B, int C, int D) {
    if (okk) {
        return C - B;
    }
    ++D, ++B;
//    int mxcd = get_max(C, D);
//    int lx = A - 1, rx = C;
//    while (lx + 1 < rx) {
//        int m = (lx + rx) >> 1;
//        if (h[get_max(m, C)] < h[mxcd]) rx = m;
//        else lx = m;
//    }
//    if (rx >= B) {
//        return -1;
//    }
//    int i = get_max(rx, B);
    queue<int> q;
    vector<int> dist(n, infI);
    for (int i = A; i < B; ++i) {
        dist[i] = 0;
        q.push(i);
    }
    while (!q.empty()) {
        int v = q.front();
        q.pop();
        if (L[v] != -1 && dist[L[v]] == infI) {
            q.push(L[v]);
            dist[L[v]] = dist[v] + 1;
        }
        if (R[v] != n && dist[R[v]] == infI) {
            q.push(R[v]);
            dist[R[v]] = dist[v] + 1;
        }
    }
    int ans = infI;
    for (int i = C; i < D; ++i) {
        ans = min(ans, dist[i]);
    }
    return (ans == infI ? -1 : 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...