Submission #986516

#TimeUsernameProblemLanguageResultExecution timeMemory
986516ZHIRDILBILDIZRainforest Jumps (APIO21_jumps)C++14
Compilation error
0 ms0 KiB
#include <bits/stdc++.h> // #include "jumps.h" #define fi first #define se second #define ll long long #define pll pair<ll, ll> #define pii pair<int, int> using namespace std; const int N = 2e5 + 10; int pw[N]; int rg[N][20]; int lf[N][20]; int mx[N][20]; void build_sparce (vector<int> h) { for (int i = 2; i <= h.size(); ++i) pw[i] = pw[i >> 1] + 1; for (int i = 0; i < h.size(); ++i) mx[i][0] = h[i]; for (int i = 1; i < 20; ++i) for (int j = 0; j <= (int)h.size() - (1 << i); ++j) mx[j][i] = max(mx[j][i - 1], mx[j + (1 << (i - 1))][i - 1]); } int get_max (int l, int r) { if (l > r) swap(l, r); int x = pw[r - l + 1]; return max(mx[l][x], mx[r - (1 << x) + 1][x]); } void init (int n, vector<int> h) { build_sparce(h); for (int i = 0; i < n; ++i) { int l = i, r = n; lf[i][0] = rg[i][0] = i; while (l + 1 < r) { int mid = (l + r) / 2; if (get_max(i, mid) == h[i]) l = mid; else r = mid; } if (r != n) rg[i][0] = r; l = -1, r = i; while (l + 1 < r) { int mid = (l + r) / 2; if (get_max(mid, i) == h[i]) r = mid; else l = mid; } if (l != -1) lf[i][0] = l; // cout << lf[i][0] << ' ' << rg[i][0] << '\n'; } for (int i = 1; i < 20; ++i) for (int j = 0; j < n; ++j) { lf[j][i] = lf[lf[j][i - 1]][i - 1]; rg[j][i] = rg[rg[j][i - 1]][i - 1]; } } int minimum_jumps(int a, int b, int c, int d) { for (int i = 19; i >= 0; --i) if (a <= lf[b][i] && rg[lf[b][i]][0] <= d) b = lf[b][i]; int kol = 0; for (int j = 0; j < 5; ++j) for (int i = 19; i >= 0; --i) if (rg[b][i] < c) { b = rg[b][i]; kol += (1 << i); } if (c <= rg[b][0] && rg[b][0] <= d) return kol + 1; return -1; } signed main () { // ios_base::sync_with_stdio(0); // cin.tie(0), cout.tie(0); int n; cin >> n; vector<int> h(n); for (int i = 0; i < n; ++i) cin >> h[i]; init(n, h); int q; cin >> q; while (q--) { int a, b, c, d; cin >> a >> b >> c >> d; cout << minimum_jumps(a, b, c, d) << '\n'; } return 0; }

Compilation message (stderr)

jumps.cpp: In function 'void build_sparce(std::vector<int>)':
jumps.cpp:18:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   18 |     for (int i = 2; i <= h.size(); ++i) pw[i] = pw[i >> 1] + 1;
      |                     ~~^~~~~~~~~~~
jumps.cpp:20:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   20 |     for (int i = 0; i < h.size(); ++i) mx[i][0] = h[i];
      |                     ~~^~~~~~~~~~
/usr/bin/ld: /tmp/ccVRu6jE.o: in function `main':
stub.cpp:(.text.startup+0x0): multiple definition of `main'; /tmp/ccSBPd4D.o:jumps.cpp:(.text.startup+0x0): first defined here
collect2: error: ld returned 1 exit status