제출 #766269

#제출 시각아이디문제언어결과실행 시간메모리
766269GrindMachine밀림 점프 (APIO21_jumps)C++17
77 / 100
4021 ms69232 KiB
// Om Namah Shivaya

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

template<typename T> using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
typedef long long int ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL)
#define pb push_back
#define endl '\n'
#define sz(a) a.size()
#define setbits(x) __builtin_popcountll(x)
#define ff first
#define ss second
#define conts continue
#define ceil2(x, y) ((x + y - 1) / (y))
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define yes cout << "Yes" << endl
#define no cout << "No" << endl

#define rep(i, n) for(int i = 0; i < n; ++i)
#define rep1(i, n) for(int i = 1; i <= n; ++i)
#define rev(i, s, e) for(int i = s; i >= e; --i)
#define trav(i, a) for(auto &i : a)

template<typename T>
void amin(T &a, T b) {
    a = min(a, b);
}

template<typename T>
void amax(T &a, T b) {
    a = max(a, b);
}

#ifdef LOCAL
#include "debug.h"
#else
#define debug(x) 42
#endif

/*

read the edi a long time ago: have faint memories from it

*/

const int MOD = 1e9 + 7;
const int N = 2e5 + 5;
const int inf1 = int(1e9) + 5;
const ll inf2 = ll(1e18) + 5;

#include "jumps.h"

template<typename T>
struct sparse_table {
    /*============================*/

    T merge(T a, T b) {
        return max(a,b);
    }

    /*============================*/

    vector<vector<T>> table;
    vector<int> bin_log;
    int LOG = 0;

    sparse_table() {

    }

    sparse_table(vector<T> &a, int n) {
        while ((1 << LOG) <= n) LOG++;

        table = vector<vector<T>>(n, vector<T>(LOG));
        bin_log = vector<int>(n + 1);

        rep(i, n) table[i][0] = a[i];

        rep1(j, LOG - 1) {
            rep(i, n) {
                int jump = 1 << (j - 1);
                if (i + jump >= n) {
                    break;
                }

                table[i][j] = merge(table[i][j - 1], table[i + jump][j - 1]);
            }
        }

        bin_log[1] = 0;
        for (int i = 2; i <= n; ++i) {
            bin_log[i] = bin_log[i / 2] + 1;
        }
    }

    T query(int l, int r) {
        int len = r - l + 1;
        int k = bin_log[len];

        T val1 = table[l][k];
        T val2 = table[r - (1 << k) + 1][k];

        return merge(val1, val2);
    }
};

const int LOG = 18;
int upl[N][LOG], upr[N][LOG], upb[N][LOG];
vector<int> a;
sparse_table<int> sparse;
vector<int> pos_of_val;

void init(int n, vector<int> h) {
    a = h;
    a.pb(inf1);

    sparse = sparse_table<int>(a,n+1);

    pos_of_val = vector<int>(n+5);
    rep(i,n) pos_of_val[a[i]] = i;

    vector<int> ngel(n,n), nger(n,n);
    stack<int> st;

    rep(i,n){
        while(!st.empty() and a[i] > a[st.top()]){
            nger[st.top()] = i;
            st.pop();
        }
        st.push(i);
    }   

    while(!st.empty()) st.pop();

    rev(i,n-1,0){
        while(!st.empty() and a[i] > a[st.top()]){
            ngel[st.top()] = i;
            st.pop();
        }
        st.push(i);
    }

    while(!st.empty()) st.pop();

    rep(j,LOG){
        upl[n][j] = upr[n][j] = upb[n][j] = n;
    }

    rep(i,n){
        upl[i][0] = ngel[i];
        upr[i][0] = nger[i];
        if(a[ngel[i]] > a[nger[i]]){
            upb[i][0] = ngel[i];
        }
        else{
            upb[i][0] = nger[i];
        }
    }

    rep1(j,LOG-1){
        rep(i,n){
            upl[i][j] = upl[upl[i][j-1]][j-1];
            upr[i][j] = upr[upr[i][j-1]][j-1];
            upb[i][j] = upb[upb[i][j-1]][j-1];
        }
    }
}

int minimum_jumps(int l1, int l2, int r1, int r2) {
    if(r1 != r2){
        int ans = inf1;
        for(int r = r1; r <= r2; ++r){
            int res = minimum_jumps(l1,l2,r,r);
            if(res != -1){
                amin(ans,res);
            }
        }

        if(ans == inf1) ans = -1;
        return ans;
    }

    int l = -1, r = r1;
    if(l1 <= r and r <= l2){
        return 0;
    }

    if(l2 < r){
        // some suff is good
        int lo = l1, hi = l2;
        int suff = -1;

        while(lo <= hi){
            int mid = (lo+hi) >> 1;
            if(sparse.query(mid,r) <= a[r]){
                suff = mid;
                hi = mid-1;
            }
            else{
                lo = mid+1;
            }
        }

        if(suff == -1) return -1;

        int mx = sparse.query(suff,l2);
        l = pos_of_val[mx];
    }
    else{
        // some pref is good
        int lo = l1, hi = l2;
        int pref = -1;

        while(lo <= hi){
            int mid = (lo+hi) >> 1;
            if(sparse.query(r,mid) <= a[r]){
                pref = mid;
                lo = mid+1;
            }
            else{
                hi = mid-1;
            }
        }

        if(pref == -1) return -1;

        int mx = sparse.query(l1,pref);
        l = pos_of_val[mx];
    }

    int ans = 0;

    // big jumps until possible
    rev(j,LOG-1,0){
        int nxt = upb[l][j];
        if(a[nxt] <= a[r]){
            l = nxt;
            ans += (1 << j);
        }
    }

    // small jumps going in one direction until reach r
    rev(j,LOG-1,0){
        int nxt = upl[l][j];
        if(a[nxt] <= a[r]){
            l = nxt;
            ans += (1 << j);
        }
    }

    rev(j,LOG-1,0){
        int nxt = upr[l][j];
        if(a[nxt] <= a[r]){
            l = nxt;
            ans += (1 << j);
        }
    }
    
    return ans;

    /*

    {
        int ans = 0;
        while(l != r){
            int lx = upl[l][0], rx = upr[l][0];
            if(a[lx] > a[r]){
                l = rx;
            }
            else if(a[rx] > a[r]){
                l = lx;
            }
            else{
                if(a[lx] > a[rx]){
                    l = lx;
                }
                else{
                    l = rx;
                }
            }

            ans++;
        }

        return ans;
    }

    */
}
#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...