Submission #415889

# Submission time Handle Problem Language Result Execution time Memory
415889 2021-06-01T16:35:29 Z valerikk Rainforest Jumps (APIO21_jumps) C++17
27 / 100
1761 ms 39792 KB
#include <bits/stdc++.h>

using namespace std;

const int N = 2e5 + 7, K = 19;
const int INF = 1e9;

int n, h[N];
int L[N], R[N], M[N];
int R1[N][K], M1[N][K];
pair<int, int> t[4 * N];

void build(int v, int l, int r) {
    if (r - l == 1) {
        t[v] = {h[l], l};
    } else {
        int m = (l + r) / 2;
        build(2 * v, l, m);
        build(2 * v + 1, m, r);
        t[v] = max(t[2 * v], t[2 * v + 1]);
    }
}

pair<int, int> get(int v, int l, int r, int a, int b) {
    if (l >= b || r <= a) return {-INF, -INF};
    if (l >= a && r <= b) return t[v];
    int m = (l + r) / 2;
    return max(get(2 * v, l, m, a, b), get(2 * v + 1, m, r, a, b));
}

int get1(int v, int l, int r, int a, int b, int x) {
    if (l >= a || r <= b || t[v].first <= x) return -1;
    if (r - l == 1) return l;
    int m = (l + r) / 2;
    int res = get1(2 * v + 1, m, r, a, b, x);
    if (res == -1) res = get1(2 * v, l, m, a, b, x);
    return res;
}

void init(int nn, vector<int> hh) {
    for (int i = 0; i < nn; i++) h[i + 1] = hh[i] - 1;
    h[0] = nn;
    h[nn + 1] = nn + 1;
    n = nn + 2;
    vector<int> st;
    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;
    st.clear();
    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;
    st.clear();
    build(1, 0, n);
    for (int i = 0; i < n; ++i) M[i] = h[L[i]] > h[R[i]] ? L[i] : R[i];
    for (int i = 0; i < n; ++i) {
        R1[i][0] = R[i];
        M1[i][0] = M[i];
    }
    for (int i = 1; i < K; ++i) {
        for (int j = 0; j < n; ++j) {
            R1[j][i] = R1[R1[j][i - 1]][i - 1];
            M1[j][i] = M1[M1[j][i - 1]][i - 1];
        }
    }
}

int minimum_jumps(int aa, int bb, int cc, int dd) {
    int a = aa + 1, b = bb + 1, c = cc + 1, d = dd + 1;
    int mx = get(1, 0, n, c, d + 1).first;
    int mx1 = get(1, 0, n, b + 1, c).first;
    if (mx1 > mx || h[b] > mx) return -1;
    int ind = get1(1, 0, n, 0, b + 1, mx);
    a = max(a, ind + 1);
    int st = get(1, 0, n, a, b + 1).second;
    int res = 1;
    for (int i = K - 1; i >= 0; --i) {
        if (h[M1[st][i]] < mx1) {
            res += (1 << i);
            st = M1[st][i];
        }
    }
    if (h[st] < mx1 && h[M[st]] <= mx) {
        st = M[st];
        ++res;
    }
    for (int i = K - 1; i >= 0; --i) {
        if (h[R1[st][i]] < mx1) {
            res += (1 << i);
            st = R1[st][i];
        }
    }
    if (h[st] < mx1 && h[R[st]] < mx) {
        st = R[st];
        ++res;
    }
    return res;
}

