Submission #415824

# Submission time Handle Problem Language Result Execution time Memory
415824 2021-06-01T15:12:45 Z valerikk Rainforest Jumps (APIO21_jumps) C++17
0 / 100
326 ms 29552 KB
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef long double ld;

const int N = 1e5 + 7;
const int L = 19;
const int INF = 1e9;

int n;
int h[N];
int lg[N];
int sparse[L][N];
vector<vector<int>> go_l, go_r, go_max;
vector<int> l, r, mx;

vector<vector<int>> build_go(vector<int> to) {
    vector<vector<int>> go(L, vector<int>(n));
    for (int i = 0; i < n; ++i) go[0][i] = to[i];
    for (int i = 1; i < L; ++i) {
        for (int j = 0; j < n; ++j) go[i][j] = go[i - 1][go[i - 1][j]];
    }
    return go;
}

void build_sparse() {
    for (int i = 0; i < n; ++i) sparse[0][i] = h[i];
    for (int i = 1; i < L; ++i) {
        for (int j = 0; j + (1 << i) <= n; ++j) sparse[i][j] = max(sparse[i - 1][j], sparse[i - 1][j + (1 << (i - 1))]);
    }
}

int get_max(int l, int r) {
    if (l >= r) return -INF;
    int t = lg[r - l];
    return max(sparse[t][l], sparse[t][r - (1 << t)]);
}

void init() {
    lg[0] = lg[1] = 0;
    for (int i = 2; i <= n; ++i) lg[i] = lg[i >> 1] + 1;
    build_sparse();

    l = vector<int>(n);
    vector<int> st;
    for (int i = n - 1; i >= 0; --i) {
        while (!st.empty() && h[st.back()] < h[i]) {
            l[st.back()] = i;
            st.pop_back();
        }
        st.push_back(i);
    }
    l[0] = 0;
    go_l = build_go(l);

    r = vector<int>(n);
    st.clear();
    for (int i = 0; i < n; ++i) {
        while (!st.empty() && h[st.back()] < h[i]) {
            r[st.back()] = i;
            st.pop_back();
        }
        st.push_back(i);
    }
    r[n - 1] = n - 1;
    go_r = build_go(r);

    mx = vector<int>(n);
    for (int i = 0; i < n; ++i) {
        if (h[l[i]] > h[r[i]]) mx[i] = l[i]; else mx[i] = r[i];
    }
    go_max = build_go(mx);
}

void init(int N, vector<int> H) {
    for (int i = 0; i < N; ++i) h[i + 1] = H[i] - 1;
    h[0] = N;
    h[N + 1] = N + 1;
    n = N + 2;
    init();
}

int minimum_jumps(int A, int B, int C, int D) {
    int sl = A + 1, sr = B + 2, tl = C + 1, tr = D + 2;

    int up = get_max(tl, tr);
    int down = get_max(sr, tl);

    if (down > up || h[sr - 1] > up) return -1;

    int s = sr - 1;
    for (int i = L - 1; i >= 0; --i) {
        if (go_l[i][s] >= sl && h[go_l[i][s]] < up) s = go_l[i][s];
    }

    int res = 0;
    for (int i = L - 1; i >= 0; --i) {
        if (s < tl && h[go_max[i][s]] <= up) {
            res += (1 << i);
            s = go_max[i][s];
        }
    }

    for (int i = L - 1; i >= 0; --i) {
        if (go_r[i][s] < tl) {
            res += (1 << i);
            s = go_r[i][s];
        }
    }

    if (s < tl) {
        ++res;
        s = r[s];
    }

    return res;
}

#ifdef LOCAL
int main() {
    freopen("input.txt", "r", stdin);
    ios::sync_with_stdio(false);
    cin.tie(0);
    int N, Q;
    cin >> N >> Q;
    vector<int> H(N);
    for (auto &i : H) cin >> i;
    init(N, H);
    while (Q--) {
        int A, B, C, D;
        cin >> A >> B >> C >> D;
        cout << minimum_jumps(A, B, C, D) << endl;
    }
    return 0;
}
#endif
# Verdict Execution time Memory Grader output
1 Correct 1 ms 200 KB Output is correct
2 Correct 1 ms 200 KB Output is correct
3 Runtime error 23 ms 3728 KB Execution killed with signal 11
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 200 KB Output is correct
2 Correct 1 ms 200 KB Output is correct
3 Correct 1 ms 200 KB Output is correct
4 Correct 1 ms 200 KB Output is correct
5 Correct 2 ms 328 KB Output is correct
6 Incorrect 3 ms 328 KB Output isn't correct
7 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 200 KB Output is correct
2 Correct 1 ms 200 KB Output is correct
3 Correct 1 ms 200 KB Output is correct
4 Correct 1 ms 200 KB Output is correct
5 Correct 2 ms 328 KB Output is correct
6 Incorrect 3 ms 328 KB Output isn't correct
7 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 200 KB Output is correct
2 Correct 1 ms 200 KB Output is correct
3 Correct 1 ms 200 KB Output is correct
4 Correct 1 ms 200 KB Output is correct
5 Runtime error 24 ms 3648 KB Execution killed with signal 11
6 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 200 KB Output is correct
2 Correct 1 ms 200 KB Output is correct
3 Correct 1 ms 200 KB Output is correct
4 Correct 326 ms 29552 KB Output is correct
5 Runtime error 31 ms 4412 KB Execution killed with signal 11
6 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 200 KB Output is correct
2 Correct 1 ms 200 KB Output is correct
3 Correct 1 ms 200 KB Output is correct
4 Correct 326 ms 29552 KB Output is correct
5 Runtime error 31 ms 4412 KB Execution killed with signal 11
6 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 200 KB Output is correct
2 Correct 1 ms 200 KB Output is correct
3 Runtime error 23 ms 3728 KB Execution killed with signal 11
4 Halted 0 ms 0 KB -