#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 {
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) {
if (i < n - 1) {
cc += qry(0, 0, n - 1, i + 1, n - 1).pref_max;
}
if (i > 0) {
cc += qry(0, 0, n - 1, 0, i - 1).suf_max;
}
ok &= (sum + cc >= 0);
} else if (sum > 0) {
if (i < n - 1) {
cc += qry(0, 0, n - 1, i + 1, n - 1).pref_min;
}
if (i > 0) {
cc += qry(0, 0, n - 1, 0, i - 1).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 time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Incorrect |
0 ms |
344 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Incorrect |
0 ms |
344 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
310 ms |
48908 KB |
Output is correct |
3 |
Correct |
345 ms |
48856 KB |
Output is correct |
4 |
Execution timed out |
2047 ms |
38692 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Execution timed out |
2049 ms |
38652 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
381 ms |
54608 KB |
Output is correct |
2 |
Correct |
384 ms |
54648 KB |
Output is correct |
3 |
Incorrect |
404 ms |
53956 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Incorrect |
0 ms |
344 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Incorrect |
0 ms |
344 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |