#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 if (v == -1) {
ret.suf_min = ret.pref_min = -1;
}
return ret;
}
int sum[N];
int suf_max[N], pref_max[N];
int suf_min[N], pref_min[N];
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));
}
}
}
void precalc(int n, vector<vector<int>>& group) {
for (int x = 0; x < n; ++x) {
for (int i : group[x]) {
if (i > 0) {
suf_min[i] = qry(0, 0, n - 1, 0, i - 1).suf_min;
}
if (i < n - 1) {
pref_min[i] = qry(0, 0, n - 1, i + 1, n - 1).pref_min;
}
}
for (int i : group[x]) {
upd(0, 0, n - 1, i, 0);
}
for (int i : group[x]) {
sum[i] = qry(0, 0, n - 1, 0, i).sum;
}
for (int i : group[x]) {
upd(0, 0, n - 1, i, 1);
}
for (int i : group[x]) {
if (i > 0) {
suf_max[i] = qry(0, 0, n - 1, 0, i - 1).suf_max;
}
if (i < n - 1) {
pref_max[i] = qry(0, 0, n - 1, i + 1, n - 1).pref_max;
}
}
}
}
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);
}
precalc(n, group);
int ans = 0;
for (int x = 0; x < n; ++x) {
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 tsum = sum[group[x][i]] - sum[group[x][mid]];
int cc = i - mid + 1;
if (tsum < 0) {
cc += pref_max[group[x][i]];
cc += suf_max[group[x][mid]];
ok &= (tsum + cc >= 0);
} else if (tsum > 0) {
cc -= pref_min[group[x][i]];
cc -= suf_min[group[x][mid]];
ok &= (tsum - 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 |
2 ms |
10584 KB |
Output is correct |
2 |
Correct |
1 ms |
10588 KB |
Output is correct |
3 |
Correct |
1 ms |
10588 KB |
Output is correct |
4 |
Correct |
1 ms |
10588 KB |
Output is correct |
5 |
Correct |
1 ms |
10588 KB |
Output is correct |
6 |
Correct |
3 ms |
10588 KB |
Output is correct |
7 |
Correct |
2 ms |
10588 KB |
Output is correct |
8 |
Correct |
1 ms |
10588 KB |
Output is correct |
9 |
Correct |
2 ms |
10588 KB |
Output is correct |
10 |
Correct |
2 ms |
10584 KB |
Output is correct |
11 |
Correct |
2 ms |
10588 KB |
Output is correct |
12 |
Correct |
1 ms |
10696 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
10584 KB |
Output is correct |
2 |
Correct |
1 ms |
10588 KB |
Output is correct |
3 |
Correct |
1 ms |
10588 KB |
Output is correct |
4 |
Correct |
1 ms |
10588 KB |
Output is correct |
5 |
Correct |
1 ms |
10588 KB |
Output is correct |
6 |
Correct |
3 ms |
10588 KB |
Output is correct |
7 |
Correct |
2 ms |
10588 KB |
Output is correct |
8 |
Correct |
1 ms |
10588 KB |
Output is correct |
9 |
Correct |
2 ms |
10588 KB |
Output is correct |
10 |
Correct |
2 ms |
10584 KB |
Output is correct |
11 |
Correct |
2 ms |
10588 KB |
Output is correct |
12 |
Correct |
1 ms |
10696 KB |
Output is correct |
13 |
Correct |
4 ms |
10840 KB |
Output is correct |
14 |
Correct |
4 ms |
10844 KB |
Output is correct |
15 |
Correct |
3 ms |
10844 KB |
Output is correct |
16 |
Correct |
4 ms |
10844 KB |
Output is correct |
17 |
Correct |
3 ms |
10844 KB |
Output is correct |
18 |
Correct |
3 ms |
10844 KB |
Output is correct |
19 |
Correct |
4 ms |
10844 KB |
Output is correct |
20 |
Correct |
4 ms |
10844 KB |
Output is correct |
21 |
Correct |
3 ms |
10844 KB |
Output is correct |
22 |
Correct |
4 ms |
10844 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
10584 KB |
Output is correct |
2 |
Correct |
693 ms |
55252 KB |
Output is correct |
3 |
Correct |
717 ms |
55344 KB |
Output is correct |
4 |
Correct |
662 ms |
47720 KB |
Output is correct |
5 |
Correct |
707 ms |
57696 KB |
Output is correct |
6 |
Correct |
690 ms |
57680 KB |
Output is correct |
7 |
Correct |
657 ms |
49232 KB |
Output is correct |
8 |
Correct |
659 ms |
49364 KB |
Output is correct |
9 |
Correct |
669 ms |
48656 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
10588 KB |
Output is correct |
2 |
Correct |
717 ms |
47640 KB |
Output is correct |
3 |
Correct |
704 ms |
48528 KB |
Output is correct |
4 |
Correct |
719 ms |
48568 KB |
Output is correct |
5 |
Correct |
710 ms |
48892 KB |
Output is correct |
6 |
Correct |
698 ms |
48576 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
937 ms |
61268 KB |
Output is correct |
2 |
Correct |
950 ms |
61264 KB |
Output is correct |
3 |
Correct |
995 ms |
60512 KB |
Output is correct |
4 |
Correct |
946 ms |
60516 KB |
Output is correct |
5 |
Correct |
811 ms |
57180 KB |
Output is correct |
6 |
Correct |
801 ms |
57432 KB |
Output is correct |
7 |
Correct |
688 ms |
56116 KB |
Output is correct |
8 |
Correct |
654 ms |
55636 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
10584 KB |
Output is correct |
2 |
Correct |
1 ms |
10588 KB |
Output is correct |
3 |
Correct |
1 ms |
10588 KB |
Output is correct |
4 |
Correct |
1 ms |
10588 KB |
Output is correct |
5 |
Correct |
1 ms |
10588 KB |
Output is correct |
6 |
Correct |
3 ms |
10588 KB |
Output is correct |
7 |
Correct |
2 ms |
10588 KB |
Output is correct |
8 |
Correct |
1 ms |
10588 KB |
Output is correct |
9 |
Correct |
2 ms |
10588 KB |
Output is correct |
10 |
Correct |
2 ms |
10584 KB |
Output is correct |
11 |
Correct |
2 ms |
10588 KB |
Output is correct |
12 |
Correct |
1 ms |
10696 KB |
Output is correct |
13 |
Correct |
4 ms |
10840 KB |
Output is correct |
14 |
Correct |
4 ms |
10844 KB |
Output is correct |
15 |
Correct |
3 ms |
10844 KB |
Output is correct |
16 |
Correct |
4 ms |
10844 KB |
Output is correct |
17 |
Correct |
3 ms |
10844 KB |
Output is correct |
18 |
Correct |
3 ms |
10844 KB |
Output is correct |
19 |
Correct |
4 ms |
10844 KB |
Output is correct |
20 |
Correct |
4 ms |
10844 KB |
Output is correct |
21 |
Correct |
3 ms |
10844 KB |
Output is correct |
22 |
Correct |
4 ms |
10844 KB |
Output is correct |
23 |
Correct |
128 ms |
18232 KB |
Output is correct |
24 |
Correct |
128 ms |
18264 KB |
Output is correct |
25 |
Correct |
128 ms |
18384 KB |
Output is correct |
26 |
Correct |
125 ms |
17492 KB |
Output is correct |
27 |
Correct |
118 ms |
17240 KB |
Output is correct |
28 |
Correct |
117 ms |
17244 KB |
Output is correct |
29 |
Correct |
99 ms |
16896 KB |
Output is correct |
30 |
Correct |
107 ms |
16964 KB |
Output is correct |
31 |
Correct |
95 ms |
17000 KB |
Output is correct |
32 |
Correct |
95 ms |
19304 KB |
Output is correct |
33 |
Correct |
123 ms |
18204 KB |
Output is correct |
34 |
Correct |
139 ms |
18372 KB |
Output is correct |
35 |
Correct |
123 ms |
18012 KB |
Output is correct |
36 |
Correct |
125 ms |
18168 KB |
Output is correct |
37 |
Correct |
125 ms |
18012 KB |
Output is correct |
38 |
Correct |
136 ms |
18224 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
10584 KB |
Output is correct |
2 |
Correct |
1 ms |
10588 KB |
Output is correct |
3 |
Correct |
1 ms |
10588 KB |
Output is correct |
4 |
Correct |
1 ms |
10588 KB |
Output is correct |
5 |
Correct |
1 ms |
10588 KB |
Output is correct |
6 |
Correct |
3 ms |
10588 KB |
Output is correct |
7 |
Correct |
2 ms |
10588 KB |
Output is correct |
8 |
Correct |
1 ms |
10588 KB |
Output is correct |
9 |
Correct |
2 ms |
10588 KB |
Output is correct |
10 |
Correct |
2 ms |
10584 KB |
Output is correct |
11 |
Correct |
2 ms |
10588 KB |
Output is correct |
12 |
Correct |
1 ms |
10696 KB |
Output is correct |
13 |
Correct |
4 ms |
10840 KB |
Output is correct |
14 |
Correct |
4 ms |
10844 KB |
Output is correct |
15 |
Correct |
3 ms |
10844 KB |
Output is correct |
16 |
Correct |
4 ms |
10844 KB |
Output is correct |
17 |
Correct |
3 ms |
10844 KB |
Output is correct |
18 |
Correct |
3 ms |
10844 KB |
Output is correct |
19 |
Correct |
4 ms |
10844 KB |
Output is correct |
20 |
Correct |
4 ms |
10844 KB |
Output is correct |
21 |
Correct |
3 ms |
10844 KB |
Output is correct |
22 |
Correct |
4 ms |
10844 KB |
Output is correct |
23 |
Correct |
693 ms |
55252 KB |
Output is correct |
24 |
Correct |
717 ms |
55344 KB |
Output is correct |
25 |
Correct |
662 ms |
47720 KB |
Output is correct |
26 |
Correct |
707 ms |
57696 KB |
Output is correct |
27 |
Correct |
690 ms |
57680 KB |
Output is correct |
28 |
Correct |
657 ms |
49232 KB |
Output is correct |
29 |
Correct |
659 ms |
49364 KB |
Output is correct |
30 |
Correct |
669 ms |
48656 KB |
Output is correct |
31 |
Correct |
717 ms |
47640 KB |
Output is correct |
32 |
Correct |
704 ms |
48528 KB |
Output is correct |
33 |
Correct |
719 ms |
48568 KB |
Output is correct |
34 |
Correct |
710 ms |
48892 KB |
Output is correct |
35 |
Correct |
698 ms |
48576 KB |
Output is correct |
36 |
Correct |
937 ms |
61268 KB |
Output is correct |
37 |
Correct |
950 ms |
61264 KB |
Output is correct |
38 |
Correct |
995 ms |
60512 KB |
Output is correct |
39 |
Correct |
946 ms |
60516 KB |
Output is correct |
40 |
Correct |
811 ms |
57180 KB |
Output is correct |
41 |
Correct |
801 ms |
57432 KB |
Output is correct |
42 |
Correct |
688 ms |
56116 KB |
Output is correct |
43 |
Correct |
654 ms |
55636 KB |
Output is correct |
44 |
Correct |
128 ms |
18232 KB |
Output is correct |
45 |
Correct |
128 ms |
18264 KB |
Output is correct |
46 |
Correct |
128 ms |
18384 KB |
Output is correct |
47 |
Correct |
125 ms |
17492 KB |
Output is correct |
48 |
Correct |
118 ms |
17240 KB |
Output is correct |
49 |
Correct |
117 ms |
17244 KB |
Output is correct |
50 |
Correct |
99 ms |
16896 KB |
Output is correct |
51 |
Correct |
107 ms |
16964 KB |
Output is correct |
52 |
Correct |
95 ms |
17000 KB |
Output is correct |
53 |
Correct |
95 ms |
19304 KB |
Output is correct |
54 |
Correct |
123 ms |
18204 KB |
Output is correct |
55 |
Correct |
139 ms |
18372 KB |
Output is correct |
56 |
Correct |
123 ms |
18012 KB |
Output is correct |
57 |
Correct |
125 ms |
18168 KB |
Output is correct |
58 |
Correct |
125 ms |
18012 KB |
Output is correct |
59 |
Correct |
136 ms |
18224 KB |
Output is correct |
60 |
Correct |
1386 ms |
58684 KB |
Output is correct |
61 |
Correct |
1300 ms |
58844 KB |
Output is correct |
62 |
Correct |
1291 ms |
58704 KB |
Output is correct |
63 |
Correct |
1232 ms |
50528 KB |
Output is correct |
64 |
Correct |
1239 ms |
50516 KB |
Output is correct |
65 |
Correct |
1235 ms |
50648 KB |
Output is correct |
66 |
Correct |
754 ms |
48628 KB |
Output is correct |
67 |
Correct |
747 ms |
48952 KB |
Output is correct |
68 |
Correct |
690 ms |
50764 KB |
Output is correct |
69 |
Correct |
670 ms |
64412 KB |
Output is correct |
70 |
Correct |
1200 ms |
57548 KB |
Output is correct |
71 |
Correct |
1209 ms |
57544 KB |
Output is correct |
72 |
Correct |
1161 ms |
56988 KB |
Output is correct |
73 |
Correct |
1203 ms |
57284 KB |
Output is correct |
74 |
Correct |
1154 ms |
57040 KB |
Output is correct |
75 |
Correct |
1239 ms |
57264 KB |
Output is correct |