Submission #968592

#TimeUsernameProblemLanguageResultExecution timeMemory
968592steveonalexRainforest Jumps (APIO21_jumps)C++17
0 / 100
137 ms32484 KiB
#include <bits/stdc++.h>
#include "jumps.h"
 
using namespace std;
 
typedef long long ll;
typedef unsigned long long ull;
 
#define ALL(v) (v).begin(), (v).end()
#define MASK(i) (1LL << (i))
#define GETBIT(mask, i) (((mask) >> (i)) & 1)
 
// mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
mt19937_64 rng(1);
ll rngesus(ll l, ll r){return ((ull) rng()) % (r - l + 1) + l;}
 
ll max(ll a, ll b){return (a > b) ? a : b;}
ll min(ll a, ll b){return (a < b) ? a : b;}
 
ll LASTBIT(ll mask){return mask & (-mask);}
ll pop_cnt(ll mask){return __builtin_popcountll(mask);}
ll ctz(ll mask){return __builtin_ctzll(mask);}
ll clz(ll mask){return __builtin_clzll(mask);}
ll logOf(ll mask){return 63 - clz(mask);}
 
template <class T1, class T2>
    bool minimize(T1 &a, T2 b){
        if (a > b){a = b; return true;}
        return false;
    }
template <class T1, class T2>
    bool maximize(T1 &a, T2 b){
        if (a < b){a = b; return true;}
        return false;
    }
template <class T>
    void printArr(T& a, string separator = " ", string finish = "\n", ostream& out = cout){
        for(auto i: a) out << i << separator;
        out << finish;
    }
template <class T>
    void remove_dup(vector<T> &a){
        sort(ALL(a));
        a.resize(unique(ALL(a)) - a.begin());
    }

const int LOG_N = 18;
const int N = 2e5 + 69;
int n;
int h[N], pref[N], suff[N];

int nxt[LOG_N][N], lnxt[LOG_N][N];

void init(int _n, vector<int> H) {
    n = _n;
    for(int i = 0; i<n; ++i) h[i] = H[i];

    vector<int> st;
    for(int i = 0; i<n; ++i){
        while(st.size() && h[st.back()] <= h[i]) st.pop_back();
        if (st.empty()) pref[i] = n;
        else pref[i] = st.back();
        st.push_back(i);
    }
    st.clear();
    for(int i = n-1; i>=0; --i){
        while(st.size() && h[st.back()] <= h[i]) st.pop_back();
        if (st.empty()) suff[i] = n;
        else suff[i] = st.back();
        st.push_back(i);
    }

    for(int i = 0; i<n; ++i){
        if (h[pref[i]] > h[suff[i]]) nxt[0][i] = pref[i];
        else nxt[0][i] = suff[i];
        lnxt[0][i] = suff[i];
    }
    lnxt[0][n] = nxt[0][n] = n;

    for(int j = 1; j < LOG_N; ++j){
        for(int i = 0; i<=n; ++i){
            nxt[j][i] = nxt[j-1][nxt[j-1][i]];
            lnxt[j][i]= lnxt[j-1][lnxt[j-1][i]];
        }
    }
}

int minimum_jumps(int A, int B, int C, int D) {
    if (A != B || C != D) exit(1);
    if (h[A] > h[C]) return -1;
    int ans = 0;
    int x = A;
    for(int j = LOG_N - 1; j>=0; --j){
        if (nxt[j][x] == n) continue;
        if (h[nxt[j][x]] < h[C]) {
            x = nxt[j][x]; 
            ans += MASK(j);
        }
    }

    for(int j= LOG_N - 1; j>=0; --j){
        if (lnxt[j][x] < C) {
            x = lnxt[j][x];
            ans += MASK(j);
        }
    }
    ans++;

    return ans;
}


// int main(void){
//     ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);

//     int n; cin >> n;
//     vector<int> h(n);
//     for(int i = 0; i<n;++i) cin >> h[i];

//     init(n, h);
//     int q; cin >> q;
//     for(int i = 0; i<q; ++i){
//         int a, b, c, d; cin >> a >> b >> c >> d;
//         cout << minimum_jumps(a, b, c, d) << "\n";
//     }

//     return 0;
// }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...