Submission #716719

# Submission time Handle Problem Language Result Execution time Memory
716719 2023-03-30T23:22:41 Z aryan12 Rainforest Jumps (APIO21_jumps) C++17
27 / 100
1303 ms 79744 KB
#include "jumps.h"
#include <bits/stdc++.h>
#include <vector>
using namespace std;

const int MAXN = 2e5 + 5;
int dp_min[19][MAXN], dp_max[19][MAXN];
vector<int> seg[MAXN * 4]; // at position i stores the value H[i]
vector<int> H, REV;
int N;

void Build(int left, int right, int pos)
{
    if(left == right)
    {
        seg[pos].push_back(H[left]);
        return;
    }
    int mid = (left + right) / 2;
    Build(left, mid, pos * 2);
    Build(mid + 1, right, pos * 2 + 1);
    merge(seg[pos * 2].begin(), seg[pos * 2].end(), seg[pos * 2 + 1].begin(), seg[pos * 2 + 1].end(), back_inserter(seg[pos]));
}

int Query(int l, int r, int pos, int ql, int qr, int qval)
{
    // cout << "qpos = " << ql << ", " << qr << ", qval = " << qval << "\n";
    if(ql > r || l > qr)
    {
        return -1;
    }
    if(ql <= l && r <= qr)
    {
        int cur_pos = lower_bound(seg[pos].begin(), seg[pos].end(), qval) - seg[pos].begin() - 1;
        if(cur_pos == -1)
        {
            return -1;
        }
        return seg[pos][cur_pos];
    }
    int mid = (l + r) >> 1;
    return max(Query(l, mid, pos * 2, ql, qr, qval), Query(mid + 1, r, pos * 2 + 1, ql, qr, qval));
}

void init(int n, std::vector<int> h) 
{
    stack<int> s;
    N = n;
    REV.resize(N + 1);
    for(int i = 0; i < h.size(); i++)
    {
        H.push_back(h[i]);
        REV[H[i]] = i;
    }
    Build(0, N - 1, 1);
    for(int i = 0; i < N; i++)
    {
        while(!s.empty() && H[s.top()] < H[i])
        {
            s.pop();
        }
        if(s.empty())
        {
            dp_min[0][i] = -1;
        }
        else
        {
            dp_min[0][i] = s.top();
        }
        s.push(i);
    }
    while(!s.empty())
    {
        s.pop();
    }
    for(int i = N - 1; i >= 0; i--)
    {
        while(!s.empty() && H[s.top()] < H[i])
        {
            s.pop();
        }
        if(s.empty())
        {
            dp_max[0][i] = -1;
        }
        else
        {
            dp_max[0][i] = s.top();
        }
        s.push(i);
        if(dp_min[0][i] == -1 || (dp_max[0][i] != -1 && H[dp_max[0][i]] < H[dp_min[0][i]]))
        {
            swap(dp_min[0][i], dp_max[0][i]);
        }
    }
    for(int i = 1; i < 19; i++)
    {
        for(int j = 0; j < N; j++)
        {
            dp_min[i][j] = (dp_min[i - 1][j] == -1) ? -1 : dp_min[i - 1][dp_min[i - 1][j]];
            dp_max[i][j] = (dp_max[i - 1][j] == -1) ? -1 : dp_max[i - 1][dp_max[i - 1][j]];
            assert(dp_max[i][j] == -1 || H[dp_max[i][j]] >= H[dp_min[i][j]]);
        }
    }
}

int minimum_jumps(int A, int B, int C, int D) 
{
    // A = B && C = D
    // cout << "---- ---- ---- ---- ----\n";
    // cout << "A = " << A << ", B = " << B << "\n";
    int query_result = Query(0, N - 1, 1, A, B, H[C]);
    // cout << "query_result = " << query_result << "\n";
    if(query_result == -1)
    {
        return -1;
    }
    A = REV[query_result];
    B = A;
    // cout << "A = " << A << ", B = " << B << "\n";
    // cout << "---- ---- ---- ---- ----\n";
    int ans = 0;
    for(int i = 18; i >= 0; i--)
    {
        if(dp_max[i][A] != -1 && H[dp_max[i][A]] <= H[C])
        {
            ans += (1 << i);
            A = dp_max[i][A];
        }
    }
    for(int i = 18; i >= 0; i--)
    {
        if(dp_min[i][A] != -1 && H[dp_min[i][A]] <= H[C])
        {
            ans += (1 << i);
            A = dp_min[i][A];
        }
    }
    if(H[A] != H[C])
    {
        return -1;
    }
    return ans;
}

Compilation message

jumps.cpp: In function 'void init(int, std::vector<int>)':
jumps.cpp:50:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   50 |     for(int i = 0; i < h.size(); i++)
      |                    ~~^~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 10 ms 19284 KB Output is correct
