Submission #416420

#TimeUsernameProblemLanguageResultExecution timeMemory
416420valerikkRainforest Jumps (APIO21_jumps)C++17
100 / 100
1575 ms41364 KiB
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; const int N = 2e5 + 7; const int L = 19; const int INF = 1e9; int n; int h[N]; pair<int, int> t[4 * N]; void tree_build(int v, int l, int r) { if (r - l == 1) { t[v] = {h[l], l}; } else { int mid = (l + r) / 2; tree_build(2 * v, l, mid); tree_build(2 * v + 1, mid, r); t[v] = max(t[2 * v], t[2 * v + 1]); } } pair<int, int> get_max(int v, int l, int r, int a, int b) { if (l >= b || r <= a) return {-INF, -INF}; if (l >= a && r <= b) return t[v]; int mid = (l + r) / 2; return max(get_max(2 * v, l, mid, a, b), get_max(2 * v + 1, mid, r, a, b)); } int find_first(int v, int l, int r, int a, int b, int val) { if (l >= b || r <= a || t[v].first <= val) return -1; if (r - l == 1) return l; int mid = (l + r) / 2; int res = find_first(2 * v + 1, mid, r, a, b, val); if (res == -1) res = find_first(2 * v, l, mid, a, b, val); return res; } vector<vector<int>> build(vector<int> arr) { vector<vector<int>> go(L, vector<int>(n)); for (int i = 0; i < n; ++i) go[0][i] = arr[i]; for (int i = 1; i < L; ++i) { for (int j = 0; j < n; ++j) go[i][j] = go[i - 1][go[i - 1][j]]; } return go; } vector<int> l, r, best; vector<vector<int>> r2, best2; void init(int N, vector<int> H) { n = N + 2; h[0] = n - 2; h[n - 1] = n - 1; for (int i = 0; i < N; ++i) h[i + 1] = H[i] - 1; l.resize(n); r.resize(n); best.resize(n); vector<int> st; for (int i = 0; i < n; ++i) { while (!st.empty() && h[st.back()] < h[i]) { r[st.back()] = i; st.pop_back(); } st.push_back(i); } for (auto i : st) r[i] = n - 1; st.clear(); for (int i = n - 1; i >= 0; --i) { while (!st.empty() && h[st.back()] < h[i]) { l[st.back()] = i; st.pop_back(); } st.push_back(i); } for (auto i : st) l[i] = 0; st.clear(); for (int i = 0; i < n; ++i) best[i] = h[l[i]] > h[r[i]] ? l[i] : r[i]; r2 = build(r); best2 = build(best); tree_build(1, 0, n); } int minimum_jumps(int A, int B, int C, int D) { ++A, ++B, ++C, ++D; int mx = get_max(1, 0, n, C, D + 1).first; int mx2 = get_max(1, 0, n, B + 1, C).first; //cout << A << " " << B << " " << C << " " << D << " " << mx << " " << mx2 << " " << h[B] << endl; if (h[B] > mx || mx2 > mx) return -1; int min_pos = find_first(1, 0, n, 0, B + 1, mx); A = max(A, min_pos + 1); int st = get_max(1, 0, n, A, B + 1).second; int id = st, res = 0; for (int i = L - 1; i >= 0; --i) { if (h[best2[i][id]] < mx2) { id = best2[i][id]; res += (1 << i); } } if (h[id] < mx2 && h[best[id]] < mx) { id = best[id]; ++res; } for (int i = L - 1; i >= 0; --i) { if (h[r2[i][id]] < mx2) { id = r2[i][id]; res += (1 << i); } } if (h[id] < mx2) { id = r[id]; ++res; } /*while (h[id] < mx2) { ++res; if (h[best[id]] < mx) id = best[id]; else id = r[id]; }*/ id = r[id]; ++res; //assert(id >= C && id <= D); return res; } int correct_minimum_jumps(int A, int B, int C, int D) { ++A, ++B, ++C, ++D; vector<int> dist(n, INF); vector<int> q; for (int i = A; i <= B; ++i) { q.push_back(i); dist[i] = 0; } for (int i = 0; i < q.size(); ++i) { int v = q[i]; if (dist[v] + 1 < dist[l[v]]) { dist[l[v]] = dist[v] + 1; q.push_back(l[v]); } if (dist[v] + 1 < dist[r[v]]) { dist[r[v]] = dist[v] + 1; q.push_back(r[v]); } } int mn = INF; for (int i = C; i <= D; ++i) mn = min(mn, dist[i]); return mn == INF ? -1 : mn; } #ifdef LOCAL int main() { freopen("input.txt", "r", stdin); ios::sync_with_stdio(false); cin.tie(0); if (0) { int N, Q; cin >> N >> Q; vector<int> H(N); for (int i = 0; i < N; ++i) cin >> H[i]; init(N, H); while (Q--) { int A, B, C, D; cin >> A >> B >> C >> D; cout << minimum_jumps(A, B, C, D) << endl; } } else { srand(0); int N = 12, Q = 100000; vector<int> H(N); iota(H.begin(), H.end(), 1); random_shuffle(H.begin(), H.end()); init(N, H); while (Q--) { vector<int> v; for (int i = 0; i < 4; ++i) v.push_back(rand() % N); sort(v.begin(), v.end()); if (v[1] == v[2]) continue; int A = v[0], B = v[1], C = v[2], D = v[3]; int ans = minimum_jumps(A, B, C, D); int correct_ans = correct_minimum_jumps(A, B, C, D); if (ans != correct_ans) { cout << N << " 1" << endl; for (int i = 0; i < N; ++i) cout << H[i] << " "; cout << endl; cout << A << " " << B << " " << C << " " << D << endl; cout << ans << " " << correct_ans << endl; return 0; } } } return 0; } #endif

Compilation message (stderr)

jumps.cpp: In function 'int correct_minimum_jumps(int, int, int, int)':
jumps.cpp:135:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  135 |     for (int i = 0; i < q.size(); ++i) {
      |                     ~~^~~~~~~~~~
#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...