#include<bits/stdc++.h>
#define fi first
#define se second
#define endl "\n"
#define ii pair<int, int>
using namespace std;
const int N = 5e5 + 10;
vector<int> pos[N];
int L[N], R[N];
int minx[N << 2], maxx[N << 2], lazy[N << 2];
int st[N], idx[N];
void push(int s, int l, int r) {
if (!lazy[s]) return;
minx[s] += lazy[s];
maxx[s] += lazy[s];
if (l != r) {
lazy[s << 1] += lazy[s];
lazy[s << 1 | 1] += lazy[s];
}
lazy[s] = 0;
}
void up(int s, int l, int r, int from, int to, int val) {
push(s, l, r);
if (l > to || r < from) return;
if (from <= l && r <= to) {
lazy[s] = val;
push(s, l, r);
return;
}
int mid = l + r >> 1;
up(s << 1, l, mid, from, to, val);
up(s << 1 | 1, mid + 1, r, from, to, val);
minx[s] = min(minx[s << 1], minx[s << 1 | 1]);
maxx[s] = max(maxx[s << 1], maxx[s << 1 | 1]);
}
int get_min(int s, int l, int r, int from, int to) {
push(s, l, r);
if (l > to || r < from) return 1e9;
if (from <= l && r <= to) return minx[s];
int mid = l + r >> 1;
return min(get_min(s << 1, l, mid, from, to), get_min(s << 1 | 1, mid + 1, r, from, to));
}
int get_max(int s, int l, int r, int from, int to) {
push(s, l, r);
if (l > to || r < from) return -1e9;
if (from <= l && r <= to) return maxx[s];
int mid = l + r >> 1;
return max(get_max(s << 1, l, mid, from, to), get_max(s << 1 | 1, mid + 1, r, from, to));
}
int sequence(int n, vector<int> a) {
int ans = 1;
a.insert(a.begin(), 0);
for(int i = 1; i <= n; i++) pos[a[i]].push_back(i);
for(int i = 1; i <= n; i++) up(1, 0, n, i, i, -i);
for(int val = 1; val <= n; val++) {
int pre = 0;
int j = 0, k = 0;
st[0] = 1e9;
for(int i = 0; i < pos[val].size(); i++) {
int cur = pos[val][i];
L[i] = get_min(1, 0, n, 0, cur - 1);
R[i] = get_max(1, 0, n, 0, cur - 1);
int from = get_min(1, 0, n, cur, n);
int to = get_max(1, 0, n, cur, n);
while (j <= i && L[j] > to || R[j] < from) {
if (L[j] + j * 2 <= st[k]) {
++k;
st[k] = L[j] + j * 2;
idx[k] = j;
}
j++;
}
if (j > 0 && L[j - 1] > to) {
int l = 1, r = k, last = 0;
while (l <= r) {
int mid = l + r >> 1;
if (to + (i + 1) * 2 >= st[mid]) {
last = mid;
r = mid - 1;
} else l = mid + 1;
}
if (last) ans = max(ans, i - idx[last] + 1);
}
ans = max(ans, i - j + 1);
}
for(int &j : pos[val]) up(1, 0, n, j, n, 2);
}
return ans;
}
#ifdef ngu
int main() {
freopen ("task.inp", "r", stdin);
freopen ("task.out", "w", stdout);
int n; cin >> n;
vector<int> a(n);
for(int i = 0; i < n; i++) cin >> a[i];
cout << sequence(n, a);
//// cout << sequence(7, {1, 2, 3, 1, 2, 1, 3});
//// cout << sequence(9, {1, 1, 2, 3, 4, 3, 2, 1, 1});
// cout << sequence(14, {2, 6, 2, 5, 3, 4, 2, 1, 4, 3, 5, 6, 3, 2});
}
#endif // ngu
Compilation message
sequence.cpp: In function 'void up(int, int, int, int, int, int)':
sequence.cpp:36:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
36 | int mid = l + r >> 1;
| ~~^~~
sequence.cpp: In function 'int get_min(int, int, int, int, int)':
sequence.cpp:47:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
47 | int mid = l + r >> 1;
| ~~^~~
sequence.cpp: In function 'int get_max(int, int, int, int, int)':
sequence.cpp:54:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
54 | int mid = l + r >> 1;
| ~~^~~
sequence.cpp: In function 'int sequence(int, std::vector<int>)':
sequence.cpp:71:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
71 | for(int i = 0; i < pos[val].size(); i++) {
| ~~^~~~~~~~~~~~~~~~~
sequence.cpp:78:23: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
78 | while (j <= i && L[j] > to || R[j] < from) {
| ~~~~~~~^~~~~~~~~~~~
sequence.cpp:92:25: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
92 | int mid = l + r >> 1;
| ~~^~~
sequence.cpp:68:11: warning: unused variable 'pre' [-Wunused-variable]
68 | int pre = 0;
| ^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
22620 KB |
Output is correct |
2 |
Correct |
4 ms |
22620 KB |
Output is correct |
3 |
Correct |
4 ms |
22620 KB |
Output is correct |
4 |
Correct |
6 ms |
23132 KB |
Output is correct |
5 |
Correct |
4 ms |
22620 KB |
Output is correct |
6 |
Correct |
5 ms |
22704 KB |
Output is correct |
7 |
Correct |
5 ms |
22620 KB |
Output is correct |
8 |
Correct |
5 ms |
22620 KB |
Output is correct |
9 |
Correct |
4 ms |
22620 KB |
Output is correct |
10 |
Correct |
4 ms |
22620 KB |
Output is correct |
11 |
Correct |
5 ms |
22620 KB |
Output is correct |
12 |
Correct |
4 ms |
22620 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
22620 KB |
Output is correct |
2 |
Correct |
4 ms |
22620 KB |
Output is correct |
3 |
Correct |
4 ms |
22620 KB |
Output is correct |
4 |
Correct |
6 ms |
23132 KB |
Output is correct |
5 |
Correct |
4 ms |
22620 KB |
Output is correct |
6 |
Correct |
5 ms |
22704 KB |
Output is correct |
7 |
Correct |
5 ms |
22620 KB |
Output is correct |
8 |
Correct |
5 ms |
22620 KB |
Output is correct |
9 |
Correct |
4 ms |
22620 KB |
Output is correct |
10 |
Correct |
4 ms |
22620 KB |
Output is correct |
11 |
Correct |
5 ms |
22620 KB |
Output is correct |
12 |
Correct |
4 ms |
22620 KB |
Output is correct |
13 |
Correct |
6 ms |
22876 KB |
Output is correct |
14 |
Correct |
6 ms |
22876 KB |
Output is correct |
15 |
Correct |
6 ms |
22856 KB |
Output is correct |
16 |
Correct |
6 ms |
22620 KB |
Output is correct |
17 |
Correct |
7 ms |
22620 KB |
Output is correct |
18 |
Correct |
7 ms |
22876 KB |
Output is correct |
19 |
Correct |
6 ms |
22620 KB |
Output is correct |
20 |
Correct |
6 ms |
22876 KB |
Output is correct |
21 |
Correct |
7 ms |
22876 KB |
Output is correct |
22 |
Correct |
6 ms |
22876 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
22620 KB |
Output is correct |
2 |
Correct |
664 ms |
50604 KB |
Output is correct |
3 |
Correct |
762 ms |
50092 KB |
Output is correct |
4 |
Correct |
614 ms |
41644 KB |
Output is correct |
5 |
Correct |
673 ms |
48484 KB |
Output is correct |
6 |
Correct |
664 ms |
52604 KB |
Output is correct |
7 |
Correct |
620 ms |
42748 KB |
Output is correct |
8 |
Correct |
621 ms |
44324 KB |
Output is correct |
9 |
Correct |
795 ms |
44224 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
22620 KB |
Output is correct |
2 |
Correct |
652 ms |
45056 KB |
Output is correct |
3 |
Correct |
636 ms |
42892 KB |
Output is correct |
4 |
Correct |
636 ms |
43816 KB |
Output is correct |
5 |
Correct |
647 ms |
46112 KB |
Output is correct |
6 |
Correct |
652 ms |
44128 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
831 ms |
54764 KB |
Output is correct |
2 |
Correct |
855 ms |
55272 KB |
Output is correct |
3 |
Correct |
830 ms |
54648 KB |
Output is correct |
4 |
Correct |
1125 ms |
54620 KB |
Output is correct |
5 |
Correct |
738 ms |
52300 KB |
Output is correct |
6 |
Correct |
743 ms |
51376 KB |
Output is correct |
7 |
Correct |
650 ms |
49580 KB |
Output is correct |
8 |
Correct |
644 ms |
49112 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
22620 KB |
Output is correct |
2 |
Correct |
4 ms |
22620 KB |
Output is correct |
3 |
Correct |
4 ms |
22620 KB |
Output is correct |
4 |
Correct |
6 ms |
23132 KB |
Output is correct |
5 |
Correct |
4 ms |
22620 KB |
Output is correct |
6 |
Correct |
5 ms |
22704 KB |
Output is correct |
7 |
Correct |
5 ms |
22620 KB |
Output is correct |
8 |
Correct |
5 ms |
22620 KB |
Output is correct |
9 |
Correct |
4 ms |
22620 KB |
Output is correct |
10 |
Correct |
4 ms |
22620 KB |
Output is correct |
11 |
Correct |
5 ms |
22620 KB |
Output is correct |
12 |
Correct |
4 ms |
22620 KB |
Output is correct |
13 |
Correct |
6 ms |
22876 KB |
Output is correct |
14 |
Correct |
6 ms |
22876 KB |
Output is correct |
15 |
Correct |
6 ms |
22856 KB |
Output is correct |
16 |
Correct |
6 ms |
22620 KB |
Output is correct |
17 |
Correct |
7 ms |
22620 KB |
Output is correct |
18 |
Correct |
7 ms |
22876 KB |
Output is correct |
19 |
Correct |
6 ms |
22620 KB |
Output is correct |
20 |
Correct |
6 ms |
22876 KB |
Output is correct |
21 |
Correct |
7 ms |
22876 KB |
Output is correct |
22 |
Correct |
6 ms |
22876 KB |
Output is correct |
23 |
Correct |
125 ms |
28024 KB |
Output is correct |
24 |
Correct |
114 ms |
27924 KB |
Output is correct |
25 |
Correct |
117 ms |
27924 KB |
Output is correct |
26 |
Correct |
112 ms |
27168 KB |
Output is correct |
27 |
Correct |
106 ms |
27160 KB |
Output is correct |
28 |
Correct |
108 ms |
27128 KB |
Output is correct |
29 |
Correct |
94 ms |
26900 KB |
Output is correct |
30 |
Correct |
94 ms |
26904 KB |
Output is correct |
31 |
Correct |
90 ms |
26780 KB |
Output is correct |
32 |
Correct |
95 ms |
29356 KB |
Output is correct |
33 |
Correct |
117 ms |
28348 KB |
Output is correct |
34 |
Correct |
119 ms |
28180 KB |
Output is correct |
35 |
Correct |
113 ms |
28112 KB |
Output is correct |
36 |
Correct |
113 ms |
28188 KB |
Output is correct |
37 |
Correct |
112 ms |
28188 KB |
Output is correct |
38 |
Correct |
112 ms |
28364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
22620 KB |
Output is correct |
2 |
Correct |
4 ms |
22620 KB |
Output is correct |
3 |
Correct |
4 ms |
22620 KB |
Output is correct |
4 |
Correct |
6 ms |
23132 KB |
Output is correct |
5 |
Correct |
4 ms |
22620 KB |
Output is correct |
6 |
Correct |
5 ms |
22704 KB |
Output is correct |
7 |
Correct |
5 ms |
22620 KB |
Output is correct |
8 |
Correct |
5 ms |
22620 KB |
Output is correct |
9 |
Correct |
4 ms |
22620 KB |
Output is correct |
10 |
Correct |
4 ms |
22620 KB |
Output is correct |
11 |
Correct |
5 ms |
22620 KB |
Output is correct |
12 |
Correct |
4 ms |
22620 KB |
Output is correct |
13 |
Correct |
6 ms |
22876 KB |
Output is correct |
14 |
Correct |
6 ms |
22876 KB |
Output is correct |
15 |
Correct |
6 ms |
22856 KB |
Output is correct |
16 |
Correct |
6 ms |
22620 KB |
Output is correct |
17 |
Correct |
7 ms |
22620 KB |
Output is correct |
18 |
Correct |
7 ms |
22876 KB |
Output is correct |
19 |
Correct |
6 ms |
22620 KB |
Output is correct |
20 |
Correct |
6 ms |
22876 KB |
Output is correct |
21 |
Correct |
7 ms |
22876 KB |
Output is correct |
22 |
Correct |
6 ms |
22876 KB |
Output is correct |
23 |
Correct |
664 ms |
50604 KB |
Output is correct |
24 |
Correct |
762 ms |
50092 KB |
Output is correct |
25 |
Correct |
614 ms |
41644 KB |
Output is correct |
26 |
Correct |
673 ms |
48484 KB |
Output is correct |
27 |
Correct |
664 ms |
52604 KB |
Output is correct |
28 |
Correct |
620 ms |
42748 KB |
Output is correct |
29 |
Correct |
621 ms |
44324 KB |
Output is correct |
30 |
Correct |
795 ms |
44224 KB |
Output is correct |
31 |
Correct |
652 ms |
45056 KB |
Output is correct |
32 |
Correct |
636 ms |
42892 KB |
Output is correct |
33 |
Correct |
636 ms |
43816 KB |
Output is correct |
34 |
Correct |
647 ms |
46112 KB |
Output is correct |
35 |
Correct |
652 ms |
44128 KB |
Output is correct |
36 |
Correct |
831 ms |
54764 KB |
Output is correct |
37 |
Correct |
855 ms |
55272 KB |
Output is correct |
38 |
Correct |
830 ms |
54648 KB |
Output is correct |
39 |
Correct |
1125 ms |
54620 KB |
Output is correct |
40 |
Correct |
738 ms |
52300 KB |
Output is correct |
41 |
Correct |
743 ms |
51376 KB |
Output is correct |
42 |
Correct |
650 ms |
49580 KB |
Output is correct |
43 |
Correct |
644 ms |
49112 KB |
Output is correct |
44 |
Correct |
125 ms |
28024 KB |
Output is correct |
45 |
Correct |
114 ms |
27924 KB |
Output is correct |
46 |
Correct |
117 ms |
27924 KB |
Output is correct |
47 |
Correct |
112 ms |
27168 KB |
Output is correct |
48 |
Correct |
106 ms |
27160 KB |
Output is correct |
49 |
Correct |
108 ms |
27128 KB |
Output is correct |
50 |
Correct |
94 ms |
26900 KB |
Output is correct |
51 |
Correct |
94 ms |
26904 KB |
Output is correct |
52 |
Correct |
90 ms |
26780 KB |
Output is correct |
53 |
Correct |
95 ms |
29356 KB |
Output is correct |
54 |
Correct |
117 ms |
28348 KB |
Output is correct |
55 |
Correct |
119 ms |
28180 KB |
Output is correct |
56 |
Correct |
113 ms |
28112 KB |
Output is correct |
57 |
Correct |
113 ms |
28188 KB |
Output is correct |
58 |
Correct |
112 ms |
28188 KB |
Output is correct |
59 |
Correct |
112 ms |
28364 KB |
Output is correct |
60 |
Correct |
948 ms |
53168 KB |
Output is correct |
61 |
Correct |
920 ms |
53592 KB |
Output is correct |
62 |
Correct |
919 ms |
52084 KB |
Output is correct |
63 |
Correct |
767 ms |
44976 KB |
Output is correct |
64 |
Correct |
795 ms |
45232 KB |
Output is correct |
65 |
Correct |
767 ms |
44880 KB |
Output is correct |
66 |
Correct |
673 ms |
44268 KB |
Output is correct |
67 |
Correct |
687 ms |
44720 KB |
Output is correct |
68 |
Correct |
650 ms |
48432 KB |
Output is correct |
69 |
Correct |
698 ms |
58564 KB |
Output is correct |
70 |
Correct |
884 ms |
52776 KB |
Output is correct |
71 |
Correct |
842 ms |
52656 KB |
Output is correct |
72 |
Correct |
854 ms |
52124 KB |
Output is correct |
73 |
Correct |
871 ms |
51872 KB |
Output is correct |
74 |
Correct |
834 ms |
52144 KB |
Output is correct |
75 |
Correct |
891 ms |
52452 KB |
Output is correct |