Submission #1196616

#TimeUsernameProblemLanguageResultExecution timeMemory
1196616Mher777Rainforest Jumps (APIO21_jumps)C++20
0 / 100
4049 ms81808 KiB
#include "jumps.h" #define _CRT_SECURE_NO_WARNINGS #include <iostream> #include <iomanip> #include <array> #include <string> #include <algorithm> #include <cmath> #include <set> #include <map> #include <unordered_set> #include <unordered_map> #include <vector> #include <stack> #include <queue> #include <deque> #include <bitset> #include <list> #include <iterator> #include <numeric> #include <complex> #include <utility> #include <random> #include <cassert> #include <fstream> using namespace std; mt19937 rnd(7069); /* -------------------- Typedefs -------------------- */ typedef int itn; typedef long long ll; typedef unsigned long long ull; typedef double db; typedef float fl; typedef long double ld; /* -------------------- Usings -------------------- */ using vi = vector<int>; using vll = vector<ll>; using mii = map<int, int>; using mll = map<ll, ll>; using pii = pair<int, int>; using pll = pair<ll, ll>; /* -------------------- Defines -------------------- */ #define ff first #define ss second #define pub push_back #define pob pop_back #define puf push_front #define pof pop_front #define mpr make_pair #define yes cout<<"Yes\n" #define no cout<<"No\n" #define all(x) (x).begin(), (x).end() #define USACO freopen("feast.in", "r", stdin); freopen("feast.out", "w", stdout); /* -------------------- Constants -------------------- */ const int dx[8] = { -1, 0, 1, 0, -1, -1, 1, 1 }; const int dy[8] = { 0, -1, 0, 1, -1, 1, -1, 1 }; const int MAX = int(2e9 + 5); const ll MAXL = ll(1e18) + 5ll; const ll MOD = ll(1000000007); const ll MOD2 = ll(998244353); const int max_N = 200005, M = 30; int a[max_N], up_right[max_N][M], dp_left[max_N], dp_right[max_N], st[max_N][M]; pii up[max_N][M]; int n, lg; int qry(int l, int r) { if (l > r) return 0; int lgx = log2(r - l + 1); return max(st[l][lgx], st[r - (1 << lgx) + 1][lgx]); } void init(int N, std::vector<int> H) { n = N; lg = log2(n); for (int lgx = 0; lgx <= lg; ++lgx) { up[0][lgx].ff = up[n + 1][lgx].ff = MAX; } vector<pii> vec; for (int i = 1; i <= n; ++i) { a[i] = H[i - 1]; st[i][0] = a[i]; vec.pub({ -a[i],i }); int ind = i - 1; while (ind) { if (a[ind] > a[i]) break; ind = dp_left[ind]; } dp_left[i] = ind; } for (int lgx = 1; lgx <= lg; ++lgx) { for (int i = 1; i + (1 << lgx) - 1 <= n; ++i) { st[i][lgx] = max(st[i][lgx - 1], st[i + (1 << (lgx - 1))][lgx - 1]); } } for (int i = n; i >= 1; --i) { int ind = i + 1; while (ind <= n) { if (a[ind] > a[i]) break; ind = dp_right[ind]; } dp_right[i] = ind; up_right[i][0] = ind; for (int lgx = 1; lgx <= lg; ++lgx) { up_right[i][lgx] = up_right[up_right[i][lgx - 1]][lgx - 1]; } } sort(all(vec)); for (auto opt : vec) { int ind = opt.ss; int val = a[dp_right[ind]], nxt = dp_right[ind]; if (a[dp_left[ind]] > a[dp_right[ind]]) { val = a[dp_left[ind]]; nxt = dp_left[ind]; } if (nxt == 0 || nxt == n + 1) val = MAXL; up[ind][0] = { val,nxt }; for (int lgx = 1; lgx <= n; ++lgx) { up[ind][lgx] = up[up[ind][lgx - 1].ss][lgx - 1]; } } } int minimum_jumps(int A, int B, int C, int D) { ++A, ++B, ++C, ++D; int max_right = qry(C, D); int max_mid = qry(B + 1, C - 1); if (max_mid >= max_right || a[B] >= max_right) return -1; int node = B; while (dp_left[node] >= A && a[dp_left[node]] < max_right) { node = dp_left[node]; } //suppose we go right many timees so fix the point until val <= max_mid int u = node; int ans1 = 0; for (int lgx = lg; lgx >= 0; --lgx) { pii opt = up[u][lgx]; if (opt.ff > max_mid) continue; u = opt.ss; ans1 += (1 << lgx); } for (int lgx = lg; lgx >= 0; --lgx) { if (up_right[u][lgx] < C && up_right[u][lgx] != 0) { u = up_right[u][lgx]; ans1 += (1 << lgx); } } if (up_right[u][0] >= C && up_right[u][0] <= D) { ++ans1; } else { ans1 = MAX; } //suppose we go to desired sequence with one move after going to most optimal nodes u = node; int ans2 = MAX, cur = 0; if (up_right[u][0] >= C && up_right[u][0] <= D) { ans2 = 1; } for (int lgx = lg; lgx >= 0; --lgx) { pii opt = up[u][lgx]; if (opt.ff >= max_right) continue; if (up_right[opt.ss][0] >= C && up_right[opt.ss][0] <= D) { ans2 = min(ans2, cur + (1 << lgx) + 1); } else { cur += (1 << lgx); u = opt.ss; } } int ans = min(ans1, ans2); return (ans == MAX ? -1 : ans); } /* 7 100 3 2 1 6 4 5 7 0 1 2 2 1 3 5 6 4 4 6 6 */

Compilation message (stderr)

jumps.cpp: In function 'void init(int, std::vector<int>)':
jumps.cpp:124:53: warning: overflow in conversion from 'll' {aka 'long long int'} to 'int' changes value from '1000000000000000005' to '-1486618619' [-Woverflow]
  124 |                 if (nxt == 0 || nxt == n + 1) val = MAXL;
      |                                                     ^~~~
#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...