Submission #980618

#TimeUsernameProblemLanguageResultExecution timeMemory
980618vjudge1Rainforest Jumps (APIO21_jumps)C++17
44 / 100
4043 ms57880 KiB
#include "jumps.h"
#include<bits/stdc++.h>

using namespace std;
#define ll long long
#define ff first
#define ss second
const int mod = 1e9 + 7;
const int inf = 1e9;
const int logn = 20;

template<typename T>
int len(T &a){return a.size();}


vector<vector<int>> up, low;

int n;
bool sub1 = 1;



vector<int> h; vector<int> pos;

struct SparseT{
  vector<vector<int>> t;
  int n, logn;
  int join(int a, int b){
    return max(a,b);
  }

  int neutral(){
    return 0;
  }

  SparseT (){}

  SparseT (int _n){
    init(_n);
  }

  SparseT (vector<int> &a){
    build(a);
  }

  void init(int _n){
    n = _n;
    logn = 32 - __builtin_clz(n);
    t.assign(_n, vector<int>(logn, neutral()));
  }

  void build(vector<int> &a){
    init((int)(a.size()));
    for(int i = 0; i < n; i ++){
      t[i][0] = a[i];
    }

    for(int i = 1; i < logn; i ++){
      for(int j = 0; j + (1 << i) <= n; j ++){
        t[j][i] = join(t[j][i - 1], t[j + (1 << (i - 1))][i - 1]);
      }
    }
  }

  int get(int l, int r){
    if(l > r) return neutral();
    int k = 31 - __builtin_clz(r - l + 1);
    return join(t[l][k], t[r - (1 << k) + 1][k]); 
  }
} t;


void init(int N, vector<int> H) {
    h = H;
    n = N;
    pos.resize(n);
    for(int i = 0; i < n; i ++) h[i] --;
    for(int i = 0; i < n; i ++) pos[h[i]] = i;
    vector<int> st;
    vector<int> lef(n), rig(n);
    for(int i = 0; i < n; i ++){
      if(h[i] != i + 1) sub1 = 0;
      while(!st.empty() && st.back() <= h[i]) st.pop_back();
      if(st.empty()) lef[i] = -1;
      else lef[i] = st.back();
      st.push_back(h[i]);
    }
    if(sub1) return;
    st.clear();
    for(int i = n - 1; i >= 0; i --){
      while(!st.empty() && st.back() <= h[i]) st.pop_back();
      if(st.empty()) rig[i] = -1;
      else rig[i] = st.back();
      st.push_back(h[i]);
    }

    up.assign(logn, vector<int> (n, -1));
    low.assign(logn, vector<int> (n, -1));

    for(int i = 0; i < n; i ++){
      up[0][h[i]] = max(lef[i], rig[i]);
    }
    t.build(h);
    // first max, then min
    for(int j = 1; j < logn; j ++){
      for(int i = 0; i < n; i ++){
        if(up[j - 1][i] == -1) continue;
        //cout << (up[j][i]) << endl;
        up[j][i] = up[j - 1][up[j - 1][i]];
      }
    }

    for(int i = 0; i < n; i ++){
      if(lef[i] == -1 && rig[i] == -1) continue;
      if(lef[i] == -1) low[0][h[i]] = rig[i];
      else if(rig[i] == -1) low[0][h[i]] = rig[i];
      else low[0][h[i]] = min(rig[i], lef[i]);
    }

    for(int j = 1; j < logn; j ++){
      for(int i = 0; i < n; i ++){
        if(low[j - 1][i] == -1) continue;
        low[j][i] = low[j - 1][low[j - 1][i]];
      }
    }
}

int minimum_jumps(int A, int B, int L, int R) {
    if(sub1) return L - B;
    int obst = t.get(B, L - 1);
    int mn = inf;

    for(int C = L; C <= R; C ++){
      if(h[C] <= obst) continue;
      int l = A - 1, r = B;
      while(r - l > 1){
        int m = (l + r) / 2;
        if(t.get(m, C - 1) < h[C]){
          r = m;
        }else l = m;
      }
      int st = t.get(r, B);
      int ans = 0;
      for(int j = logn - 1; j >= 0; j --){
        if(up[j][st] != -1 && up[j][st] <= h[C]){
          st = up[j][st];
          ans += 1 << j;
        }
      }

      for(int j = logn - 1; j >= 0; j --){
        if(low[j][st] != -1 && low[j][st] <= h[C]){
          ans += 1 << j;
          st = low[j][st];
        }
      }
      mn = min(mn, ans);
    }

    return (mn == inf ? -1 : mn);
}
#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...