Submission #1119059

#TimeUsernameProblemLanguageResultExecution timeMemory
1119059Zero_OPRainforest Jumps (APIO21_jumps)C++14
100 / 100
2088 ms65552 KiB
#include <bits/stdc++.h> using namespace std; #define dbg(x) "[" #x " = " << (x) << "]" const int MAX = 2e5 + 5; const int inf = 1e9; int N, L, H[MAX], next_left[20][MAX], next_right[20][MAX], next_greater[20][MAX]; int rmq_id[20][MAX]; int combine(int i, int j){ return (H[i] < H[j] ? j : i); } void init(int _N, vector<int> _H){ N = _N; for(int i = 0; i < N; ++i) H[i] = _H[i]; stack<int> st; for(int i = 0; i < N; ++i){ while(!st.empty() && H[st.top()] <= H[i]) st.pop(); if(!st.empty()) { next_left[0][i] = st.top(); } else next_left[0][i] = -1; st.push(i); } stack<int>().swap(st); for(int i = N - 1; i >= 0; --i){ while(!st.empty() && H[st.top()] <= H[i]) st.pop(); if(!st.empty()) { next_right[0][i] = st.top(); } else next_right[0][i] = -1; st.push(i); } for(int i = 0; i < N; ++i){ if(next_left[0][i] == -1 && next_right[0][i] == -1) next_greater[0][i] = -1; else if(next_left[0][i] == -1) next_greater[0][i] = next_right[0][i]; else if(next_right[0][i] == -1) next_greater[0][i] = next_left[0][i]; else next_greater[0][i] = (H[next_left[0][i]] < H[next_right[0][i]] ? next_right[0][i] : next_left[0][i]); } for(int i = 0; i < N; ++i){ rmq_id[0][i] = i; } #define refine(a, i, j) a[i][j] = (a[i - 1][j] == -1 ? -1 : a[i - 1][a[i - 1][j]]) L = 32 - __builtin_clz(N); for(int i = 1; i < L; ++i){ for(int j = 0; j < N; ++j){ refine(next_left, i, j); refine(next_right, i, j); refine(next_greater, i, j); if(j + (1 << i) <= N){ rmq_id[i][j] = combine(rmq_id[i - 1][j], rmq_id[i - 1][j + (1 << (i - 1))]); } } } } int get_id(int l, int r){ if(l > r) return -1; int k = 31 - __builtin_clz(r - l + 1); return combine(rmq_id[k][l], rmq_id[k][r - (1 << k) + 1]); } int minimum_jumps(int a, int b, int c, int d){ int max_cd = get_id(c, d); int max_bc = get_id(b + 1, c - 1); int v_cd = H[max_cd]; int v_bc = (max_bc == -1 ? 0 : H[max_bc]); int s = b; for(int i = L - 1; i >= 0; --i){ if(next_left[i][s] != -1 && next_left[i][s] >= a && H[next_left[i][s]] < v_cd){ s = next_left[i][s]; } } int ans = 0; for(int i = L - 1; i >= 0; --i){ if(next_greater[i][s] != -1 && H[next_greater[i][s]] < v_bc){ ans += (1 << i); s = next_greater[i][s]; } } if(c <= next_right[0][s] && next_right[0][s] <= d) return ans + 1; //just only right if(next_left[0][s] != -1 && c <= next_right[0][next_left[0][s]] && next_right[0][next_left[0][s]] <= d) return ans + 2; //left first, right second for(int i = L - 1; i >= 0; --i){ //consecutive rights if(next_right[i][s] != -1 && next_right[i][s] <= d && H[next_right[i][s]] <= v_bc){ ans += (1 << i); s = next_right[i][s]; } } if(c <= next_right[0][s] && next_right[0][s] <= d) return ans + 1; return -1; } #ifdef LOCAL int main(){ ios_base::sync_with_stdio(0); cin.tie(0); freopen("task.inp", "r", stdin); freopen("task.out", "w", stdout); int N, Q; cin >> N >> Q; vector<int> H(N); for(int i = 0; i < N; ++i){ cin >> H[i]; } init(N, H); 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; } #endif //LOCAL
#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...