Submission #731074

#TimeUsernameProblemLanguageResultExecution timeMemory
731074Magikarp4000Rainforest Jumps (APIO21_jumps)C++17
0 / 100
1 ms336 KiB
#include "jumps.h"
#include <bits/stdc++.h>
using namespace std;
#define OPTM ios_base::sync_with_stdio(0); cin.tie(0);
#define INF int(1e9+7)
#define ln '\n' 
#define ll long long
#define ull unsigned long long
#define ui unsigned int
#define us unsigned short
#define FOR(i,s,n) for (int i = s; i < n; i++)
#define FORR(i,n,s) for (int i = n; i > s; i--)
#define FORX(u, arr) for (auto u : arr)
#define PB push_back
#define in(v,x) (v.find(x) != v.end())
#define F first
#define S second
#define PII pair<int, int>
#define PLL pair<ll, ll>
#define UM unordered_map
#define US unordered_set
#define PQ priority_queue
#define ALL(v) v.begin(), v.end()
const ll LLINF = 1e18+1;

const int MAXN = 2e5+1, LOG = 19;
int n;
int v[MAXN][2];
int l[MAXN], r[MAXN];
int jump[MAXN][LOG], jump1[MAXN][LOG];
int h[MAXN];

void init(int N, std::vector<int> H) {
  n = N;
  FOR(i,1,n+1) h[i] = H[i-1];
  stack<PII> st;
  FOR(i,1,n+1) {
    while (!st.empty() && st.top().F <= h[i]) st.pop();
    if (st.empty()) l[i] = 0;
    else l[i] = st.top().S;
    st.push({h[i],i});
  }
  while (!st.empty()) st.pop();
  FORR(i,n,0) {
    while (!st.empty() && st.top().F <= h[i]) st.pop();
    if (st.empty()) r[i] = 0;
    else r[i] = st.top().S;
    st.push({h[i],i});
  }
  
  FOR(i,1,n+1) {
    int c = l[i] == 0 ? 0: h[l[i]];
    int d = r[i] == 0 ? 0: h[r[i]];
    v[i][0] = l[i];
    v[i][1] = r[i];
    if (c < d) swap(v[i][0], v[i][1]);
  }
  // FOR(i,1,n+1) cout << v[i][0] << ' ';
  // cout << ln;
  // FOR(i,1,n+1) cout << v[i][1] << ' ';
  // cout << ln;
  FOR(i,1,n+1) {
    jump[i][0] = v[i][0];
    jump1[i][0] = v[i][1];
  }
  FOR(j,1,LOG) {
    FOR(i,1,n+1) {
      jump[i][j] = jump[jump[i][j-1]][j-1];
      jump1[i][j] = jump1[jump1[i][j-1]][j-1];
    }
  }
}

int minimum_jumps(int a, int b, int c, int d) {
  //a--; b--; c--; d--;
  //cout << a << ' ' << c << ln;
  int res = 0;
  FORR(j,LOG-1,-1) {
    if (jump[a][j] == 0) continue;
    if (h[jump[a][j]] <= h[c]) {
      res += (1<<j);
      a = jump[a][j];
    }
  }
  FORR(j,LOG-1,-1) {
    if (jump1[a][j] == 0) continue;
    if (h[jump1[a][j]] <= h[c]) {
      res += (1<<j);
      a = jump1[a][j];
    }
  }
  if (a != c) return -1;
  return res;
}
#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...