Submission #558915

#TimeUsernameProblemLanguageResultExecution timeMemory
558915hoanghq2004밀림 점프 (APIO21_jumps)C++14
23 / 100
1055 ms52152 KiB
#include <bits/stdc++.h>
#pragma GCC optimize ("O3")
#pragma GCC optimize ("unroll-loops")
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include "jumps.h"

using namespace __gnu_pbds;
using namespace std;

template <typename T>
using ordered_set = tree <T, null_type, less <T>, rb_tree_tag, tree_order_statistics_node_update>;

const int N = 2e5 + 10;

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

void init(int N, std::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()) R[stk.top()] = n + 1, 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);
    }
    while (stk.size()) L[stk.top()] = 0, stk.pop();
    for (int i = 1; i <= n; ++i) {
        high[i][0] = (h[L[i]] > h[R[i]] ? L[i] : R[i]);
        low[i][0] = (h[L[i]] < h[R[i]] ? L[i] : R[i]);
        nxt[i][0] = L[i];
    }
    for (int lg = 1; (1 << lg) <= n; ++lg) {
        for (int i = 1; i + (1 << lg) - 1 <= n; ++i) {
            high[i][lg] = high[high[i][lg - 1]][lg - 1];
            low[i][lg] = low[low[i][lg - 1]][lg - 1];
            nxt[i][lg] = nxt[nxt[i][lg - 1]][lg - 1];
        }
    }
}

int minimum_jumps(int A, int B, int C, int D) {
    ++A, ++B, ++C, ++D;
    for (int lg = 19; lg >= 0; --lg)
        if (nxt[B][lg] >= A && h[nxt[B][lg]] <= h[C]) B = nxt[B][lg];
    int hmax = 0;
    for (int i = C; i <= D; ++i) hmax = max(hmax, h[i]);
    int cost = 0;
    for (int lg = 19; lg >= 0; --lg)
        if (h[high[A][lg]] && h[high[A][lg]] <= hmax)
            A = high[A][lg],
            cost += (1 << lg);
    for (int lg = 19; lg >= 0; --lg)
        if (h[low[A][lg]] && h[low[A][lg]] <= hmax)
            A = low[A][lg],
            cost += (1 << lg);
    if (A > D || A < C) return -1;
    return cost;
}

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