Submission #758493

#TimeUsernameProblemLanguageResultExecution timeMemory
758493rainboySequence (APIO23_sequence)C++17
50 / 100
2087 ms32708 KiB
#include "sequence.h" #include <vector> using namespace std; typedef vector<int> vi; const int N = 500000, N_ = 1 << 19; /* N_ = pow2(ceil(log2(N + 1))) */ unsigned int X = 12345; int rand_() { return (X *= 3) >> 1; } int aa[N], ii[N], n; int ss1[N_ * 2], pp1[N_ * 2], qq1[N_ * 2], ss2[N_ * 2], pp2[N_ * 2], qq2[N_ * 2], n_; void pul(int *ss, int *pp, int *qq, int i) { int l = i << 1, r = l | 1; ss[i] = ss[l] + ss[r]; pp[i] = min(pp[l], ss[l] + pp[r]); qq[i] = min(qq[l] + ss[r], qq[r]); } void build(int *ss, int *pp, int *qq, int x) { for (int i = 0; i < n_; i++) ss[n_ + i] = i < n ? x : 0, pp[n_ + i] = qq[n_ + i] = min(ss[n_ + i], 0); for (int i = n_ - 1; i > 0; i--) pul(ss, pp, qq, i); } void update(int *ss, int *pp, int *qq, int i, int x) { i += n_; ss[i] = x, pp[i] = qq[i] = min(x, 0); while (i > 1) pul(ss, pp, qq, i >>= 1); } int query_s(int *ss, int l, int r) { int s = 0; for (l += n_, r += n_; l <= r; l >>= 1, r >>= 1) { if ((l & 1) == 1) s += ss[l++]; if ((r & 1) == 0) s += ss[r--]; } return s; } int query_q(int *ss, int *qq, int r) { int l = 0, s = 0, q = 0; for (l += n_, r += n_; l <= r; l >>= 1, r >>= 1) if ((r & 1) == 0) q = min(qq[r] + s, q), s += ss[r], r--; return q; } int query_p(int *ss, int *pp, int l) { int r = n_ - 1, s = 0, p = 0; for (l += n_, r += n_; l <= r; l >>= 1, r >>= 1) if ((l & 1) == 1) p = min(p, s + pp[l]), s += ss[l], l++; return p; } void sort(int *ii, int l, int r) { while (l < r) { int i = l, j = l, k = r, i_ = ii[l + rand_() % (r - l)], tmp; while (j < k) { int c = aa[ii[j]] != aa[i_] ? aa[ii[j]] - aa[i_] : ii[j] - i_; if (c == 0) j++; else if (c < 0) { tmp = ii[i], ii[i] = ii[j], ii[j] = tmp; i++, j++; } else { k--; tmp = ii[j], ii[j] = ii[k], ii[k] = tmp; } } sort(ii, l, i); l = k; } } bool solve(int k) { build(ss1, pp1, qq1, 1), build(ss2, pp2, qq2, -1); for (int i = 0, j; i < n; i = j) { j = i + 1; while (j < n && aa[ii[j]] == aa[ii[i]]) j++; for (int h = i; h < j; h++) { int i_ = ii[h]; update(ss1, pp1, qq1, i_, -1); } for (int h = i; h + k <= j; h++) { int l = ii[h], r = ii[h + k - 1], s; if ((s = query_s(ss1, l, r)) > 0) { if (query_q(ss1, qq1, l - 1) + s + query_p(ss1, pp1, r + 1) <= 0) return true; } else if ((s = query_s(ss2, l, r)) > 0) { if (query_q(ss2, qq2, l - 1) + s + query_p(ss2, pp2, r + 1) <= 0) return true; } else return true; } for (int h = i; h < j; h++) { int i_ = ii[h]; update(ss2, pp2, qq2, i_, 1); } } return false; } int sequence(int n1, vi aa_) { n = n1; n_ = 1; while (n_ <= n) n_ <<= 1; for (int i = 0; i < n; i++) { aa[i] = aa_[i]; ii[i] = i; } sort(ii, 0, n); int lower = 0, upper = n + 1; while (upper - lower > 1) { int k = (lower + upper) / 2; if (solve(k)) lower = k; else upper = k; } return lower; }
#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...