Submission #476663

#TimeUsernameProblemLanguageResultExecution timeMemory
476663hoanghq2004Rainforest Jumps (APIO21_jumps)C++14
0 / 100
57 ms37520 KiB
#include "jumps.h"
#include <bits/stdc++.h>

using namespace std;

const int Nmax = 1e5 + 10;

int n;
int low[Nmax][20], high[Nmax][20];
int L[Nmax], R[Nmax];
int h[Nmax];

void init(int N, vector <int> H) {
    n = N;
    for (int i = 1; i <= n; ++i) h[i] = H[i - 1];
    stack <int> stk;
    for (int i = 1; i <= n; ++i) {
        while (stk.size() && h[stk.top()] < h[i]) {
            R[stk.top()] = i;
            stk.pop();
        }
        stk.push(i);
    }
    while (stk.size()) stk.pop();
    for (int i = n; i >= 1; --i) {
        while (stk.size() && h[stk.top()] < h[i]) {
            L[stk.top()] = i;
            stk.pop();
        }
        stk.push(i);
    }
    for (int i = 1; i <= n; ++i) {
        if (h[L[i]] < h[R[i]]) {
            high[i][0] = R[i];
            low[i][0] = L[i];
        } else {
            high[i][0] = L[i];
            low[i][0] = R[i];
        }
    }
    for (int lg = 1; (1 << lg) <= n; ++lg) {
        for (int i = 1; i <= n; ++i) {
            low[i][lg] = low[low[i][lg - 1]][lg - 1];
            high[i][lg] = high[high[i][lg - 1]][lg - 1];
        }
    }

}

int minimum_jumps(int A, int B, int C, int D) {
    ++A, ++B, ++C, ++D;
    // asume that A = B and C = D
    int need = 0;
    for (int i = 19; i >= 0; --i) {
        if (high[A][i] && h[high[A][i]] <= h[C]) {
            need += (1 << i);
            A = high[A][i];
        }
    }
    if (A == C) return need;
    for (int i = 19; i >= 0; --i) {
        if (low[A][i] && h[low[A][i]] <= h[C]) {
            need += (1 << i);
            A = low[A][i];
        }
    }
    return need;
}
//
//int main() {
//}
#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...