Submission #561393

# Submission time Handle Problem Language Result Execution time Memory
561393 2022-05-12T18:23:15 Z piOOE Rainforest Jumps (APIO21_jumps) C++17
60 / 100
1223 ms 47948 KB
#include "jumps.h"

#include <bits/stdc++.h>

using namespace std;

#define all(x) begin(x), end(x)

const int logn = 19, N = 200001;

int h[N], jump[logn][N], R[N], L[N], spt[logn][N], n, jmp[logn][N];
const int infI = 1e9 + 7;


int Min(int i, int j) {
    return h[i] < h[j] ? i : j;
}

int Max(int i, int j) {
    return h[i] > h[j] ? i : j;
}

int get_max(int l, int r) {
    if (r <= l) return -1;
    int sz = __lg(r - l);
    return Max(spt[sz][l], spt[sz][r - (1 << sz)]);
}

void init(int nn, vector<int> H) {
    n = nn;
    for (int i = 0; i < n; ++i) {
        h[i] = H[i];
    }
    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();
        }
        if (st.empty()) {
            L[i] = -1;
        } else {
            L[i] = st.back();
        }
        st.push_back(i);
    }
    for (int i: st) {
        R[i] = n;
    }
    st.clear();
    for (int i = 0; i < n; ++i) {
        if (R[i] == n)
            jump[0][i] = L[i];
        else if (L[i] == -1)
            jump[0][i] = R[i];
        else
            jump[0][i] = Max(L[i], R[i]);
        jmp[0][i] = R[i];
        spt[0][i] = i;
    }
    jmp[0][n] = n;
    jump[0][n] = -1;
    for (int j = 1; j < logn; ++j) {
        for (int i = 0; i <= n; ++i) {
            if (jump[j - 1][i] != -1)
                jump[j][i] = jump[j - 1][jump[j - 1][i]];
            else
                jump[j][i] = -1;
            jmp[j][i] = jmp[j - 1][jmp[j - 1][i]];
        }
    }
    for (int j = 1; j < logn; ++j) {
        for (int i = 0; i + (1 << j) <= n; ++i) {
            spt[j][i] = Max(spt[j - 1][i], spt[j - 1][i + (1 << (j - 1))]);
        }
    }
}

