Submission #442152

# Submission time Handle Problem Language Result Execution time Memory
442152 2021-07-07T08:31:43 Z Haruto810198 Rainforest Jumps (APIO21_jumps) C++17
44 / 100
1404 ms 67764 KB
#include <bits/stdc++.h>

using namespace std;

//#define int long long
#define double long double

#define FOR(i,l,r,d) for(int i=(l);i<=(r);i+=(d))
#define szof(x) ((int)(x).size())

#define vi vector<int>
#define pii pair<int,int>

#define F first
#define S second

#define pb push_back
#define eb emplace_back
#define mkp make_pair

#define V st[cidx]
#define LC st[cidx*2]
#define RC st[cidx*2+1]
#define lsb(x) ((x)&(-(x)))

//const int INF = 2147483647;
//const int LNF = INF*INF;
const int INF = 1000000007;
const int MOD = 1000000007;
const int mod = 998244353;
const double eps = 1e-12;

//#pragma GCC optimize("Ofast")
//#pragma GCC optimize("unroll-loops")

const int MAX = 200010;
const int lgMAX = 20;

int n;
int h[MAX];
int edge_l[MAX], edge_r[MAX];
int anc_l[MAX][lgMAX];
int anc_hi[MAX][lgMAX], anc_lo[MAX][lgMAX];
int Max[MAX][lgMAX];
int dest_Max;

int query_Max(int L, int R){ /// [L, R]
    int len = R - L + 1;
    int lglen = __lg(len);
    return max(Max[L][lglen], Max[R - (1<<lglen) + 1][lglen]);
}

int get_start(int A, int B){
    int ret = B;
    for(int j=lgMAX-1; j>=0; j--){
        if( anc_l[ret][j] != INF and h[anc_l[ret][j]] < dest_Max and anc_l[ret][j] >= A ){
            ret = anc_l[ret][j];
        }
    }
    return ret;
}

pii jump_hi(int v){
    int ret = 0;
    for(int j=lgMAX-1; j>=0; j--){
        if( anc_hi[v][j] != INF and h[anc_hi[v][j]] <= dest_Max ){
            v = anc_hi[v][j];
            ret += (1<<j);
        }
    }
    return mkp(v, ret);
}

int jump_lo(int v){
    int ret = 0;
    for(int j=lgMAX-1; j>=0; j--){
        if( anc_lo[v][j] != INF and h[anc_lo[v][j]] <= dest_Max ){
            v = anc_lo[v][j];
            ret += (1<<j);
        }
    }
    if( h[v] > dest_Max ){
        ret = INF;
    }
    if( h[v] < dest_Max and ( anc_lo[v][0] == INF or h[anc_lo[v][0]] > dest_Max ) ){
        ret = INF;
    }
    return ret;
}

