제출 #877200

#제출 시각아이디문제언어결과실행 시간메모리
877200NeroZein서열 (APIO23_sequence)C++17
13 / 100
2043 ms54100 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; } 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)); } } } 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); } int ans = 0; for (int x = 0; x < n; ++x) { for (int i : group[x]) { upd(0, 0, n - 1, i, 0); } 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 sum = qry(0, 0, n - 1, group[x][mid], group[x][i]).sum; int cc = i - mid + 1; if (sum < 0) { cc += qry(0, 0, n - 1, group[x][i], n - 1).pref_max; cc += qry(0, 0, n - 1, 0, group[x][mid]).suf_max; ok &= (sum + cc >= 0); } else if (sum > 0) { cc -= qry(0, 0, n - 1, group[x][i], n - 1).pref_min; cc -= qry(0, 0, n - 1, 0, group[x][mid]).suf_min; ok &= (sum - 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...