제출 #1118800

#제출 시각아이디문제언어결과실행 시간메모리
1118800Zero_OPRainforest Jumps (APIO21_jumps)C++14
23 / 100
2098 ms98880 KiB
#include <bits/stdc++.h>

using namespace std;

const int MAX = 2e5 + 5;
const int inf = 1e9;

int N, L, H[MAX], next_left[20][MAX], next_right[20][MAX], next_greater[20][MAX];

void init(int _N, vector<int> _H){
    N = _N;
    for(int i = 0; i < N; ++i) H[i] = _H[i];

    stack<int> st;
    for(int i = 0; i < N; ++i){
        while(!st.empty() && H[st.top()] <= H[i]) st.pop();
        if(!st.empty()) {
            next_left[0][i] = st.top();
        } else next_left[0][i] = -1;
        st.push(i);
    }

    stack<int>().swap(st);
    for(int i = N - 1; i >= 0; --i){
        while(!st.empty() && H[st.top()] <= H[i]) st.pop();
        if(!st.empty()) {
            next_right[0][i] = st.top();
        } else next_right[0][i] = -1;
        st.push(i);
    }

    for(int i = 0; i < N; ++i){
        if(next_left[0][i] == -1 && next_right[0][i] == -1) next_greater[0][i] = -1;
        else if(next_left[0][i] == -1) next_greater[0][i] = next_right[0][i];
        else if(next_right[0][i] == -1) next_greater[0][i] = next_left[0][i];
        else next_greater[0][i] = (H[next_left[0][i]] < H[next_right[0][i]] ? next_right[0][i] : next_left[0][i]);
    }

    #define refine(a, i, j) a[i][j] = (a[i - 1][j] == -1 ? -1 : a[i - 1][a[i - 1][j]])

    L = 32 - __builtin_clz(N);
    for(int i = 1; i < L; ++i){
        for(int j = 0; j < N; ++j){
            refine(next_left, i, j);
            refine(next_right, i, j);
            refine(next_greater, i, j);
        }
    }
}

int minimum_jumps(int a, int b, int c, int d){
    assert(a == b && c == d);
    if(H[a] > H[c]) return -1;

    int ans = 0;
    for(int i = L - 1; i >= 0; --i){
        if(next_greater[i][a] != -1 && H[next_greater[i][a]] < H[c]){
            ans += (1 << i);
            a = next_greater[i][a];
        }
    }

    for(int i = L - 1; i >= 0; --i){
        if(next_right[i][a] != -1 && H[next_right[i][a]] < H[c]){
            ans += (1 << i);
            a = next_right[i][a];
        }
    }

    if(H[next_right[0][a]] != H[c]) return -1;
    ++ans;

    return ans;
}

#ifdef LOCAL
int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    freopen("task.inp", "r", stdin);
    freopen("task.out", "w", stdout);

    int N, Q;
    cin >> N >> Q;

    vector<int> H(N);
    for(int i = 0; i < N; ++i){
        cin >> H[i];
    }

    init(N, H);

    for(int i = 0; i < Q; ++i){
        int a, b, c, d;
        cin >> a >> b >> c >> d;
        cout << minimum_jumps(a, b, c, d) << '\n';
    }

    return 0;
}
#endif //LOCAL
#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...