제출 #894261

#제출 시각아이디문제언어결과실행 시간메모리
894261box서열 (APIO23_sequence)C++17
100 / 100
1912 ms76848 KiB
#pragma GCC optimize("Ofast") #include <algorithm> #include <array> #include <cassert> #include <chrono> #include <iostream> #include <random> #include <vector> using namespace std; typedef vector<int> vi; const int N = 5e5, N_ = 1 << 19; namespace D1 { struct node { int minL, minR, maxL, maxR, sum; }; node operator+(const node &u, const node &v) { return {min(u.minL, u.sum + v.minL), min(v.minR, v.sum + u.minR), max(u.maxL, u.sum + v.maxL), max(v.maxR, v.sum + u.maxR), u.sum + v.sum}; } const node id = {0, 0, 0, 0, 0}; node st[N_ * 2]; int n_; void activate(int i) { st[i += n_] = {-1, -1, 0, 0, -1}; while (i /= 2) st[i] = st[i * 2] + st[i * 2 + 1]; } node qry(int l, int r) { node u = id, v = id; for (l += n_, r += n_; l <= r; l /= 2, r /= 2) { if (l & 1) u = u + st[l++]; if (~r & 1) v = st[r--] + v; } return u + v; } } // namespace D1 const int M_ = 1 << 20, offset = N_, INF = 1e8; int mx[M_ * 2]; vi reset; void upd(int i, int x) { reset.push_back(i); i += offset; i += M_; while (i) mx[i] = max(mx[i], x), i /= 2; } int qry(int l, int r) { l += offset; r += offset; int x = -INF; for (l += M_, r += M_; l <= r; l /= 2, r /= 2) { if (l & 1) x = max(x, mx[l++]); if (~r & 1) x = max(x, mx[r--]); } return x; } void clr() { for (int i : reset) { i += offset; i += M_; while (i) mx[i] = -INF, i /= 2; } vi().swap(reset); } int minL[N], maxL[N], minR[N], maxR[N], sumL[N], sumR[N], ans; void dnc(int l, int r) { if (l < r) { int m = (l + r) / 2; dnc(l, m); dnc(m + 1, r); int sum = 0; vector<array<int, 3>> left, right; for (int i = m; i >= l; i--) { if (i < m) sum += sumR[i]; int v = maxL[i] + sum + (m - i + 1); int u = minL[i] + sum - (m - i + 1) + 1; v *= -1; left.push_back({v, u, (m - i + 1)}); } sum = 0; for (int i = m + 1; i <= r; i++) { sum += sumL[i]; int v = maxR[i] + sum + (i - m); int u = minR[i] + sum - (i - m); u *= -1; right.push_back({v, u, i - m}); } sort(left.begin(), left.end()); sort(right.begin(), right.end()); int p = 0; for (auto [u, v, k] : right) { while (p < int(left.size()) && left[p][0] <= u) { upd(left[p][1], left[p][2]); p++; } ans = max(ans, k + qry(-offset, v)); } clr(); } } void solve(int n, vi a) { vector<vi> ii(n); for (int i = 0; i < n; i++) ii[a[i]].push_back(i); D1::n_ = 1; while (D1::n_ < n) D1::n_ *= 2; for (int i = 0; i < n; i++) D1::st[D1::n_ + i] = {0, 0, 1, 1, 1}; for (int i = D1::n_ - 1; i > 0; i--) D1::st[i] = D1::st[i * 2] + D1::st[i * 2 + 1]; for (int x = 0; x < n; x++) { if (ii[x].empty()) continue; int m = ii[x].size(); for (int p = 0; p < m; p++) { D1::node u = D1::qry(p == 0 ? 0 : ii[x][p - 1] + 1, ii[x][p] - 1); D1::node v = D1::qry(ii[x][p] + 1, p == m - 1 ? n - 1 : ii[x][p + 1] - 1); sumL[p] = u.sum; sumR[p] = v.sum; minL[p] = u.minR; maxL[p] = u.maxR; minR[p] = v.minL; maxR[p] = v.maxL; } dnc(0, m - 1); for (int i : ii[x]) { D1::activate(i); } } } int sequence(int n, vi a) { for (int i = 0; i < M_ * 2; i++) mx[i] = -INF; for (int i = 0; i < n; i++) a[i]--; ans = 1; solve(n, a); for (int i = 0; i < n; i++) a[i] = n - a[i] - 1; solve(n, a); return ans; }
#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...