제출 #838772

#제출 시각아이디문제언어결과실행 시간메모리
838772arbuzick서열 (APIO23_sequence)C++17
100 / 100
1373 ms62412 KiB
#include <bits/stdc++.h> using namespace std; const int R = 1 << 19; int mn[R * 2], mx[R * 2], psh[R * 2]; void push(int node) { if (psh[node] != 0) { mn[node * 2] += psh[node]; mx[node * 2] += psh[node]; psh[node * 2] += psh[node]; mn[node * 2 + 1] += psh[node]; mx[node * 2 + 1] += psh[node]; psh[node * 2 + 1] += psh[node]; } psh[node] = 0; } void add(int ql, int qr, int val, int node = 1, int nl = 0, int nr = R) { if (ql <= nl && nr <= qr) { mn[node] += val; mx[node] += val; psh[node] += val; return; } if (qr <= nl || nr <= ql) { return; } push(node); int nm = (nl + nr) / 2; add(ql, qr, val, node * 2, nl, nm); add(ql, qr, val, node * 2 + 1, nm, nr); mn[node] = min(mn[node * 2], mn[node * 2 + 1]); mx[node] = max(mx[node * 2], mx[node * 2 + 1]); } pair<int, int> get(int ql, int qr, int node = 1, int nl = 0, int nr = R) { if (ql <= nl && nr <= qr) { return {mn[node], mx[node]}; } if (qr <= nl || nr <= ql) { return {R, -R}; } push(node); int nm = (nl + nr) / 2; auto p1 = get(ql, qr, node * 2, nl, nm), p2 = get(ql, qr, node * 2 + 1, nm, nr); mn[node] = min(mn[node * 2], mn[node * 2 + 1]); mx[node] = max(mx[node * 2], mx[node * 2 + 1]); return {min(p1.first, p2.first), max(p1.second, p2.second)}; } int sequence(int n, vector<int> a) { vector<vector<int>> pos(n); for (int i = 0; i < n; ++i) { a[i]--; pos[a[i]].push_back(i); } for (int i = 0; i < n; ++i) { add(i + 1, n + 1, 1); } int ans = 1; vector<int> pr_min1(n), pr_min2(n), pr_max1(n), pr_max2(n), suf_min1(n), suf_min2(n), suf_max1(n), suf_max2(n); for (int vl = 0; vl < n; ++vl) { for (int i = 0; i < (int)pos[vl].size(); ++i) { auto p = get(0, pos[vl][i] + 1); pr_min1[i] = p.first; pr_max1[i] = p.second; p = get(pos[vl][i] + 1, n + 1); suf_min1[i] = p.first; suf_max1[i] = p.second; } for (auto p : pos[vl]) { add(p + 1, n + 1, -2); } for (int i = 0; i < (int)pos[vl].size(); ++i) { auto p = get(0, pos[vl][i] + 1); pr_min2[i] = p.first; pr_max2[i] = p.second; p = get(pos[vl][i] + 1, n + 1); suf_min2[i] = p.first; suf_max2[i] = p.second; } int l = 1, r = pos[vl].size() + 1; while (l < r - 1) { int m = (l + r) / 2; bool check = false; for (int i = 0; i + m <= (int)pos[vl].size(); ++i) { if (pr_min1[i] <= suf_max1[i + m - 1] && suf_min1[i + m - 1] <= pr_max1[i]) { check = true; } else if (pr_min2[i] <= suf_max2[i + m - 1] && suf_min2[i + m - 1] <= pr_max2[i]) { check = true; } else if (pr_max1[i] <= suf_min1[i + m - 1] && suf_max2[i + m - 1] <= pr_min2[i]) { check = true; } else if (suf_max1[i] <= pr_min1[i + m - 1] && pr_max2[i + m - 1] <= suf_min2[i]) { check = true; } } if (check) { l = m; } else { r = m; } } ans = max(ans, l); } 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...