Submission #744926

#TimeUsernameProblemLanguageResultExecution timeMemory
744926t6twotwoRainforest Jumps (APIO21_jumps)C++17
27 / 100
1359 ms57512 KiB
#include "jumps.h"
#include <bits/stdc++.h>
using namespace std;
int N;
vector<int> H, T, W;
bool subtask1;
vector<vector<int>> adj, hi, lo, G;
void init(int n, vector<int> h) {
    N = n, H = h;
    H.push_back(N);
    subtask1 = 1;
    for (int i = 0; i < N; i++) {
        H[i]--;
        if (i != H[i]) {
            subtask1 = 0;
        }
    }
    adj.resize(N);
    vector<int> stk;
    for (int i = N - 1; i >= 0; i--) {
        while (!stk.empty() && H[stk.back()] < H[i]) {
            stk.pop_back();
        }
        if (!stk.empty()) {
            adj[i].push_back(stk.back());
        }
        stk.push_back(i);
    }
    stk.clear();
    for (int i = 0; i < N; i++) {
        while (!stk.empty() && H[stk.back()] < H[i]) {
            stk.pop_back();
        }
        if (!stk.empty()) {
            adj[i].push_back(stk.back());
        }
        stk.push_back(i);
    }
    lo.resize(N + 1, vector<int>(18, N));
    hi.resize(N + 1, vector<int>(18, N));
    for (int i = 0; i < N; i++) {
        if (adj[i].size() == 1) {
            hi[i][0] = adj[i][0];
        }
        if (adj[i].size() == 2) {
            if (H[adj[i][0]] > H[adj[i][1]]) {
                swap(adj[i][0], adj[i][1]);
            }
            lo[i][0] = adj[i][0];
            hi[i][0] = adj[i][1];
        }
    }
    for (int j = 0; j < 17; j++) {
        for (int i = 0; i < N; i++) {
            lo[i][j + 1] = lo[lo[i][j]][j];
            hi[i][j + 1] = hi[hi[i][j]][j];
        }
    }
    T.resize(N, N);
    W.resize(N, N);
    vector<int> A(N);
    for (int i = 0; i < N; i++) {
        A[H[i]] = i;
    }
    for (int i = N - 1; i >= 0; i--) {
        if (lo[A[i]][0] != N) {
            T[A[i]] = T[lo[A[i]][0]];
            W[A[i]] = W[lo[A[i]][0]] + 1;
        } else {
            T[A[i]] = A[i];
            W[A[i]] = 0;
        }
    }
}
int jump(int x, int y) {
    int ans = 0;
    for (int i = 17; i >= 0; i--) {
        if (H[hi[x][i]] <= H[y]) {
            ans += 1 << i;
            x = hi[x][i];
        }
    }
    for (int i = 17; i >= 0; i--) {
        if (H[lo[x][i]] <= H[y]) {
            ans += 1 << i;
            x = lo[x][i];
        }
    }
    if (x != y) {
        ans = N;
    }
    return ans;
}
int minimum_jumps(int A, int B, int C, int D) {
    if (subtask1) {
        return C - B;
    }
    if (C == D) {
        int p = A;
        for (int i = A; i <= B; i++) {
            if (H[i] > H[p]) {
                p = i;
            }
        }
        int ans = N;
        for (int i = C; i <= D; i++) {
            ans = min(ans, jump(p, i));
            for (int j = p + 1; j <= B; j++) {
                if (T[j] == T[C]) {
                    if (W[j] > W[C]) {
                        ans = min(ans, W[j] - W[C]);
                    }
                }
            }
            for (int j = p - 1; j >= A; j--) {
                if (T[j] == T[C]) {
                    if (W[j] > W[C]) {
                        ans = min(ans, W[j] - W[C]);
                    }
                }
            }
        }
        if (ans == N) {
            ans = -1;
        }
        return ans;
    }
    if (A == B && C == D) {
        int ans = jump(A, C);
        if (ans == N) {
            ans = -1;
        }
        return ans;
    }
    vector<int> dis(N, N);
    queue<int> q;
    for (int i = A; i <= B; i++) {
        dis[i] = 0;
        q.push(i);
    }
    while (!q.empty()) {
        int x = q.front();
        q.pop();
        if (C <= x && x <= D) {
            return dis[x];
        }
        for (int y : adj[x]) {
            if (dis[y] == N) {
                dis[y] = dis[x] + 1;
                q.push(y);
            }
        }
    }
    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...