Submission #598807

#TimeUsernameProblemLanguageResultExecution timeMemory
598807JomnoiRainforest Jumps (APIO21_jumps)C++17
4 / 100
1197 ms50436 KiB
#include <bits/stdc++.h>
#include "jumps.h"
using namespace std;

const int MAX_N = 2e5 + 5;

int N;
int H[MAX_N];
int L[MAX_N][20], R[MAX_N][20], J[MAX_N][20];

void init(int n, vector <int> h) {
    N = n;
    for(int i = 1; i <= N; i++) {
        H[i] = h[i - 1];
    }
    H[0] = H[N + 1] = N + 1;

    stack <int> stk;
    for(int i = 0; i <= N + 1; i++) {
        while(!stk.empty() and H[stk.top()] < H[i]) {
            stk.pop();
        }

        if(!stk.empty()) {
            L[i][0] = stk.top();
        }
        else {
            L[i][0] = i;
        }
        stk.push(i);
    }
    while(!stk.empty()) {
        stk.pop();
    }

    for(int i = N + 1; i >= 0; i--) {
        while(!stk.empty() and H[stk.top()] < H[i]) {
            stk.pop();
        }

        if(!stk.empty()) {
            R[i][0] = stk.top();
        }
        else {
            R[i][0] = i;
        }
        stk.push(i);
    }

    L[N + 1][0] = 0;
    R[0][0] = N + 1;
    for(int i = 1; i <= N; i++) {
        if(H[L[i][0]] > H[R[i][0]]) {
            J[i][0] = L[i][0];
        }
        else {
            J[i][0] = R[i][0];
        }
    }

    for(int j = 1; j < 20; j++) {
        for(int i = 0; i <= N + 1; i++) {
            L[i][j] = L[L[i][j - 1]][j - 1];
            R[i][j] = R[R[i][j - 1]][j - 1];
            J[i][j] = J[J[i][j - 1]][j - 1];
        }
    }
}

int get_max(int l, int r) {
    for(int i = 19; i >= 0; i--) {
        if(R[l][i] < r) {
            l = R[l][i];
        }
    }
    return l;
}

int minimum_jumps(int A, int B, int C, int D) {
    A++, B++, C++, D++;

    int lft = get_max(A, B), rgt = get_max(C, D);
    if(R[lft][0] > D) {
        return -1;
    }
    if(B + 1 == C) {
        return 1;
    }

    int mid = get_max(B + 1, C - 1);
    if(R[mid][0] > D) {
        return -1;
    }
    if(H[lft] > H[mid]) {
        return 1;
    }

    int now = B;
    for(int i = 19; i >= 0; i--) {
        if(L[now][i] >= A and H[L[now][i]] < H[rgt]) {
            now = L[now][i];
        }
    }

    int ans = 0;
    for(int i = 19; i >= 0; i--) {
        if(H[J[now][i]] < H[rgt] and J[now][i] < C) {
            now = J[now][i];
            ans += (1<<i);
        }
    }
    for(int i = 19; i >= 0; i--) {
        if(R[now][i] < C) {
            now = R[now][i];
            ans += (1<<i);
        }
    }
    if(R[now][0] <= D) {
        return ans + 1;
    }
    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...