제출 #870596

#제출 시각아이디문제언어결과실행 시간메모리
870596LOLOLO밀림 점프 (APIO21_jumps)C++14
4 / 100
790 ms81872 KiB
#include "jumps.h"
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
 
#define           f     first
#define           s     second
#define           pb    push_back
#define           ep    emplace
#define           eb    emplace_back
#define           lb    lower_bound
#define           ub    upper_bound
#define       all(x)    x.begin(), x.end()
#define      rall(x)    x.rbegin(), x.rend()
#define   uniquev(v)    sort(all(v)), (v).resize(unique(all(v)) - (v).begin())
#define     mem(f,x)    memset(f , x , sizeof(f))
#define        sz(x)    (int)(x).size()
#define  __lcm(a, b)    (1ll * ((a) / __gcd((a), (b))) * (b))
#define          mxx    *max_element
#define          mnn    *min_element
#define    cntbit(x)    __builtin_popcountll(x)
#define       len(x)    (int)(x.length())
 
const ll N = 2e5 + 100;
int h[N], sp[N][20], low[N][20], high[N][20], n, l[N][20], r[N][20];
 
void init(int _n, vector <int> _h) {
    n = _n;
    for (int i = 1; i <= n; i++) {
        h[i] = _h[i - 1];
        sp[i][0] = i;
    }
 
    r[n + 1][0] = low[n + 1][0] = high[n + 1][0] = n + 1;
    h[0] = h[n + 1] = 1e9;
 
    stack <int> st;
    for (int i = 0; i <= n; i++) {
        while (sz(st) && h[i] > h[st.top()]) {
            st.pop();
        }
 
        if (sz(st)) {
            l[i][0] = high[i][0] = low[i][0] = st.top();
        }
 
        st.push(i);
    }
 
    while (sz(st))
        st.pop();
 
    for (int i = n + 1; i >= 1; i--) {
        while (sz(st) && h[i] > h[st.top()]) {
            st.pop();
        }
 
        if (sz(st)) {
            if (h[high[i][0]] <= h[st.top()]) {
                high[i][0] = st.top();
            } else {
                low[i][0] = st.top();
            }

            r[i][0] = st.top();
        }
 
        st.push(i);
    }
 
    for (int i = 1; i < 20; i++) {
        for (int j = 0; j <= n + 1; j++) {
            high[j][i] = high[high[j][i - 1]][i - 1];
            low[j][i] = low[low[j][i - 1]][i - 1];
            l[j][i] = l[l[j][i - 1]][i - 1];
            r[j][i] = r[r[j][i - 1]][i - 1];
        }
    }
 
    for (int i = n; i >= 1; i--) {
        for (int j = 1; (1 << j) + i <= n + 1; j++) {
            int L = sp[i][j - 1], R = sp[i + (1 << (j - 1))][j - 1];
            sp[i][j] = h[L] > h[R] ? L : R;
        }
    }
}
 
int max_index(int l, int r) {
    int k = log2(r - l + 1);
    int L = sp[l][k], R = sp[r - (1 << k) + 1][k];
    return h[L] > h[R] ? L : R;
}
 
int minimum_jumps(int a, int b, int c, int d) {
    a++, b++, c++, d++;
    int p = max_index(c, d);
 
    if (b == c - 1) {
        return r[b][0] <= d ? 1 : -1;
    }
 
    int mid = max_index(b + 1, c - 1);
    int st = b, cnt = 0;
 
    for (int i = 19; i >= 0; i--) {
        if (l[st][i] >= a && h[l[st][i]] < h[mid]) {
            st = l[st][i];
        }
    }

    if (l[st][0] >= a && r[l[st][0]][0] <= d)
        return 1;
 
    for (int i = 19; i >= 0; i--) {
        if (h[high[st][i]] <= h[mid]) {
            cnt += (1 << i);
            st = high[st][i];
        }
    }
 
    for (int i = 19; i >= 0; i--) {
        if (r[st][i] < c) {
            cnt += (1 << i);
            st = r[st][i];
        }
    }

    if (r[st][0] <= d)
        return cnt + 1;

    return -1;
}

컴파일 시 표준 에러 (stderr) 메시지

jumps.cpp: In function 'int minimum_jumps(int, int, int, int)':
jumps.cpp:96:9: warning: unused variable 'p' [-Wunused-variable]
   96 |     int p = max_index(c, d);
      |         ^
#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...