This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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], pp[N], qq[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] = max(pp[l], ss[l] + pp[r]);
qq[i] = max(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] = max(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] = max(x, 0);
while (i > 1)
pul(ss, pp, qq, i >>= 1);
}
int query_p(int *ss, int *pp, int r) {
int l = 0, p = 0;
for (l += n_, r += n_; l <= r; l >>= 1, r >>= 1)
if ((r & 1) == 0)
p = max(pp[r], ss[r] + p), r--;
return p;
}
int query_q(int *ss, int *qq, int l) {
int r = n_ - 1, q = 0;
for (l += n_, r += n_; l <= r; l >>= 1, r >>= 1)
if ((l & 1) == 1)
q = max(q + ss[l], qq[l]), l++;
return q;
}
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;
}
}
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);
build(ss1, pp1, qq1, 1), build(ss2, pp2, qq2, -1);
int ans = 0;
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);
}
if (j * 2 < n) {
for (int h = i; h < j; h++)
pp[h] = query_p(ss1, pp1, ii[h] - 1);
for (int h = i; h < j; h++)
qq[h] = query_q(ss1, qq1, ii[h] + 1);
for (int h = i, h_ = i; h < j; h++) {
while (h_ < j && pp[h] + qq[h_] >= n - j * 2)
h_++;
ans = max(ans, h_ - h);
}
} else if (i * 2 > n) {
for (int h = i; h < j; h++)
pp[h] = query_p(ss2, pp2, ii[h] - 1);
for (int h = i; h < j; h++)
qq[h] = query_q(ss2, qq2, ii[h] + 1);
for (int h = i, h_ = i; h < j; h++) {
while (h_ < j && pp[h] + qq[h_] >= i * 2 - n)
h_++;
ans = max(ans, h_ - h);
}
} else
ans = max(ans, j - i);
for (int h = i; h < j; h++) {
int i_ = ii[h];
update(ss2, pp2, qq2, i_, 1);
}
}
return ans;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |