Submission #935511

#TimeUsernameProblemLanguageResultExecution timeMemory
935511qwe1rt1yuiop1Cat Exercise (JOI23_ho_t4)C++14
21 / 100
99 ms18248 KiB
#include <bits/stdc++.h> using namespace std; using pii = pair<int, int>; int n; vector<int> seg, v, pos; void build(int x, int l, int r) { if (l + 1 == r) { seg[x] = v[l]; return; } int mid = (l + r) >> 1; build(x << 1, l, mid), build(x << 1 | 1, mid, r); seg[x] = max(seg[x << 1], seg[x << 1 | 1]); } int qry(int x, int l, int r, int ql, int qr) { if (ql <= l && r <= qr) return seg[x]; int mid = (l + r) >> 1; if (qr <= mid) return qry(x << 1, l, mid, ql, qr); if (ql >= mid) return qry(x << 1 | 1, mid, r, ql, qr); int retl = qry(x << 1, l, mid, ql, qr), retr = qry(x << 1 | 1, mid, r, ql, qr); return max(retl, retr); } int ans(int x, int l, int r) { assert(qry(1, 0, n, l, r) == x); if (x == 0 || l + 1 == r) return 0; if (pos[x] == l) { int rx = qry(1, 0, n, pos[x] + 1, r); return ans(rx, pos[x] + 1, r) + pos[rx] - pos[x]; } if (pos[x] + 1 == r) { int lx = qry(1, 0, n, l, pos[x]); return ans(lx, l, pos[x]) + pos[x] - pos[lx]; } int lx = qry(1, 0, n, l, pos[x]), rx = qry(1, 0, n, pos[x] + 1, r); return max(ans(lx, l, pos[x]) + pos[x] - pos[lx], ans(rx, pos[x] + 1, r) + pos[rx] - pos[x]); } /* 4 3 4 1 2 1 2 2 3 3 4 */ void solve() { cin >> n; v.resize(n); for (int &i : v) cin >> i, --i; for (int i = 1, a, b; i < n; ++i) cin >> a >> b; pos.resize(n); for (int i = 0; i < n; ++i) pos[v[i]] = i; seg.assign(n * 4 + 10, 0); build(1, 0, n); cout << ans(n - 1, 0, n) << '\n'; } /* */ signed main() { ios::sync_with_stdio(0); cin.tie(0); solve(); 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...