Submission #442260

# Submission time Handle Problem Language Result Execution time Memory
442260 2021-07-07T10:53:43 Z Haruto810198 Rainforest Jumps (APIO21_jumps) C++17
4 / 100
1590 ms 52116 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], anc_r[MAX][lgMAX];
int anc_hi[MAX][lgMAX];

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;
        }
        else if(edge_l[i]==INF or edge_r[i]==INF){
            anc_hi[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_l[i][0] = edge_l[i];
        anc_r[i][0] = edge_r[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_l [i][j] = (anc_l [i][j-1]!=INF) ? anc_l [ anc_l [i][j-1] ][j-1] : INF;
            anc_r [i][j] = (anc_r [i][j-1]!=INF) ? anc_r [ anc_r [i][j-1] ][j-1] : INF;
        }
    }

}

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

    int ret = 0;

    for(int j=lgMAX-1; j>=0; j--){
        if(anc_l[D][j]!=INF and anc_l[D][j] >= C){
            D = anc_l[D][j];
        }
    }
    //cerr<<"D = "<<D<<endl;

    int start = B;
    for(int j=lgMAX-1; j>=0; j--){
        if(anc_l[start][j]!=INF and h[anc_l[start][j]] < h[D] and anc_l[start][j] >= A){
            start = anc_l[start][j];
        }
    }
    //cerr<<"start = "<<start<<endl;

    int wall = B;
    for(int j=lgMAX-1; j>=0; j--){
        if(anc_r[wall][j]!=INF and anc_r[wall][j] < C){
            wall = anc_r[wall][j];
        }
    }
    //cerr<<"wall = "<<wall<<endl;

    if(edge_r[wall]==-1 or h[edge_r[wall]] > h[D]) return -1;

    int v = start;
    for(int j=lgMAX-1; j>=0; j--){
        if(anc_hi[v][j]!=INF and h[anc_hi[v][j]] <= h[wall]){
            v = anc_hi[v][j];
            ret += (1<<j);
        }
    }

    if(h[v] < h[wall] and anc_hi[v][0]!=INF and h[anc_hi[v][0]] <= h[D]){
        v = anc_hi[v][0];
        ret++;
    }

    for(int j=lgMAX-1; j>=0; j--){
        if(anc_r[v][j]!=INF and anc_r[v][j] < C){
            v = anc_r[v][j];
            ret += (1<<j);
        }
    }

    if(C <= v and v <= D){
        return ret;
    }
    else if(C <= edge_r[v] and edge_r[v] <= D){
        return ret + 1;
    }
    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;

}
*/

# 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 243 ms 41524 KB Output is correct
4 Correct 1468 ms 52116 KB Output is correct
5 Correct 1139 ms 26424 KB Output is correct
6 Correct 1590 ms 52020 KB Output is correct
7 Correct 1052 ms 35636 KB Output is correct
8 Correct 1300 ms 51988 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 328 KB Output is correct
2 Correct 0 ms 328 KB Output is correct
3 Runtime error 3 ms 456 KB Execution killed with signal 11
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 328 KB Output is correct
2 Correct 0 ms 328 KB Output is correct
3 Runtime error 3 ms 456 KB Execution killed with signal 11
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 328 KB Output is correct
2 Correct 0 ms 328 KB Output is correct
3 Runtime error 3 ms 456 KB Execution killed with signal 11
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 328 KB Output is correct
2 Runtime error 3 ms 584 KB Execution killed with signal 11
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 328 KB Output is correct
2 Runtime error 3 ms 584 KB Execution killed with signal 11
3 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 243 ms 41524 KB Output is correct
4 Correct 1468 ms 52116 KB Output is correct
5 Correct 1139 ms 26424 KB Output is correct
6 Correct 1590 ms 52020 KB Output is correct
7 Correct 1052 ms 35636 KB Output is correct
8 Correct 1300 ms 51988 KB Output is correct
9 Correct 1 ms 328 KB Output is correct
10 Correct 0 ms 328 KB Output is correct
11 Runtime error 3 ms 456 KB Execution killed with signal 11
12 Halted 0 ms 0 KB -