제출 #877208

#제출 시각아이디문제언어결과실행 시간메모리
877208NeroZein서열 (APIO23_sequence)C++17
100 / 100
1386 ms64412 KiB
#include "sequence.h" #include "bits/stdc++.h" using namespace std; const int N = 5e5 + 5; struct node { int sum = 0; int suf_max = 0, pref_max = 0; int suf_min = 0, pref_min = 0; }; node comb(node lx, node rx) { node ret; ret.sum = lx.sum + rx.sum; ret.suf_max = max(rx.suf_max, rx.sum + lx.suf_max); ret.pref_max = max(lx.pref_max, lx.sum + rx.pref_max); ret.suf_min = min(rx.suf_min, rx.sum + lx.suf_min); ret.pref_min = min(lx.pref_min, lx.sum + rx.pref_min); return ret; } node init(int v) { node ret; ret.sum = v; if (v == 1) { ret.suf_max = ret.pref_max = 1; } else if (v == -1) { ret.suf_min = ret.pref_min = -1; } return ret; } int sum[N]; int suf_max[N], pref_max[N]; int suf_min[N], pref_min[N]; node seg[N * 2]; void upd(int nd, int l, int r, int p, int v) { if (l == r) { seg[nd] = init(v); return; } int mid = (l + r) >> 1; int rs = nd + ((mid - l + 1) << 1); if (p <= mid) { upd(nd + 1, l, mid, p, v); } else { upd(rs, mid + 1, r, p, v); } seg[nd] = comb(seg[nd + 1], seg[rs]); } node qry(int nd, int l, int r, int s, int e) { if (l >= s && r <= e) { return seg[nd]; } int mid = (l + r) >> 1; int rs = nd + ((mid - l + 1) << 1); if (mid >= e) { return qry(nd + 1, l, mid, s, e); } else { if (mid < s) { return qry(rs, mid + 1, r, s, e); } else { return comb(qry(nd + 1, l, mid, s, e), qry(rs, mid + 1, r, s, e)); } } } void precalc(int n, vector<vector<int>>& group) { for (int x = 0; x < n; ++x) { for (int i : group[x]) { if (i > 0) { suf_min[i] = qry(0, 0, n - 1, 0, i - 1).suf_min; } if (i < n - 1) { pref_min[i] = qry(0, 0, n - 1, i + 1, n - 1).pref_min; } } for (int i : group[x]) { upd(0, 0, n - 1, i, 0); } for (int i : group[x]) { sum[i] = qry(0, 0, n - 1, 0, i).sum; } for (int i : group[x]) { upd(0, 0, n - 1, i, 1); } for (int i : group[x]) { if (i > 0) { suf_max[i] = qry(0, 0, n - 1, 0, i - 1).suf_max; } if (i < n - 1) { pref_max[i] = qry(0, 0, n - 1, i + 1, n - 1).pref_max; } } } } int sequence(int n, vector<int> a) { vector<vector<int>> group(n); for (int i = 0; i < n; ++i) { --a[i]; group[a[i]].push_back(i); } for (int i = 0; i < n; ++i) { upd(0, 0, n - 1, i, -1); } precalc(n, group); int ans = 0; for (int x = 0; x < n; ++x) { for (int i = 0; i < (int) group[x].size(); ++i) { int l = 0, r = i; while (l < r) { bool ok = true; int mid = (l + r) / 2; int tsum = sum[group[x][i]] - sum[group[x][mid]]; int cc = i - mid + 1; if (tsum < 0) { cc += pref_max[group[x][i]]; cc += suf_max[group[x][mid]]; ok &= (tsum + cc >= 0); } else if (tsum > 0) { cc -= pref_min[group[x][i]]; cc -= suf_min[group[x][mid]]; ok &= (tsum - cc <= 0); } (ok ? r = mid : l = mid + 1); } ans = max(ans, i - l + 1); } for (int i : group[x]) { upd(0, 0, n - 1, i, 1); } } 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...