void init(int N, vi H){

    /// input
    n = N;
    FOR(i, 0, n-1, 1){
        h[i] = H[i];
    }

    /// monotonic stack
    FOR(i, 0, n-1, 1){
        edge_l[i] = INF; /// INF -> no edge
        edge_r[i] = INF;
    }

    vi stk;
    FOR(i, 0, n-1, 1){

        while(!stk.empty() and h[stk.back()] < h[i]){
            stk.pop_back();
        }

        if(!stk.empty()){
            edge_l[i] = stk.back();
        }
        stk.pb(i);

    }

    stk.clear();

    for(int i=n-1; i>=0; i--){

        while(!stk.empty() and h[stk.back()] < h[i]){
            stk.pop_back();
        }

        if(!stk.empty()){
            edge_r[i] = stk.back();
        }
        stk.pb(i);

    }

    /// 2^k-th ancestor
    FOR(i, 0, n-1, 1){
        if(edge_l[i]==INF and edge_r[i]==INF){
            anc_hi[i][0] = INF;
            anc_lo[i][0] = INF;
        }
        else if(edge_l[i]==INF or edge_r[i]==INF){
            anc_hi[i][0] = INF;
            anc_lo[i][0] = min(edge_l[i], edge_r[i]);
        }
        else{
            anc_hi[i][0] = (h[edge_l[i]] > h[edge_r[i]]) ? edge_l[i] : edge_r[i];
            anc_lo[i][0] = (h[edge_l[i]] < h[edge_r[i]]) ? edge_l[i] : edge_r[i];
        }
        anc_l [i][0] = edge_l[i];
    }
    FOR(j, 1, lgMAX-1, 1){
        FOR(i, 0, n-1, 1){
            anc_hi[i][j] = (anc_hi[i][j-1]!=INF) ? anc_hi[ anc_hi[i][j-1] ][j-1] : INF;
            anc_lo[i][j] = (anc_lo[i][j-1]!=INF) ? anc_lo[ anc_lo[i][j-1] ][j-1] : INF;
            anc_l [i][j] = (anc_l [i][j-1]!=INF) ? anc_l [ anc_l [i][j-1] ][j-1] : INF;
        }
    }

    /// sparse table for Max query
    FOR(i, 0, n-1, 1){
        Max[i][0] = h[i];
    }
    FOR(j, 1, lgMAX-1, 1){
        FOR(i, 0, n-1, 1){
            if(i + (1<<j-1) <= n-1){
                Max[i][j] = max(Max[i][j-1], Max[i + (1<<j-1)][j-1]);
            }
            else{
                Max[i][j] = Max[i][j-1];
            }
        }
    }

}

int minimum_jumps(int A, int B, int C, int D){

    dest_Max = query_Max(C, D);
    //cerr<<"dest_Max = "<<dest_Max<<endl;

    int start = get_start(A, B);
    //cerr<<"start = "<<start<<endl;

    int v = start, res;

    pii tmp = jump_hi(start);
    v = tmp.F;
    res = tmp.S;
    //cerr<<"jump_hi : v = "<<tmp.F<<", res += "<<tmp.S<<endl;

    res += jump_lo(v);
    //cerr<<"jump_lo : res += "<<jump_lo(v)<<endl;

    //cerr<<"res = "<<res<<endl;

    if(res < INF){
        return res;
    }
    else{
        return -1;
    }

}

/*
signed main(){

    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    init(7, {3, 2, 1, 6, 4, 5, 7});

    cerr<<minimum_jumps(4, 4, 6, 6)<<endl; /// 2
    cerr<<minimum_jumps(1, 3, 5, 6)<<endl; /// 1
    cerr<<minimum_jumps(0, 1, 2, 2)<<endl; /// -1

    return 0;

}
*/

Compilation message

