제출 #951278

#제출 시각아이디문제언어결과실행 시간메모리
951278Nhoksocqt1밀림 점프 (APIO21_jumps)C++17
27 / 100
839 ms243916 KiB
#include<bits/stdc++.h> using namespace std; #define inf 0x3f3f3f3f #define sz(x) int((x).size()) #define fi first #define se second typedef long long ll; typedef pair<int, int> ii; template<class X, class Y> inline bool maximize(X &x, const Y &y) {return (x < y ? x = y, 1 : 0);} template<class X, class Y> inline bool minimize(X &x, const Y &y) {return (x > y ? x = y, 1 : 0);} mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); int Random(int l, int r) { return uniform_int_distribution<int>(l, r)(rng); } const int MAXN = 200005; struct SegNode { int cnt, id, L, R; } seg[50 * MAXN]; int P[MAXN][18], Pn[MAXN][18], Pi[MAXN][18], Pa[MAXN][18]; int h[MAXN], dist[MAXN], version[MAXN], nxt[MAXN], prv[MAXN], nTree, nArr; bool check_sub1; int update(int oldID, int l, int r, int pos, int cnt, int id) { if(l == r) { seg[++nTree] = seg[oldID]; seg[nTree].cnt += cnt; seg[nTree].id = id; return nTree; } int cur(++nTree), mid = (l + r) >> 1; seg[cur] = seg[oldID]; if(pos <= mid) { seg[cur].L = update(seg[oldID].L, l, mid, pos, cnt, id); } else { seg[cur].R = update(seg[oldID].R, mid + 1, r, pos, cnt, id); } seg[cur].cnt = seg[seg[cur].L].cnt + seg[seg[cur].R].cnt; return cur; } int walkOnSeg(int idr, int idl, int l, int r, int u, int v) { if(u <= l && r <= v) { while(l < r) { int mid = (l + r) >> 1; if(seg[seg[idr].R].cnt - seg[seg[idl].R].cnt > 0) { idr = seg[idr].R, idl = seg[idl].R; l = mid + 1; } else { idr = seg[idr].L, idl = seg[idl].L; r = mid; } } return seg[idr].id; } int res(-1), mid = (l + r) >> 1; if(mid + 1 <= v && seg[seg[idr].R].cnt - seg[seg[idl].R].cnt > 0) res = walkOnSeg(seg[idr].R, seg[idl].R, mid + 1, r, u, v); if(res < 0 && mid >= u && seg[seg[idr].L].cnt - seg[seg[idl].L].cnt > 0) res = walkOnSeg(seg[idr].L, seg[idl].L, l, mid, u, v); return res; } void init(int _N, vector<int> _H) { nArr = _N; check_sub1 = 1; for (int i = 1; i <= nArr; ++i) { h[i] = _H[i - 1]; check_sub1 &= (i == 1 || h[i] > h[i - 1]); } stack<int> st; for (int i = 1; i <= nArr; ++i) { while(sz(st) && h[st.top()] <= h[i]) st.pop(); prv[i] = (sz(st)) ? st.top() : 0; st.push(i); } while(sz(st)) st.pop(); for (int i = nArr; i > 0; --i) { while(sz(st) && h[st.top()] <= h[i]) st.pop(); nxt[i] = (sz(st)) ? st.top() : nArr + 1; st.push(i); } nTree = 0; for (int i = 1; i <= nArr; ++i) version[i] = update(version[i - 1], 1, nArr, h[i], +1, i); for (int i = 1; i <= nArr; ++i) { Pi[i][0] = Pa[i][0] = h[i]; P[i][0] = (h[nxt[i]] >= h[prv[i]]) ? nxt[i] : prv[i]; Pn[i][0] = nxt[i]; } P[0][0] = 0, P[nArr + 1][0] = Pn[nArr + 1][0] = nArr + 1; for (int j = 1; (1 << j) <= nArr; ++j) { for (int i = 0; i <= nArr + 1; ++i) { P[i][j] = P[P[i][j - 1]][j - 1]; Pn[i][j] = Pn[Pn[i][j - 1]][j - 1]; if(i + (1 << j) - 1 <= nArr) { Pi[i][j] = min(Pi[i][j - 1], Pi[i + (1 << (j - 1))][j - 1]); Pa[i][j] = max(Pa[i][j - 1], Pa[i + (1 << (j - 1))][j - 1]); } } } } int brute(int a, int b, int c, int d) { for (int i = 1; i <= nArr; ++i) dist[i] = -1; queue<int> qu; for (int i = a; i <= b; ++i) { qu.push(i); dist[i] = 0; } while(sz(qu)) { int u(qu.front()); qu.pop(); if(u >= c && u <= d) return dist[u]; if(prv[u] > 0 && dist[prv[u]] < 0) { dist[prv[u]] = dist[u] + 1; qu.push(prv[u]); } if(nxt[u] <= nArr && dist[nxt[u]] < 0) { dist[nxt[u]] = dist[u] + 1; qu.push(nxt[u]); } } return -1; } inline int getMin(int l, int r) { int log = 31 - __builtin_clz(r - l + 1); return min(Pi[l][log], Pi[r - (1 << log) + 1][log]); } inline int getMax(int l, int r) { int log = 31 - __builtin_clz(r - l + 1); return max(Pa[l][log], Pa[r - (1 << log) + 1][log]); } int sub5(int a, int c) { int res(0); for (int i = 31 - __builtin_clz(nArr); i >= 0; --i) { if(P[a][i] != 0 && P[a][i] != nArr + 1 && h[P[a][i]] < h[c]) { res += (1 << i); a = P[a][i]; } } for (int i = 31 - __builtin_clz(nArr); i >= 0; --i) { if(Pn[a][i] != nArr + 1 && h[Pn[a][i]] <= h[c]) { res += (1 << i); a = Pn[a][i]; } } assert(a == c); return res; } int magicFunc(int a, int b, int c, int d) { int maxBetween(-1), maxEnd = getMax(c, d); if(b + 1 <= c - 1) maxBetween = getMax(b + 1, c - 1); //assert(maxEnd > 1); int u = walkOnSeg(version[b], version[a - 1], 1, nArr, 1, maxEnd - 1); //assert(u != -1); int res(0); for (int i = 31 - __builtin_clz(nArr); i >= 0; --i) { if(P[u][i] != 0 && P[u][i] != nArr + 1 && h[P[u][i]] < maxBetween) { u = P[u][i]; res += (1 << i); } } if(h[u] < maxBetween) { u = nxt[u]; ++res; } if(u < c) { u = nxt[u]; ++res; } assert(c <= u && u <= d); return res; } int minimum_jumps(int a, int b, int c, int d) { ++a, ++b, ++c, ++d; if(check_sub1) return c - b; int minStart = getMin(a, b), maxBetween(-1), maxEnd = getMax(c, d); if(b + 1 <= c - 1) maxBetween = getMax(b + 1, c - 1); if(minStart >= maxEnd || maxBetween >= maxEnd) return -1; if(a == b && c == d) return sub5(a, c); return magicFunc(a, b, c, d); } #ifdef Nhoksocqt1 int main(void) { ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); #define TASK "jumps" if(fopen(TASK".inp", "r")) { freopen(TASK".inp", "r", stdin); freopen(TASK".out", "w", stdout); } vector<int> h; int n, q; cin >> n >> q; h.resize(n); for (int i = 0; i < n; ++i) cin >> h[i]; init(n, h); for (int t = 0; t < q; ++t) { int a, b, c, d; cin >> a >> b >> c >> d; cout << "ANSWER " << a << ' ' << b << ' ' << c << ' ' << d << ": " << minimum_jumps(a, b, c, d) << '\n'; } return 0; } #endif // Nhoksocqt1
#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...