2 Correct 11 ms 19280 KB Output is correct
3 Correct 190 ms 69396 KB Output is correct
4 Correct 1303 ms 79744 KB Output is correct
5 Correct 914 ms 49312 KB Output is correct
6 Correct 1198 ms 79676 KB Output is correct
7 Correct 1081 ms 62908 KB Output is correct
8 Correct 1276 ms 79648 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 10 ms 19280 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 10 ms 19280 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 10 ms 19292 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 10 ms 19280 KB Output is correct
2 Correct 10 ms 19280 KB Output is correct
3 Correct 11 ms 19280 KB Output is correct
4 Correct 294 ms 46988 KB Output is correct
5 Correct 1022 ms 79616 KB Output is correct
6 Correct 549 ms 29140 KB Output is correct
7 Correct 845 ms 79632 KB Output is correct
8 Correct 458 ms 40960 KB Output is correct
9 Correct 1119 ms 79608 KB Output is correct
10 Correct 1156 ms 79672 KB Output is correct
11 Correct 1224 ms 79640 KB Output is correct
12 Correct 1232 ms 79548 KB Output is correct
13 Correct 1144 ms 79612 KB Output is correct
14 Correct 1224 ms 79620 KB Output is correct
15 Correct 1169 ms 79644 KB Output is correct
16 Correct 1160 ms 79600 KB Output is correct
17 Correct 13 ms 19196 KB Output is correct
18 Correct 11 ms 19280 KB Output is correct
19 Correct 12 ms 19228 KB Output is correct
20 Correct 12 ms 19272 KB Output is correct
21 Correct 13 ms 19280 KB Output is correct
22 Correct 13 ms 19240 KB Output is correct
23 Correct 13 ms 19244 KB Output is correct
24 Correct 13 ms 19280 KB Output is correct
25 Correct 10 ms 19280 KB Output is correct
26 Correct 12 ms 19408 KB Output is correct
27 Correct 28 ms 19792 KB Output is correct
28 Correct 31 ms 19792 KB Output is correct
29 Correct 31 ms 19792 KB Output is correct
30 Correct 32 ms 19764 KB Output is correct
31 Correct 27 ms 19792 KB Output is correct
32 Correct 10 ms 19280 KB Output is correct
33 Correct 79 ms 52992 KB Output is correct
34 Correct 134 ms 79668 KB Output is correct
35 Correct 131 ms 79628 KB Output is correct
36 Correct 131 ms 79568 KB Output is correct
37 Correct 122 ms 79604 KB Output is correct
38 Correct 115 ms 79632 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 19280 KB Output is correct
2 Correct 10 ms 19280 KB Output is correct
3 Correct 11 ms 19280 KB Output is correct
4 Correct 294 ms 46988 KB Output is correct
5 Correct 1022 ms 79616 KB Output is correct
6 Correct 549 ms 29140 KB Output is correct
7 Correct 845 ms 79632 KB Output is correct
8 Correct 458 ms 40960 KB Output is correct
9 Correct 1119 ms 79608 KB Output is correct
10 Correct 1156 ms 79672 KB Output is correct
11 Correct 1224 ms 79640 KB Output is correct
12 Correct 1232 ms 79548 KB Output is correct
13 Correct 1144 ms 79612 KB Output is correct
14 Correct 1224 ms 79620 KB Output is correct
15 Correct 1169 ms 79644 KB Output is correct
16 Correct 1160 ms 79600 KB Output is correct
17 Correct 13 ms 19196 KB Output is correct
18 Correct 11 ms 19280 KB Output is correct
19 Correct 12 ms 19228 KB Output is correct
20 Correct 12 ms 19272 KB Output is correct
21 Correct 13 ms 19280 KB Output is correct
22 Correct 13 ms 19240 KB Output is correct
23 Correct 13 ms 19244 KB Output is correct
24 Correct 13 ms 19280 KB Output is correct
25 Correct 10 ms 19280 KB Output is correct
26 Correct 12 ms 19408 KB Output is correct
27 Correct 28 ms 19792 KB Output is correct
28 Correct 31 ms 19792 KB Output is correct
29 Correct 31 ms 19792 KB Output is correct
30 Correct 32 ms 19764 KB Output is correct
31 Correct 27 ms 19792 KB Output is correct
32 Correct 10 ms 19280 KB Output is correct
33 Correct 79 ms 52992 KB Output is correct
34 Correct 134 ms 79668 KB Output is correct
35 Correct 131 ms 79628 KB Output is correct
36 Correct 131 ms 79568 KB Output is correct
37 Correct 122 ms 79604 KB Output is correct
38 Correct 115 ms 79632 KB Output is correct
39 Correct 10 ms 19280 KB Output is correct
40 Correct 11 ms 19188 KB Output is correct
41 Correct 11 ms 19280 KB Output is correct
42 Correct 294 ms 46968 KB Output is correct
43 Correct 1172 ms 79552 KB Output is correct
44 Correct 742 ms 29208 KB Output is correct
45 Correct 676 ms 79632 KB Output is correct
46 Correct 660 ms 40936 KB Output is correct
47 Correct 1132 ms 79588 KB Output is correct
48 Correct 1232 ms 79604 KB Output is correct
49 Correct 1232 ms 79608 KB Output is correct
50 Correct 1168 ms 79640 KB Output is correct
51 Correct 1000 ms 79636 KB Output is correct
52 Correct 1099 ms 79600 KB Output is correct
53 Correct 1033 ms 79548 KB Output is correct
54 Correct 967 ms 79612 KB Output is correct
55 Correct 11 ms 19280 KB Output is correct
56 Incorrect 204 ms 79552 KB Output isn't correct
57 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 10 ms 19284 KB Output is correct
2 Correct 11 ms 19280 KB Output is correct
3 Correct 190 ms 69396 KB Output is correct
4 Correct 1303 ms 79744 KB Output is correct
5 Correct 914 ms 49312 KB Output is correct
6 Correct 1198 ms 79676 KB Output is correct
7 Correct 1081 ms 62908 KB Output is correct
8 Correct 1276 ms 79648 KB Output is correct
9 Incorrect 10 ms 19280 KB Output isn't correct
10 Halted 0 ms 0 KB -