Submission #561337

#TimeUsernameProblemLanguageResultExecution timeMemory
561337ian2024Rainforest Jumps (APIO21_jumps)C++14
0 / 100
20 ms28444 KiB
#include <bits/stdc++.h> using namespace std; struct C_HASH { static uint64_t splitmix64(uint64_t x) { // http://xorshift.di.unimi.it/splitmix64.c x += 0x9e3779b97f4a7c15; x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9; x = (x ^ (x >> 27)) * 0x94d049bb133111eb; return x ^ (x >> 31); } size_t operator()(uint64_t x) const { static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count(); return splitmix64(x + FIXED_RANDOM); } }; mt19937 rng(chrono::system_clock::now().time_since_epoch().count()); typedef pair<int, int> ii; typedef tuple<int, int, int> iii; typedef long long ll; const int INF = 1e9; const ll INFL = 1e18; const int MOD = 1000000007; #define SIZE(a) (int)(a).size() #define endl '\n' #define all(x) x.begin(), x.end() #define ALL(x) begin(x), end(x) int pos[200005]; int lo[200005][18]; int hi[200005][18]; void init(int N, std::vector<int> H) { for (int i = 0; i < 200005; ++i) { for (int j = 0; j < 18; ++j) { lo[i][j] = INF; hi[i][j] = INF; } } for (int i = 0; i < N; ++i) { pos[H[i]] = i; } set<int> s; for (int i = N; i >= 1; --i) { s.insert(pos[i]); auto l = s.lower_bound(pos[i]); if (l != s.begin()) { l--; lo[i][0] = H[*l]; } auto r = s.upper_bound(pos[i]); if (r != s.end()) hi[i][0] = H[*r]; if (hi[i][0] < lo[i][0]) { swap(hi[i][0], lo[i][0]); } } for (int j = 1; j < 18; ++j) { for (int i = 1; i <= N; ++i) { if (lo[i][j - 1] != INF) lo[i][j] = lo[lo[i][j - 1]][j - 1]; if (hi[i][j - 1] != INF) hi[i][j] = hi[hi[i][j - 1]][j - 1]; } } } int minimum_jumps(int A, int B, int C, int D) { int jumps = 0; int curr = A; for (int i = 17; i >= 0; --i) { if (hi[curr][i] <= C) { curr = hi[curr][i]; jumps += (1 << i); } } for (int i = 17; i >= 0; --i) { if (lo[curr][i] <= C) { curr = lo[curr][i]; jumps += (1 << i); } } if (curr != C) return -1; return jumps; } #ifdef LOCAL_PROJECT 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; } #else #include "jumps.h" #define cerr \ if (false) cerr #endif
#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...