int minimum_jumps(int A, int B, int C, int D) {
    ++D, ++B;
    int mxcd = get_max(C, D);
    int lx = A - 1, rx = C;
    while (lx + 1 < rx) {
        int m = (lx + rx) >> 1;
        if (h[get_max(m, C)] < h[mxcd]) rx = m;
        else lx = m;
    }
    if (rx >= B) {
        return -1;
    }
    int i = get_max(rx, B);
    int ans = infI;
    int nw = 0;
    for (int j = logn - 1; j > -1; --j) {
        if (jump[j][i] == -1) continue;
        if (jump[j][i] >= C && jump[j][i] < D) {
            ans = min(ans, nw + (1 << j));
        } else if (h[jump[j][i]] >= h[C] && h[jump[j][i]] < h[mxcd]) {
            assert(jump[j][i] < C);
            ans = min(ans, nw + (1 << j) + 1);
        }
        if (h[jump[j][i]] < h[C]) {
            nw += (1 << j);
            i = jump[j][i];
        }
    }
    assert(L[i] != C);
    assert(i < C);
    if (jump[0][i] >= C && jump[0][i] < D) {
        return min(ans, nw + 1);
    } else {
        if (h[jump[0][i]] < h[mxcd]) {
            ans = min(ans, nw + 2);
        }
        for (int j = logn - 1; j > -1; --j) {
            if (jmp[j][i] < C) {
                nw += (1 << j);
                i = jmp[j][i];
            }
        }
        assert(R[i] >= C && R[i] < D);
        return min(ans, nw + 1);
    }
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 464 KB Output is correct
2 Correct 0 ms 464 KB Output is correct
3 Correct 149 ms 37408 KB Output is correct
4 Correct 1135 ms 47012 KB Output is correct
5 Correct 867 ms 23640 KB Output is correct
6 Correct 1123 ms 47028 KB Output is correct
7 Correct 772 ms 32052 KB Output is correct
8 Correct 1014 ms 47028 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 464 KB Output is correct
2 Correct 0 ms 464 KB Output is correct
3 Correct 1 ms 464 KB Output is correct
4 Correct 0 ms 464 KB Output is correct
5 Incorrect 2 ms 464 KB Output isn't correct
6 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 464 KB Output is correct
2 Correct 0 ms 464 KB Output is correct
3 Correct 1 ms 464 KB Output is correct
4 Correct 0 ms 464 KB Output is correct
5 Incorrect 2 ms 464 KB Output isn't correct
6 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 464 KB Output is correct
2 Correct 0 ms 464 KB Output is correct
3 Correct 1 ms 464 KB Output is correct
4 Correct 0 ms 464 KB Output is correct
5 Correct 39 ms 37916 KB Output is correct
6 Correct 61 ms 47028 KB Output is correct
7 Correct 24 ms 24008 KB Output is correct
8 Correct 49 ms 47008 KB Output is correct
9 Correct 7 ms 7248 KB Output is correct
10 Correct 50 ms 47028 KB Output is correct
11 Correct 46 ms 47004 KB Output is correct
12 Correct 49 ms 46924 KB Output is correct
13 Correct 64 ms 46940 KB Output is correct
14 Correct 55 ms 47012 KB Output is correct
15 Correct 61 ms 47656 KB Output is correct
16 Correct 46 ms 47892 KB Output is correct
17 Correct 64 ms 47896 KB Output is correct
18 Correct 1 ms 464 KB Output is correct
19 Correct 1 ms 640 KB Output is correct
20 Correct 1 ms 592 KB Output is correct
21 Correct 1 ms 592 KB Output is correct
22 Correct 1 ms 592 KB Output is correct
23 Correct 1 ms 640 KB Output is correct
24 Correct 1 ms 592 KB Output is correct
25 Correct 1 ms 592 KB Output is correct
26 Correct 1 ms 720 KB Output is correct
27 Correct 1 ms 976 KB Output is correct
28 Correct 1 ms 1024 KB Output is correct
29 Correct 1 ms 976 KB Output is correct
30 Correct 1 ms 976 KB Output is correct
31 Correct 2 ms 976 KB Output is correct
32 Correct 0 ms 464 KB Output is correct
33 Correct 51 ms 47056 KB Output is correct
34 Correct 50 ms 47028 KB Output is correct
35 Correct 47 ms 47012 KB Output is correct
36 Correct 53 ms 46976 KB Output is correct
37 Correct 46 ms 47532 KB Output is correct
38 Correct 58 ms 47948 KB Output is correct
39 Correct 1 ms 512 KB Output is correct
40 Correct 29 ms 27132 KB Output is correct
41 Correct 72 ms 47012 KB Output is correct
42 Correct 48 ms 47012 KB Output is correct
43 Correct 67 ms 47040 KB Output is correct
44 Correct 47 ms 47512 KB Output is correct
45 Correct 48 ms 47896 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 512 KB Output is correct
2 Correct 0 ms 464 KB Output is correct
3 Correct 0 ms 464 KB Output is correct
4 Correct 267 ms 21436 KB Output is correct
5 Correct 775 ms 47016 KB Output is correct
6 Correct 641 ms 7888 KB Output is correct
7 Correct 1020 ms 46928 KB Output is correct
8 Correct 638 ms 16204 KB Output is correct
9 Correct 726 ms 47048 KB Output is correct
10 Correct 988 ms 47008 KB Output is correct
11 Correct 1197 ms 46988 KB Output is correct
12 Correct 1223 ms 46980 KB Output is correct
13 Correct 754 ms 47028 KB Output is correct
14 Correct 1075 ms 47548 KB Output is correct
15 Correct 881 ms 47932 KB Output is correct
16 Correct 895 ms 47868 KB Output is correct
17 Correct 0 ms 464 KB Output is correct
18 Correct 0 ms 464 KB Output is correct
19 Correct 2 ms 464 KB Output is correct
20 Correct 2 ms 592 KB Output is correct
21 Correct 2 ms 592 KB Output is correct
22 Correct 2 ms 592 KB Output is correct
23 Correct 2 ms 592 KB Output is correct
24 Correct 2 ms 592 KB Output is correct
25 Correct 1 ms 592 KB Output is correct
26 Correct 1 ms 720 KB Output is correct
27 Correct 22 ms 976 KB Output is correct
28 Correct 21 ms 976 KB Output is correct
29 Correct 17 ms 976 KB Output is correct
30 Correct 20 ms 976 KB Output is correct
31 Correct 17 ms 976 KB Output is correct
32 Correct 0 ms 464 KB Output is correct
33 Correct 30 ms 27208 KB Output is correct
34 Correct 56 ms 47032 KB Output is correct
35 Correct 45 ms 46936 KB Output is correct
36 Correct 51 ms 47036 KB Output is correct
37 Correct 58 ms 47552 KB Output is correct
38 Correct 46 ms 47948 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 512 KB Output is correct
2 Correct 0 ms 464 KB Output is correct
3 Correct 0 ms 464 KB Output is correct
4 Correct 267 ms 21436 KB Output is correct
5 Correct 775 ms 47016 KB Output is correct
6 Correct 641 ms 7888 KB Output is correct
7 Correct 1020 ms 46928 KB Output is correct
8 Correct 638 ms 16204 KB Output is correct
9 Correct 726 ms 47048 KB Output is correct
10 Correct 988 ms 47008 KB Output is correct
11 Correct 1197 ms 46988 KB Output is correct
12 Correct 1223 ms 46980 KB Output is correct
13 Correct 754 ms 47028 KB Output is correct
14 Correct 1075 ms 47548 KB Output is correct
15 Correct 881 ms 47932 KB Output is correct
16 Correct 895 ms 47868 KB Output is correct
17 Correct 0 ms 464 KB Output is correct
18 Correct 0 ms 464 KB Output is correct
19 Correct 2 ms 464 KB Output is correct
20 Correct 2 ms 592 KB Output is correct
21 Correct 2 ms 592 KB Output is correct
22 Correct 2 ms 592 KB Output is correct
23 Correct 2 ms 592 KB Output is correct
24 Correct 2 ms 592 KB Output is correct
25 Correct 1 ms 592 KB Output is correct
26 Correct 1 ms 720 KB Output is correct
27 Correct 22 ms 976 KB Output is correct
28 Correct 21 ms 976 KB Output is correct
29 Correct 17 ms 976 KB Output is correct
30 Correct 20 ms 976 KB Output is correct
31 Correct 17 ms 976 KB Output is correct
32 Correct 0 ms 464 KB Output is correct
33 Correct 30 ms 27208 KB Output is correct
34 Correct 56 ms 47032 KB Output is correct
35 Correct 45 ms 46936 KB Output is correct
36 Correct 51 ms 47036 KB Output is correct
37 Correct 58 ms 47552 KB Output is correct
38 Correct 46 ms 47948 KB Output is correct
39 Correct 0 ms 464 KB Output is correct
40 Correct 1 ms 464 KB Output is correct
41 Correct 0 ms 464 KB Output is correct
42 Correct 241 ms 21452 KB Output is correct
43 Correct 840 ms 47048 KB Output is correct
44 Correct 589 ms 7888 KB Output is correct
45 Correct 849 ms 47012 KB Output is correct
46 Correct 484 ms 16200 KB Output is correct
47 Correct 936 ms 47028 KB Output is correct
48 Correct 881 ms 47024 KB Output is correct
49 Correct 918 ms 47016 KB Output is correct
50 Correct 1006 ms 46996 KB Output is correct
51 Correct 829 ms 47032 KB Output is correct
52 Correct 966 ms 47552 KB Output is correct
53 Correct 828 ms 47936 KB Output is correct
54 Correct 903 ms 47932 KB Output is correct
55 Correct 0 ms 464 KB Output is correct
56 Correct 97 ms 47012 KB Output is correct
57 Correct 732 ms 47004 KB Output is correct
58 Correct 470 ms 8400 KB Output is correct
59 Correct 720 ms 47044 KB Output is correct
60 Correct 347 ms 16720 KB Output is correct
61 Correct 903 ms 47000 KB Output is correct
62 Correct 978 ms 47020 KB Output is correct
63 Correct 994 ms 47048 KB Output is correct
64 Correct 831 ms 47032 KB Output is correct
65 Correct 875 ms 46920 KB Output is correct
66 Correct 912 ms 47496 KB Output is correct
67 Correct 774 ms 47904 KB Output is correct
68 Correct 841 ms 47932 KB Output is correct
69 Correct 0 ms 464 KB Output is correct
70 Correct 1 ms 464 KB Output is correct
71 Correct 2 ms 592 KB Output is correct
72 Correct 2 ms 592 KB Output is correct
73 Correct 2 ms 592 KB Output is correct
74 Correct 3 ms 592 KB Output is correct
75 Correct 4 ms 592 KB Output is correct
76 Correct 1 ms 464 KB Output is correct
77 Correct 0 ms 464 KB Output is correct
78 Correct 2 ms 464 KB Output is correct
79 Correct 2 ms 592 KB Output is correct
80 Correct 2 ms 592 KB Output is correct
81 Correct 2 ms 592 KB Output is correct
82 Correct 2 ms 592 KB Output is correct
83 Correct 2 ms 592 KB Output is correct
84 Correct 1 ms 592 KB Output is correct
85 Correct 2 ms 592 KB Output is correct
86 Correct 17 ms 976 KB Output is correct
87 Correct 21 ms 976 KB Output is correct
88 Correct 17 ms 976 KB Output is correct
89 Correct 16 ms 988 KB Output is correct
90 Correct 17 ms 976 KB Output is correct
91 Correct 0 ms 592 KB Output is correct
92 Correct 1 ms 720 KB Output is correct
93 Correct 15 ms 976 KB Output is correct
94 Correct 17 ms 976 KB Output is correct
95 Correct 17 ms 976 KB Output is correct
96 Correct 18 ms 976 KB Output is correct
97 Correct 17 ms 976 KB Output is correct
98 Correct 0 ms 464 KB Output is correct
99 Correct 48 ms 47012 KB Output is correct
100 Correct 63 ms 46976 KB Output is correct
101 Correct 45 ms 47048 KB Output is correct
102 Correct 48 ms 47048 KB Output is correct
103 Correct 47 ms 47532 KB Output is correct
104 Correct 47 ms 47908 KB Output is correct
105 Correct 0 ms 464 KB Output is correct
106 Correct 29 ms 27184 KB Output is correct
107 Correct 47 ms 47032 KB Output is correct
108 Correct 56 ms 46996 KB Output is correct
109 Correct 48 ms 47008 KB Output is correct
110 Correct 45 ms 47552 KB Output is correct
111 Correct 44 ms 47896 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 464 KB Output is correct
2 Correct 0 ms 464 KB Output is correct
3 Correct 149 ms 37408 KB Output is correct
4 Correct 1135 ms 47012 KB Output is correct
5 Correct 867 ms 23640 KB Output is correct
6 Correct 1123 ms 47028 KB Output is correct
7 Correct 772 ms 32052 KB Output is correct
8 Correct 1014 ms 47028 KB Output is correct
9 Correct 1 ms 464 KB Output is correct
10 Correct 0 ms 464 KB Output is correct
11 Correct 1 ms 464 KB Output is correct
12 Correct 0 ms 464 KB Output is correct
13 Incorrect 2 ms 464 KB Output isn't correct
14 Halted 0 ms 0 KB -