#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) {
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 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 |
348 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 |
348 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 |
306 ms |
45416 KB |
Output is correct |
3 |
Correct |
364 ms |
45584 KB |
Output is correct |
4 |
Execution timed out |
2049 ms |
37588 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 |
2025 ms |
37748 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
394 ms |
51552 KB |
Output is correct |
2 |
Correct |
370 ms |
51044 KB |
Output is correct |
3 |
Correct |
409 ms |
50732 KB |
Output is correct |
4 |
Correct |
384 ms |
53840 KB |
Output is correct |
5 |
Correct |
391 ms |
50612 KB |
Output is correct |
6 |
Correct |
377 ms |
50612 KB |
Output is correct |
7 |
Correct |
361 ms |
49440 KB |
Output is correct |
8 |
Incorrect |
354 ms |
48976 KB |
Output isn't correct |
9 |
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 |
348 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 |
348 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |