#include <bits/stdc++.h>
#include "sequence.h"
using namespace std;
const int N = 5e5 + 1;
typedef long long ll;
int n;
struct segtree{
int mn[N * 4],mx[N * 4],add[N * 4];
void build(int v = 1,int tl = 1,int tr = n){
add[v] = mn[v] = mx[v] = 0;
if(tl == tr) return;
int tm = (tl + tr) >> 1;
build(v + v,tl,tm);build(v + v + 1,tm + 1,tr);
}
void inc(int v,int val){
add[v] += val;
mn[v] += val;
mx[v] += val;
}
void push(int v){
if(add[v]){
inc(v + v,add[v]);
inc(v + v +1,add[v]);
add[v] = 0;
}
}
void upd(int l,int r,int val,int v = 1,int tl = 1,int tr = n){
if(l > r || tl > r || l > tr){
return;
}
if(tl >= l && tr <= r){
inc(v,val);
}else{
push(v);
int tm = (tl + tr) >> 1;
upd(l,r,val,v+v,tl,tm);
upd(l,r,val,v+v+1,tm+1,tr);
mn[v] = min(mn[v + v],mn[v + v + 1]);
mx[v] = max(mx[v + v],mx[v + v + 1]);
}
}
int get_min(int l,int r,int v = 1,int tl = 1,int tr = n){
if(l > r || tl > r || l > tr) return 1e9;
if(tl >= l && tr <= r) return mn[v];
push(v);
int tm = (tl + tr) >> 1;
mn[v] = min(mn[v + v],mn[v + v + 1]);
mx[v] = max(mx[v + v],mx[v + v + 1]);
return min(get_min(l,r,v+v,tl,tm),get_min(l,r,v+v+1,tm+1,tr));
}
int get_max(int l,int r,int v = 1,int tl = 1,int tr = n){
if(l > r || tl > r || l > tr) return -1e9;
if(tl >= l && tr <= r) return mx[v];
push(v);
int tm = (tl + tr) >> 1;
mn[v] = min(mn[v + v],mn[v + v + 1]);
mx[v] = max(mx[v + v],mx[v + v + 1]);
return max(get_max(l,r,v+v,tl,tm),get_max(l,r,v+v+1,tm+1,tr));
}
}T,T1;
vector<int> pos[N];
bool inter(int l,int r,int l1,int r1){
return (min(r,r1) - max(l,l1) >= 0);
}
int sequence(int nn, std::vector<int> a) {
n = nn;
int mn = 1e9;
for(int i = 0;i < nn;i++){
mn = min(mn,a[i]);
pos[a[i]].push_back(i + 1);
}
T.build();
T1.build();
for(int i = 1;i <= n;i++){
T.upd(i,n,1);
T1.upd(i,n,1);
}
int res = 1;
for(int i = 1;i <= n;i++){
for(int j:pos[i - 1]){
T.upd(j,n,-2);
}
for(int j:pos[i]){
T1.upd(j,n,-2);
}
int it = 0;
for(int j = 0;j < (int)pos[i].size();j++){
while(it < (int)pos[i].size()){
ll mn_sum = T1.get_min(pos[i][it],n) - max(0,T1.get_max(1,pos[i][j] - 1));
ll mx_sum = T.get_max(pos[i][it],n) - min(0,T.get_min(1,pos[i][j] - 1));
int len = (it - j + 1);
assert(mx_sum >= mn_sum);
if(mn_sum * mx_sum <=0 ){
it++;
}else{
break;
}
}
res = max(res,it - j);
}
}
return res;
}
// int main() {
// int N;
// assert(1 == scanf("%d", &N));
// std::vector<int> A(N);
// for (int i = 0; i < N; ++i) {
// assert(1 == scanf("%d", &A[i]));
// }
// int result = sequence(N, A);
// printf("%d\n", result);
// return 0;
// }
Compilation message
sequence.cpp: In function 'int sequence(int, std::vector<int>)':
sequence.cpp:92:21: warning: unused variable 'len' [-Wunused-variable]
92 | int len = (it - j + 1);
| ^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
23132 KB |
Output is correct |
2 |
Correct |
5 ms |
23128 KB |
Output is correct |
3 |
Correct |
5 ms |
23132 KB |
Output is correct |
4 |
Correct |
5 ms |
23132 KB |
Output is correct |
5 |
Correct |
6 ms |
23132 KB |
Output is correct |
6 |
Correct |
6 ms |
23128 KB |
Output is correct |
7 |
Correct |
5 ms |
23128 KB |
Output is correct |
8 |
Correct |
7 ms |
23128 KB |
Output is correct |
9 |
Correct |
6 ms |
23132 KB |
Output is correct |
10 |
Correct |
5 ms |
23132 KB |
Output is correct |
11 |
Correct |
6 ms |
23132 KB |
Output is correct |
12 |
Correct |
5 ms |
23132 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
23132 KB |
Output is correct |
2 |
Correct |
5 ms |
23128 KB |
Output is correct |
3 |
Correct |
5 ms |
23132 KB |
Output is correct |
4 |
Correct |
5 ms |
23132 KB |
Output is correct |
5 |
Correct |
6 ms |
23132 KB |
Output is correct |
6 |
Correct |
6 ms |
23128 KB |
Output is correct |
7 |
Correct |
5 ms |
23128 KB |
Output is correct |
8 |
Correct |
7 ms |
23128 KB |
Output is correct |
9 |
Correct |
6 ms |
23132 KB |
Output is correct |
10 |
Correct |
5 ms |
23132 KB |
Output is correct |
11 |
Correct |
6 ms |
23132 KB |
Output is correct |
12 |
Correct |
5 ms |
23132 KB |
Output is correct |
13 |
Correct |
8 ms |
23128 KB |
Output is correct |
14 |
Correct |
9 ms |
23164 KB |
Output is correct |
15 |
Correct |
8 ms |
23132 KB |
Output is correct |
16 |
Correct |
8 ms |
23220 KB |
Output is correct |
17 |
Correct |
9 ms |
23128 KB |
Output is correct |
18 |
Correct |
8 ms |
23128 KB |
Output is correct |
19 |
Correct |
7 ms |
23132 KB |
Output is correct |
20 |
Correct |
7 ms |
23132 KB |
Output is correct |
21 |
Correct |
8 ms |
23132 KB |
Output is correct |
22 |
Correct |
7 ms |
23132 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
23132 KB |
Output is correct |
2 |
Correct |
752 ms |
63568 KB |
Output is correct |
3 |
Correct |
809 ms |
63428 KB |
Output is correct |
4 |
Correct |
753 ms |
54576 KB |
Output is correct |
5 |
Correct |
807 ms |
62492 KB |
Output is correct |
6 |
Correct |
778 ms |
62480 KB |
Output is correct |
7 |
Correct |
689 ms |
55040 KB |
Output is correct |
8 |
Correct |
753 ms |
55356 KB |
Output is correct |
9 |
Correct |
733 ms |
54584 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
23128 KB |
Output is correct |
2 |
Correct |
834 ms |
54712 KB |
Output is correct |
3 |
Correct |
854 ms |
54584 KB |
Output is correct |
4 |
Correct |
907 ms |
54576 KB |
Output is correct |
5 |
Correct |
765 ms |
54712 KB |
Output is correct |
6 |
Correct |
884 ms |
54668 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1133 ms |
69160 KB |
Output is correct |
2 |
Correct |
1113 ms |
69156 KB |
Output is correct |
3 |
Correct |
1064 ms |
68584 KB |
Output is correct |
4 |
Correct |
1168 ms |
68436 KB |
Output is correct |
5 |
Correct |
1083 ms |
65248 KB |
Output is correct |
6 |
Correct |
1232 ms |
65388 KB |
Output is correct |
7 |
Correct |
938 ms |
64044 KB |
Output is correct |
8 |
Correct |
958 ms |
63736 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
23132 KB |
Output is correct |
2 |
Correct |
5 ms |
23128 KB |
Output is correct |
3 |
Correct |
5 ms |
23132 KB |
Output is correct |
4 |
Correct |
5 ms |
23132 KB |
Output is correct |
5 |
Correct |
6 ms |
23132 KB |
Output is correct |
6 |
Correct |
6 ms |
23128 KB |
Output is correct |
7 |
Correct |
5 ms |
23128 KB |
Output is correct |
8 |
Correct |
7 ms |
23128 KB |
Output is correct |
9 |
Correct |
6 ms |
23132 KB |
Output is correct |
10 |
Correct |
5 ms |
23132 KB |
Output is correct |
11 |
Correct |
6 ms |
23132 KB |
Output is correct |
12 |
Correct |
5 ms |
23132 KB |
Output is correct |
13 |
Correct |
8 ms |
23128 KB |
Output is correct |
14 |
Correct |
9 ms |
23164 KB |
Output is correct |
15 |
Correct |
8 ms |
23132 KB |
Output is correct |
16 |
Correct |
8 ms |
23220 KB |
Output is correct |
17 |
Correct |
9 ms |
23128 KB |
Output is correct |
18 |
Correct |
8 ms |
23128 KB |
Output is correct |
19 |
Correct |
7 ms |
23132 KB |
Output is correct |
20 |
Correct |
7 ms |
23132 KB |
Output is correct |
21 |
Correct |
8 ms |
23132 KB |
Output is correct |
22 |
Correct |
7 ms |
23132 KB |
Output is correct |
23 |
Correct |
170 ms |
31836 KB |
Output is correct |
24 |
Correct |
182 ms |
32008 KB |
Output is correct |
25 |
Correct |
159 ms |
32012 KB |
Output is correct |
26 |
Correct |
165 ms |
30920 KB |
Output is correct |
27 |
Correct |
163 ms |
31068 KB |
Output is correct |
28 |
Correct |
162 ms |
30808 KB |
Output is correct |
29 |
Correct |
132 ms |
30300 KB |
Output is correct |
30 |
Correct |
133 ms |
30596 KB |
Output is correct |
31 |
Correct |
83 ms |
30672 KB |
Output is correct |
32 |
Correct |
105 ms |
32812 KB |
Output is correct |
33 |
Correct |
144 ms |
31832 KB |
Output is correct |
34 |
Correct |
161 ms |
31836 KB |
Output is correct |
35 |
Correct |
142 ms |
31576 KB |
Output is correct |
36 |
Correct |
155 ms |
31576 KB |
Output is correct |
37 |
Correct |
149 ms |
31824 KB |
Output is correct |
38 |
Correct |
152 ms |
31836 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
23132 KB |
Output is correct |
2 |
Correct |
5 ms |
23128 KB |
Output is correct |
3 |
Correct |
5 ms |
23132 KB |
Output is correct |
4 |
Correct |
5 ms |
23132 KB |
Output is correct |
5 |
Correct |
6 ms |
23132 KB |
Output is correct |
6 |
Correct |
6 ms |
23128 KB |
Output is correct |
7 |
Correct |
5 ms |
23128 KB |
Output is correct |
8 |
Correct |
7 ms |
23128 KB |
Output is correct |
9 |
Correct |
6 ms |
23132 KB |
Output is correct |
10 |
Correct |
5 ms |
23132 KB |
Output is correct |
11 |
Correct |
6 ms |
23132 KB |
Output is correct |
12 |
Correct |
5 ms |
23132 KB |
Output is correct |
13 |
Correct |
8 ms |
23128 KB |
Output is correct |
14 |
Correct |
9 ms |
23164 KB |
Output is correct |
15 |
Correct |
8 ms |
23132 KB |
Output is correct |
16 |
Correct |
8 ms |
23220 KB |
Output is correct |
17 |
Correct |
9 ms |
23128 KB |
Output is correct |
18 |
Correct |
8 ms |
23128 KB |
Output is correct |
19 |
Correct |
7 ms |
23132 KB |
Output is correct |
20 |
Correct |
7 ms |
23132 KB |
Output is correct |
21 |
Correct |
8 ms |
23132 KB |
Output is correct |
22 |
Correct |
7 ms |
23132 KB |
Output is correct |
23 |
Correct |
752 ms |
63568 KB |
Output is correct |
24 |
Correct |
809 ms |
63428 KB |
Output is correct |
25 |
Correct |
753 ms |
54576 KB |
Output is correct |
26 |
Correct |
807 ms |
62492 KB |
Output is correct |
27 |
Correct |
778 ms |
62480 KB |
Output is correct |
28 |
Correct |
689 ms |
55040 KB |
Output is correct |
29 |
Correct |
753 ms |
55356 KB |
Output is correct |
30 |
Correct |
733 ms |
54584 KB |
Output is correct |
31 |
Correct |
834 ms |
54712 KB |
Output is correct |
32 |
Correct |
854 ms |
54584 KB |
Output is correct |
33 |
Correct |
907 ms |
54576 KB |
Output is correct |
34 |
Correct |
765 ms |
54712 KB |
Output is correct |
35 |
Correct |
884 ms |
54668 KB |
Output is correct |
36 |
Correct |
1133 ms |
69160 KB |
Output is correct |
37 |
Correct |
1113 ms |
69156 KB |
Output is correct |
38 |
Correct |
1064 ms |
68584 KB |
Output is correct |
39 |
Correct |
1168 ms |
68436 KB |
Output is correct |
40 |
Correct |
1083 ms |
65248 KB |
Output is correct |
41 |
Correct |
1232 ms |
65388 KB |
Output is correct |
42 |
Correct |
938 ms |
64044 KB |
Output is correct |
43 |
Correct |
958 ms |
63736 KB |
Output is correct |
44 |
Correct |
170 ms |
31836 KB |
Output is correct |
45 |
Correct |
182 ms |
32008 KB |
Output is correct |
46 |
Correct |
159 ms |
32012 KB |
Output is correct |
47 |
Correct |
165 ms |
30920 KB |
Output is correct |
48 |
Correct |
163 ms |
31068 KB |
Output is correct |
49 |
Correct |
162 ms |
30808 KB |
Output is correct |
50 |
Correct |
132 ms |
30300 KB |
Output is correct |
51 |
Correct |
133 ms |
30596 KB |
Output is correct |
52 |
Correct |
83 ms |
30672 KB |
Output is correct |
53 |
Correct |
105 ms |
32812 KB |
Output is correct |
54 |
Correct |
144 ms |
31832 KB |
Output is correct |
55 |
Correct |
161 ms |
31836 KB |
Output is correct |
56 |
Correct |
142 ms |
31576 KB |
Output is correct |
57 |
Correct |
155 ms |
31576 KB |
Output is correct |
58 |
Correct |
149 ms |
31824 KB |
Output is correct |
59 |
Correct |
152 ms |
31836 KB |
Output is correct |
60 |
Correct |
1327 ms |
63424 KB |
Output is correct |
61 |
Correct |
1310 ms |
63416 KB |
Output is correct |
62 |
Correct |
1266 ms |
63436 KB |
Output is correct |
63 |
Correct |
1244 ms |
56204 KB |
Output is correct |
64 |
Correct |
1265 ms |
56180 KB |
Output is correct |
65 |
Correct |
1343 ms |
56172 KB |
Output is correct |
66 |
Correct |
948 ms |
54724 KB |
Output is correct |
67 |
Correct |
903 ms |
54840 KB |
Output is correct |
68 |
Correct |
546 ms |
55848 KB |
Output is correct |
69 |
Correct |
687 ms |
68988 KB |
Output is correct |
70 |
Correct |
1208 ms |
62504 KB |
Output is correct |
71 |
Correct |
1210 ms |
62320 KB |
Output is correct |
72 |
Correct |
1158 ms |
61872 KB |
Output is correct |
73 |
Correct |
1186 ms |
62196 KB |
Output is correct |
74 |
Correct |
1194 ms |
62052 KB |
Output is correct |
75 |
Correct |
1179 ms |
62036 KB |
Output is correct |