Submission #476666

#TimeUsernameProblemLanguageResultExecution timeMemory
476666hoanghq2004Rainforest Jumps (APIO21_jumps)C++14
0 / 100
216 ms37700 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];
        }
    }
    if (A != C) return -1;
    return need;
}
//
//int main() {
//  int N, Q;
//  assert(2 == scanf("%d %d", &N, &Q));
//  std::vector<int> H(N);
//  for (int i = 0; i < N; ++i) {
//    assert(1 == scanf("%d", &H[i]));
//  }
//  init(N, H);
//
//  for (int i = 0; i < Q; ++i) {
//    int A, B, C, D;
//    assert(4 == scanf("%d %d %d %d", &A, &B, &C, &D));
//    printf("%d\n", minimum_jumps(A, B, C, D));
//  }
//  return 0;
//}
#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...