jumps.cpp: In function 'void init(int, std::vector<int>)':
jumps.cpp:164:25: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
  164 |             if(i + (1<<j-1) <= n-1){
      |                        ~^~
jumps.cpp:165:59: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
  165 |                 Max[i][j] = max(Max[i][j-1], Max[i + (1<<j-1)][j-1]);
      |                                                          ~^~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 328 KB Output is correct
2 Correct 0 ms 328 KB Output is correct
3 Incorrect 279 ms 53904 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 328 KB Output is correct
2 Correct 0 ms 328 KB Output is correct
3 Correct 0 ms 328 KB Output is correct
4 Correct 0 ms 328 KB Output is correct
5 Incorrect 2 ms 328 KB Output isn't correct
6 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 328 KB Output is correct
2 Correct 0 ms 328 KB Output is correct
3 Correct 0 ms 328 KB Output is correct
4 Correct 0 ms 328 KB Output is correct
5 Incorrect 2 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 0 ms 328 KB Output is correct
5 Correct 144 ms 53796 KB Output is correct
6 Correct 183 ms 66788 KB Output is correct
7 Correct 78 ms 34308 KB Output is correct
8 Correct 181 ms 66920 KB Output is correct
9 Correct 18 ms 10296 KB Output is correct
10 Correct 183 ms 66828 KB Output is correct
11 Incorrect 156 ms 67712 KB Output isn't correct
12 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 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 371 ms 30656 KB Output is correct
5 Correct 1311 ms 66848 KB Output is correct
6 Correct 841 ms 11208 KB Output is correct
7 Correct 1277 ms 66700 KB Output is correct
8 Correct 629 ms 23236 KB Output is correct
9 Correct 1350 ms 66796 KB Output is correct
10 Correct 1310 ms 67716 KB Output is correct
11 Correct 1130 ms 67684 KB Output is correct
12 Correct 992 ms 67620 KB Output is correct
13 Correct 1131 ms 66736 KB Output is correct
14 Correct 1319 ms 67304 KB Output is correct
15 Correct 1114 ms 67708 KB Output is correct
16 Correct 1253 ms 67700 KB Output is correct
17 Correct 0 ms 328 KB Output is correct
18 Correct 0 ms 328 KB Output is correct
19 Correct 2 ms 328 KB Output is correct
20 Correct 3 ms 328 KB Output is correct
21 Correct 2 ms 328 KB Output is correct
22 Correct 2 ms 328 KB Output is correct
23 Correct 3 ms 328 KB Output is correct
24 Correct 3 ms 328 KB Output is correct
25 Correct 0 ms 328 KB Output is correct
26 Correct 2 ms 548 KB Output is correct
27 Correct 25 ms 968 KB Output is correct
28 Correct 26 ms 968 KB Output is correct
29 Correct 22 ms 968 KB Output is correct
30 Correct 26 ms 968 KB Output is correct
31 Correct 17 ms 968 KB Output is correct
32 Correct 0 ms 328 KB Output is correct
33 Correct 93 ms 38724 KB Output is correct
34 Correct 167 ms 66744 KB Output is correct
35 Correct 161 ms 67592 KB Output is correct
36 Correct 167 ms 66708 KB Output is correct
37 Correct 206 ms 67256 KB Output is correct
38 Correct 171 ms 67620 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 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 371 ms 30656 KB Output is correct
5 Correct 1311 ms 66848 KB Output is correct
6 Correct 841 ms 11208 KB Output is correct
7 Correct 1277 ms 66700 KB Output is correct
8 Correct 629 ms 23236 KB Output is correct
9 Correct 1350 ms 66796 KB Output is correct
10 Correct 1310 ms 67716 KB Output is correct
11 Correct 1130 ms 67684 KB Output is correct
12 Correct 992 ms 67620 KB Output is correct
13 Correct 1131 ms 66736 KB Output is correct
14 Correct 1319 ms 67304 KB Output is correct
15 Correct 1114 ms 67708 KB Output is correct
16 Correct 1253 ms 67700 KB Output is correct
17 Correct 0 ms 328 KB Output is correct
18 Correct 0 ms 328 KB Output is correct
19 Correct 2 ms 328 KB Output is correct
20 Correct 3 ms 328 KB Output is correct
21 Correct 2 ms 328 KB Output is correct
22 Correct 2 ms 328 KB Output is correct
23 Correct 3 ms 328 KB Output is correct
24 Correct 3 ms 328 KB Output is correct
25 Correct 0 ms 328 KB Output is correct
26 Correct 2 ms 548 KB Output is correct
27 Correct 25 ms 968 KB Output is correct
28 Correct 26 ms 968 KB Output is correct
29 Correct 22 ms 968 KB Output is correct
30 Correct 26 ms 968 KB Output is correct
31 Correct 17 ms 968 KB Output is correct
32 Correct 0 ms 328 KB Output is correct
33 Correct 93 ms 38724 KB Output is correct
34 Correct 167 ms 66744 KB Output is correct
35 Correct 161 ms 67592 KB Output is correct
36 Correct 167 ms 66708 KB Output is correct
37 Correct 206 ms 67256 KB Output is correct
38 Correct 171 ms 67620 KB Output is correct
39 Correct 1 ms 328 KB Output is correct
40 Correct 0 ms 328 KB Output is correct
41 Correct 1 ms 328 KB Output is correct
42 Correct 304 ms 30656 KB Output is correct
43 Correct 1203 ms 66732 KB Output is correct
44 Correct 770 ms 11208 KB Output is correct
45 Correct 1142 ms 66880 KB Output is correct
46 Correct 661 ms 23232 KB Output is correct
47 Correct 1233 ms 66724 KB Output is correct
48 Correct 1213 ms 67636 KB Output is correct
49 Correct 1404 ms 67640 KB Output is correct
50 Correct 1190 ms 67636 KB Output is correct
51 Correct 1159 ms 66752 KB Output is correct
52 Correct 1373 ms 67216 KB Output is correct
53 Correct 1150 ms 67628 KB Output is correct
54 Correct 1255 ms 67652 KB Output is correct
55 Correct 1 ms 328 KB Output is correct
56 Correct 224 ms 66604 KB Output is correct
57 Correct 1161 ms 66752 KB Output is correct
58 Correct 488 ms 11932 KB Output is correct
59 Correct 1253 ms 66704 KB Output is correct
60 Correct 482 ms 23872 KB Output is correct
61 Correct 1095 ms 66788 KB Output is correct
62 Correct 1381 ms 67728 KB Output is correct
63 Correct 1348 ms 67504 KB Output is correct
64 Correct 1082 ms 67408 KB Output is correct
65 Correct 1203 ms 66788 KB Output is correct
66 Correct 1394 ms 67256 KB Output is correct
67 Correct 1188 ms 67640 KB Output is correct
68 Correct 1102 ms 67712 KB Output is correct
69 Correct 1 ms 328 KB Output is correct
70 Correct 1 ms 328 KB Output is correct
71 Correct 3 ms 328 KB Output is correct
72 Correct 2 ms 328 KB Output is correct
73 Correct 3 ms 328 KB Output is correct
74 Correct 3 ms 328 KB Output is correct
75 Correct 3 ms 328 KB Output is correct
76 Correct 1 ms 328 KB Output is correct
77 Correct 1 ms 328 KB Output is correct
78 Correct 2 ms 328 KB Output is correct
79 Correct 3 ms 328 KB Output is correct
80 Correct 4 ms 328 KB Output is correct
81 Correct 2 ms 328 KB Output is correct
82 Correct 2 ms 328 KB Output is correct
83 Correct 2 ms 328 KB Output is correct
84 Correct 1 ms 328 KB Output is correct
85 Correct 5 ms 328 KB Output is correct
86 Correct 25 ms 968 KB Output is correct
87 Correct 17 ms 968 KB Output is correct
88 Correct 25 ms 968 KB Output is correct
89 Correct 28 ms 968 KB Output is correct
90 Correct 28 ms 968 KB Output is correct
91 Correct 0 ms 328 KB Output is correct
92 Correct 1 ms 456 KB Output is correct
93 Correct 25 ms 984 KB Output is correct
94 Correct 28 ms 968 KB Output is correct
95 Correct 25 ms 968 KB Output is correct
96 Correct 13 ms 968 KB Output is correct
97 Correct 26 ms 968 KB Output is correct
98 Correct 0 ms 328 KB Output is correct
99 Correct 172 ms 66708 KB Output is correct
100 Correct 170 ms 66756 KB Output is correct
101 Correct 167 ms 67540 KB Output is correct
102 Correct 163 ms 66736 KB Output is correct
103 Correct 195 ms 67220 KB Output is correct
104 Correct 158 ms 67764 KB Output is correct
105 Correct 0 ms 328 KB Output is correct
106 Correct 93 ms 38796 KB Output is correct
107 Correct 164 ms 66880 KB Output is correct
108 Correct 158 ms 67596 KB Output is correct
109 Correct 176 ms 66796 KB Output is correct
110 Correct 202 ms 67292 KB Output is correct
111 Correct 157 ms 67636 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 328 KB Output is correct
2 Correct 0 ms 328 KB Output is correct
3 Incorrect 279 ms 53904 KB Output isn't correct
4 Halted 0 ms 0 KB -