제출 #413757

#제출 시각아이디문제언어결과실행 시간메모리
413757flashmt밀림 점프 (APIO21_jumps)C++17
31 / 100
4073 ms41832 KiB
#include <bits/stdc++.h>
using namespace std;
const int N = 200200;
const int LG = 18;

int n, l[N], r[N], high[N][LG], low[N][LG];
vector<int> h;

void initEdges()
{
  vector<pair<int, int>> sortedH;
  for (int i = 0; i < n; i++)
    sortedH.push_back({h[i], i});
  sort(sortedH.begin(), sortedH.end(), greater<pair<int, int>>());
  set<int> s;
  s.insert(-1);
  s.insert(n);
  for (auto [_, i] : sortedH)
  {
    auto it = s.lower_bound(i);
    r[i] = *it;
    l[i] = *(--it);
    s.insert(i);
  }
}

void constructTable()
{
  for (int i = 0; i < n; i++)
  {
    low[i][0] = high[i][0] = n;
    if (l[i] < 0) low[i][0] = r[i];
    else if (r[i] == n) low[i][0] = l[i];
    else
    {
      low[i][0] = l[i];
      high[i][0] = r[i];
      if (h[r[i]] < h[l[i]])
        swap(low[i][0], high[i][0]);
    }
    for (int j = 1; j < LG; j++)
      low[i][j] = high[i][j] = n;
  }

  for (int j = 0; j + 1 < LG; j++)
    for (int i = 0; i < n; i++)
    {
      if (low[i][j] < n)
        low[i][j + 1] = low[low[i][j]][j];
      if (high[i][j] < n)
        high[i][j + 1] = high[high[i][j]][j];
    }
}

void init(int N, vector<int> H) {
  n = N;
  h = H;
  initEdges();
  constructTable();
}

int calcDist(int s, int t)
{
  int res = 0;
  for (int i = LG - 1; i >= 0; i--)
  {
    int x = high[s][i];
    if (x < n && h[x] <= h[t])
    {
      s = x;
      res |= 1 << i;
    }
  }
  for (int i = LG - 1; i >= 0; i--)
  {
    int x = low[s][i];
    if (x < n && h[x] <= h[t])
    {
      s = x;
      res += 1 << i;
    }
  }
  return s == t ? res : -1;
}

int minimum_jumps(int A, int B, int C, int D) {
  int ans = n;
  for (int i = A; i <= B; i++)
    for (int j = C; j <= D; j++)
    {
      int dist = calcDist(i, j);
      if (dist >= 0)
        ans = min(ans, dist);
    }
  return ans < n ? ans : -1;
}
#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...