Submission #975132

#TimeUsernameProblemLanguageResultExecution timeMemory
975132yoav_sRainforest Jumps (APIO21_jumps)C++17
44 / 100
4091 ms61328 KiB
#include <bits/stdc++.h> using namespace std; typedef int ll; typedef vector<ll> v; typedef vector<v> vv; typedef vector<vv> vvv; typedef pair<ll, ll> p; typedef vector<p> vp; typedef vector<vp> vvp; typedef vector<vvp> vvvp; typedef pair<ll, p> tri; typedef vector<tri> vtri; typedef vector<vtri> vvtri; typedef vector<vvtri> vvvtri; typedef vector<bool> vb; typedef vector<vb> vvb; typedef vector<vvb> vvvb; #define f first #define s second #define pb push_back #define eb emplace_back #define all(v) (v).begin(),(v).end() const ll INF = 1e18; const ll mod = 1e9 + 7; #include "jumps.h" v H; ll N; v nextHigher; v prevHigher; v st; ll n; vv largestLifting; v rightHeight; ll logN; v nextLargest; vv nextLifting; vv st2; ll query(ll l, ll r, v &st, bool maxi = true) { ll n = st.size() / 2; l += n; r += n; ll res = maxi? -INF : INF; while (l <= r) { if (l % 2 == 1) { if (maxi) res = max(res, st[l]); else res = min(res, st[l]); l++; } if (r % 2 == 0) { if (maxi) res = max(res,st[r]); else res = min(res, st[l]); r--; } l /= 2; r /= 2; } return res; } ll largestSmaller(ll l, ll r, ll threshold, vv &st) { ll n = st.size() / 2; ll tl = n, tr = threshold + n - 1; v segs, segsRev; while (tl <= tr) { if (tl % 2 == 1) { segs.pb(tl); tl++; } if (tr % 2 == 0) { segsRev.pb(tr); tr--; } tl /= 2; tr /= 2; } reverse(all(segs)); for (ll x : segs) segsRev.pb(x); for (ll x : segsRev) { auto ptr = lower_bound(all(st[x]), l); if (ptr != st[x].end() && (*ptr) <= r) { while (x < n) { auto ptr = lower_bound(all(st[2 * x + 1]), l); if (ptr != st[2 * x + 1].end() && (*ptr) <= r) x = 2 * x + 1; else x *= 2; } return st[x][0]; } } return -1; } void init(int Nn, std::vector<int> Hh) { vp hSorted; for (ll i =0 ; i < Nn; i++) hSorted.eb(Hh[i], i); sort(all(hSorted)); H = v(Nn, -1); for (ll i = 0; i < hSorted.size(); i++) H[hSorted[i].s] = i; N = Nn; nextHigher = v(N, N); stack<p> mon; for (ll i = N - 1; i >= 0; i--) { while (!mon.empty() && mon.top().s <= H[i]) mon.pop(); if (!mon.empty()) nextHigher[i] = mon.top().f; mon.emplace(i, H[i]); } mon = stack<p>(); prevHigher = v(N, -1); for (ll i = 0; i < N; i++) { while (!mon.empty() && mon.top().s <= H[i]) mon.pop(); if (!mon.empty()) prevHigher[i] = mon.top().f; mon.emplace(i, H[i]); } n = pow(2, ceil(log2(N))); st = v(2 * n, -INF); for (ll i = 0; i < N; i++) st[n + i] = H[i]; for (ll i = n - 1; i> 0; i--) st[i] = max(st[2 * i], st[2 * i + 1]); nextLargest = v(N, -1); for (ll i = 0; i < N; i++) { if (nextHigher[i] != N) nextLargest[i] = nextHigher[i]; if (prevHigher[i] != -1 && (nextLargest[i] == -1 || H[nextLargest[i]] < H[prevHigher[i]])) nextLargest[i] = prevHigher[i]; } logN = log2(N); largestLifting = vv(N, v(logN + 1, -1)); for (ll i = 0; i < N; i++) largestLifting[i][0] = nextLargest[i]; for (ll iter = 1; iter <= logN; iter++) { for (ll i= 0; i < N; i++) { if (largestLifting[i][iter - 1] == -1) largestLifting[i][iter] = -1; else largestLifting[i][iter] = largestLifting[largestLifting[i][iter - 1]][iter - 1]; } } vv nextHigherRev(N + 1); for (ll i =0; i <N; i++) nextHigherRev[nextHigher[i]].pb(i); rightHeight = v(N + 1, 0); stack<p> dfs; dfs.emplace(N, 0); while (!dfs.empty()) { auto top = dfs.top(); dfs.pop(); rightHeight[top.f] = top.s; for (ll x : nextHigherRev[top.f]) dfs.emplace(x, top.s + 1); } /* st2 = vv(2 * n, v()); for (ll i = 0; i < N; i++) st2[H[i] + n] = {i}; for (ll i = n - 1; i> 0; i--) { merge(all(st2[2 * i]), all(st2[2 * i + 1]), back_inserter(st2[i])); } */ nextLifting = vv(N + 1, v(logN + 1, N)); for (ll i = 0; i < N; i++) nextLifting[i][0] = nextHigher[i]; for (ll i = 1; i <= logN; i++) { for (ll j = 0; j < N; j++) { nextLifting[j][i] = nextLifting[nextLifting[j][i - 1]][i - 1]; } } } int minimum_jumps(int A, int B, int C, int D) { ll mini = INF; for (ll j = C; j <= D; j++) { //ll i = largestSmaller(A, B, H[j], st2); ll i = -1; for (ll k = A; k <= B; k++) { if (H[k] > H[j]) i = -1; else if (i == -1 || H[k] > H[i]) i = k; } if (i == -1 || query(i, j - 1, st, true) > H[j]) continue; ll steps = 0; ll cur = i; for (ll k = logN; k >= 0; k--) { if (largestLifting[cur][k] != -1 && H[largestLifting[cur][k]] <= H[j]) { cur = largestLifting[cur][k]; steps += (1 << k); } } steps += abs(rightHeight[cur] - rightHeight[j]); mini = min(mini, steps); } if (mini == INF) mini = -1; return mini; } /* int main() { int N, Q; assert(2 == scanf("%d %d", &N, &Q)); std::vector<int> H(N); for (int i = 0; i < N; ++i) { assert(1 == scanf("%d", &H[i])); } init(N, H); for (int i = 0; i < Q; ++i) { int A, B, C, D; assert(4 == scanf("%d %d %d %d", &A, &B, &C, &D)); printf("%d\n", minimum_jumps(A, B, C, D)); } return 0; } /**/

Compilation message (stderr)

jumps.cpp:232:1: warning: "/*" within comment [-Wcomment]
  232 | /**/
      |  
jumps.cpp:27:16: warning: overflow in conversion from 'double' to 'll' {aka 'int'} changes value from '1.0e+18' to '2147483647' [-Woverflow]
   27 | const ll INF = 1e18;
      |                ^~~~
jumps.cpp: In function 'void init(int, std::vector<int>)':
jumps.cpp:117:22: warning: comparison of integer expressions of different signedness: 'll' {aka 'int'} and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  117 |     for (ll i = 0; i < hSorted.size(); i++) H[hSorted[i].s] = 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...