Submission #980673

#TimeUsernameProblemLanguageResultExecution timeMemory
980673vjudge1Rainforest Jumps (APIO21_jumps)C++17
23 / 100
740 ms32072 KiB
#include <bits/stdc++.h>
#include "jumps.h"

using namespace std;

const int N = 200200;

int h[N];
int to[N][18][2];

void init(int N, vector<int> H) {
    for (int i = 0; i < N; i++)
        h[i] = H[i];
    
    vector<int> stack;
    for (int i = 0; i < N; i++) {
        while (!stack.empty() && H[stack.back()] < H[i]) 
            stack.pop_back();
        if (stack.empty()) to[i][0][0] = i;
        else to[i][0][0] = stack.back();
        stack.push_back(i);
    }
    stack.clear();
    for (int i = N - 1; i >= 0; i--) {
        while (!stack.empty() && H[stack.back()] < H[i])
            stack.pop_back();
        if (stack.empty()) to[i][0][1] = i;
        else to[i][0][1] = stack.back();
        stack.push_back(i);
    }
    for (int i = 0; i < N; i++) {
        if (H[to[i][0][0]] < H[to[i][0][1]])
            swap(to[i][0][0], to[i][0][1]);
    }
    for (int k = 1; k < 18; k++) {
        for (int i = 0; i < N; i++) {
            to[i][k][0] = to[ to[i][k - 1][0] ][k - 1][0];
            to[i][k][1] = to[ to[i][k - 1][1] ][k - 1][1];
        }
    }
}

int minimum_jumps(int A, int B, int C, int D) {
    if (A == B && C == D) {
        int ans = 0;
        int cur = A;
        for (int k = 17; k >= 0; k--) {
            if (h[to[cur][k][0]] < h[C]) {
                cur = to[cur][k][0];
                ans += (1 << k);
            }
        }
        if (to[cur][0][0] == C) return ans + 1;

        for (int k = 17; k >= 0; k--) {
            if (h[to[cur][k][1]] < h[C]) {
                cur = to[cur][k][1];
                ans += (1 << k);
            }
        }
        
        if (to[cur][0][1] == C) return ans + 1;
        return -1;
    }
    return 0;
}
/*
7 1
3 2 1 6 4 5 7
4 4 6 6

*/
#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...