#ifdef LOCAL
int main() {
    freopen("input.txt", "r", stdin);
    ios::sync_with_stdio(false);
    cin.tie(0);
    int nn, qq;
    cin >> nn >> qq;
    vector<int> hh(nn);
    for (auto &x : hh) cin >> x;
    init(nn, hh);
    while (qq--) {
        int aa, bb, cc, dd;
        cin >> aa >> bb >> cc >> dd;
        cout << minimum_jumps(aa, bb, cc, dd) << endl;
    }
    return 0;
}
#endif
# Verdict Execution time Memory Grader output
1 Correct 1 ms 328 KB Output is correct
2 Correct 1 ms 328 KB Output is correct
3 Correct 196 ms 32564 KB Output is correct
4 Correct 1674 ms 39792 KB Output is correct
5 Correct 1279 ms 20136 KB Output is correct
6 Correct 1761 ms 39732 KB Output is correct
7 Correct 1324 ms 28556 KB Output is correct
8 Correct 1618 ms 39732 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 328 KB Output is correct
2 Correct 1 ms 328 KB Output is correct
3 Correct 1 ms 328 KB Output is correct
4 Correct 1 ms 328 KB Output is correct
5 Incorrect 3 ms 328 KB Output isn't correct
6 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 328 KB Output is correct
2 Correct 1 ms 328 KB Output is correct
3 Correct 1 ms 328 KB Output is correct
4 Correct 1 ms 328 KB Output is correct
5 Incorrect 3 ms 328 KB Output isn't correct
6 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 328 KB Output is correct
2 Correct 2 ms 328 KB Output is correct
3 Correct 2 ms 328 KB Output is correct
4 Correct 1 ms 328 KB Output is correct
5 Correct 82 ms 32016 KB Output is correct
6 Correct 121 ms 38924 KB Output is correct
7 Correct 61 ms 19892 KB Output is correct
8 Correct 103 ms 38840 KB Output is correct
9 Correct 11 ms 5920 KB Output is correct
10 Correct 99 ms 38840 KB Output is correct
11 Correct 106 ms 39732 KB Output is correct
12 Incorrect 106 ms 39456 KB Output isn't correct
13 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 328 KB Output is correct
2 Correct 1 ms 328 KB Output is correct
3 Correct 1 ms 328 KB Output is correct
4 Correct 261 ms 18068 KB Output is correct
5 Correct 1205 ms 38836 KB Output is correct
6 Correct 837 ms 6984 KB Output is correct
7 Correct 1230 ms 38720 KB Output is correct
8 Correct 422 ms 14144 KB Output is correct
9 Correct 1439 ms 38812 KB Output is correct
10 Correct 1654 ms 39732 KB Output is correct
11 Correct 1243 ms 39688 KB Output is correct
12 Correct 1567 ms 39568 KB Output is correct
13 Correct 1333 ms 38840 KB Output is correct
14 Correct 1469 ms 39260 KB Output is correct
15 Correct 1152 ms 39692 KB Output is correct
16 Correct 1400 ms 39692 KB Output is correct
17 Correct 1 ms 328 KB Output is correct
18 Correct 1 ms 328 KB Output is correct
19 Correct 3 ms 328 KB Output is correct
20 Correct 3 ms 328 KB Output is correct
21 Correct 6 ms 328 KB Output is correct
22 Correct 5 ms 328 KB Output is correct
23 Correct 4 ms 328 KB Output is correct
24 Correct 5 ms 328 KB Output is correct
25 Correct 1 ms 328 KB Output is correct
26 Correct 2 ms 456 KB Output is correct
27 Correct 19 ms 712 KB Output is correct
28 Correct 36 ms 712 KB Output is correct
29 Correct 19 ms 712 KB Output is correct
30 Correct 25 ms 712 KB Output is correct
31 Correct 35 ms 712 KB Output is correct
32 Correct 1 ms 328 KB Output is correct
33 Correct 61 ms 22308 KB Output is correct
34 Correct 118 ms 38844 KB Output is correct
35 Correct 127 ms 39568 KB Output is correct
36 Correct 99 ms 38832 KB Output is correct
37 Correct 129 ms 39328 KB Output is correct
38 Correct 107 ms 39716 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 328 KB Output is correct
2 Correct 1 ms 328 KB Output is correct
3 Correct 1 ms 328 KB Output is correct
4 Correct 261 ms 18068 KB Output is correct
5 Correct 1205 ms 38836 KB Output is correct
6 Correct 837 ms 6984 KB Output is correct
7 Correct 1230 ms 38720 KB Output is correct
8 Correct 422 ms 14144 KB Output is correct
9 Correct 1439 ms 38812 KB Output is correct
10 Correct 1654 ms 39732 KB Output is correct
11 Correct 1243 ms 39688 KB Output is correct
12 Correct 1567 ms 39568 KB Output is correct
13 Correct 1333 ms 38840 KB Output is correct
14 Correct 1469 ms 39260 KB Output is correct
15 Correct 1152 ms 39692 KB Output is correct
16 Correct 1400 ms 39692 KB Output is correct
17 Correct 1 ms 328 KB Output is correct
18 Correct 1 ms 328 KB Output is correct
19 Correct 3 ms 328 KB Output is correct
20 Correct 3 ms 328 KB Output is correct
21 Correct 6 ms 328 KB Output is correct
22 Correct 5 ms 328 KB Output is correct
23 Correct 4 ms 328 KB Output is correct
24 Correct 5 ms 328 KB Output is correct
25 Correct 1 ms 328 KB Output is correct
26 Correct 2 ms 456 KB Output is correct
27 Correct 19 ms 712 KB Output is correct
28 Correct 36 ms 712 KB Output is correct
29 Correct 19 ms 712 KB Output is correct
30 Correct 25 ms 712 KB Output is correct
31 Correct 35 ms 712 KB Output is correct
32 Correct 1 ms 328 KB Output is correct
33 Correct 61 ms 22308 KB Output is correct
34 Correct 118 ms 38844 KB Output is correct
35 Correct 127 ms 39568 KB Output is correct
36 Correct 99 ms 38832 KB Output is correct
37 Correct 129 ms 39328 KB Output is correct
38 Correct 107 ms 39716 KB Output is correct
39 Correct 1 ms 328 KB Output is correct
40 Correct 1 ms 328 KB Output is correct
41 Correct 1 ms 328 KB Output is correct
42 Correct 372 ms 18120 KB Output is correct
43 Correct 1330 ms 38820 KB Output is correct
44 Correct 724 ms 6984 KB Output is correct
45 Correct 1331 ms 38816 KB Output is correct
46 Correct 825 ms 14240 KB Output is correct
47 Correct 1372 ms 38840 KB Output is correct
48 Correct 1588 ms 39664 KB Output is correct
49 Correct 1072 ms 39572 KB Output is correct
50 Correct 1736 ms 39584 KB Output is correct
51 Correct 1523 ms 38808 KB Output is correct
52 Correct 1539 ms 39260 KB Output is correct
53 Correct 980 ms 39692 KB Output is correct
54 Correct 1211 ms 39716 KB Output is correct
55 Correct 1 ms 328 KB Output is correct
56 Incorrect 176 ms 38616 KB Output isn't correct
57 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 328 KB Output is correct
2 Correct 1 ms 328 KB Output is correct
3 Correct 196 ms 32564 KB Output is correct
4 Correct 1674 ms 39792 KB Output is correct
5 Correct 1279 ms 20136 KB Output is correct
6 Correct 1761 ms 39732 KB Output is correct
7 Correct 1324 ms 28556 KB Output is correct
8 Correct 1618 ms 39732 KB Output is correct
9 Correct 1 ms 328 KB Output is correct
10 Correct 1 ms 328 KB Output is correct
11 Correct 1 ms 328 KB Output is correct
12 Correct 1 ms 328 KB Output is correct
13 Incorrect 3 ms 328 KB Output isn't correct
14 Halted 0 ms 0 KB -