제출 #968785

#제출 시각아이디문제언어결과실행 시간메모리
968785steveonalex밀림 점프 (APIO21_jumps)C++17
100 / 100
1228 ms44396 KiB
#include <bits/stdc++.h> #include "jumps.h" using namespace std; typedef long long ll; typedef unsigned long long ull; #define ALL(v) (v).begin(), (v).end() #define MASK(i) (1LL << (i)) #define GETBIT(mask, i) (((mask) >> (i)) & 1) // mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); mt19937_64 rng(1); ll rngesus(ll l, ll r){return ((ull) rng()) % (r - l + 1) + l;} ll max(ll a, ll b){return (a > b) ? a : b;} ll min(ll a, ll b){return (a < b) ? a : b;} ll LASTBIT(ll mask){return mask & (-mask);} ll pop_cnt(ll mask){return __builtin_popcountll(mask);} ll ctz(ll mask){return __builtin_ctzll(mask);} ll clz(ll mask){return __builtin_clzll(mask);} ll logOf(ll mask){return 63 - clz(mask);} template <class T1, class T2> bool minimize(T1 &a, T2 b){ if (a > b){a = b; return true;} return false; } template <class T1, class T2> bool maximize(T1 &a, T2 b){ if (a < b){a = b; return true;} return false; } template <class T> void printArr(T& a, string separator = " ", string finish = "\n", ostream& out = cout){ for(auto i: a) out << i << separator; out << finish; } template <class T> void remove_dup(vector<T> &a){ sort(ALL(a)); a.resize(unique(ALL(a)) - a.begin()); } const int INF = 1e9; struct SegmentTree{ int n; vector<int> a; SegmentTree(){} SegmentTree(int _n){ n = _n; a.resize(n * 2 + 2, -INF); } void update(int i, int v){ i += n; if (!maximize(a[i], v)) return; while(i > 1){ i >>= 1; a[i] = max(a[i * 2], a[i * 2 + 1]); } } int get(int l, int r){ l += n; r += n + 1; int ans = -INF; while(l < r){ if (l & 1) maximize(ans, a[l++]); if (r & 1) maximize(ans, a[--r]); l >>= 1; r >>= 1; } return ans; } }; const int LOG_N = 18; const int N = 2e5 + 69; int n; int h[N], pref[N], suff[N]; int nxt[LOG_N][N], lnxt[LOG_N][N]; SegmentTree segg; map<int, int> pos; void init(int _n, vector<int> H) { n = _n; for(int i = 0; i<n; ++i) h[i] = H[i]; vector<int> st; for(int i = 0; i<n; ++i){ while(st.size() && h[st.back()] <= h[i]) st.pop_back(); if (st.empty()) pref[i] = n; else pref[i] = st.back(); st.push_back(i); } st.clear(); for(int i = n-1; i>=0; --i){ while(st.size() && h[st.back()] <= h[i]) st.pop_back(); if (st.empty()) suff[i] = n; else suff[i] = st.back(); st.push_back(i); } for(int i = 0; i<n; ++i){ if (h[pref[i]] > h[suff[i]]) nxt[0][i] = pref[i]; else nxt[0][i] = suff[i]; lnxt[0][i] = suff[i]; } lnxt[0][n] = nxt[0][n] = n; for(int j = 1; j < LOG_N; ++j){ for(int i = 0; i<=n; ++i){ nxt[j][i] = nxt[j-1][nxt[j-1][i]]; lnxt[j][i]= lnxt[j-1][lnxt[j-1][i]]; } } segg = SegmentTree(n); for(int i = 0;i<n; ++i) segg.update(i + 1, H[i]); for(int i = 0; i<n; ++i) pos[H[i]] = i; } int walk_to_success(int x, int C, int D){ int ans = 0; for(int j = LOG_N - 1; j>=0; --j){ if (lnxt[j][x] < C) { x = lnxt[j][x]; ans += MASK(j); } } x = lnxt[0][x]; ans++; if (x > D) return INF; return ans; } int minimum_jumps(int A, int B, int C, int D) { int val_between = segg.get(B + 2, C); int val_r = segg.get(C + 1, D + 1); if (val_r < val_between) return -1; int R = val_r; int l = C, r = D; while(l < r){ int mid = (l + r) >> 1; if (segg.get(C + 1, mid + 1) > val_between) r = mid; else l = mid + 1; } int L = h[l]; l = A, r = B + 1; while(l < r){ int mid = (l + r) >> 1; if (segg.get(mid+1, B+1) > R) l = mid + 1; else r = mid; } if (l > B) return -1; int val = segg.get(l + 1, B + 1); int x = pos[val]; int ans = 0; for(int j = LOG_N - 1; j>=0; --j){ if (nxt[j][x] == n) continue; if (h[nxt[j][x]] < L) { x = nxt[j][x]; ans += MASK(j); } } ans += min(walk_to_success(x, C, D), walk_to_success(nxt[0][x], C, D) + 1); if (ans > INF) return -1; return ans; } // int main(void){ // ios::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; // for(int i = 0; i<q; ++i){ // int a, b, c, d; cin >> a >> b >> c >> d; // cout << minimum_jumps(a, b, c, d) << "\n"; // } // return 0; // }
#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...