#include "sequence.h"
#include<bits/stdc++.h>
#define pb push_back
#define fi first
#define se second
#define mp make_pair
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
int readint(){
int x=0,f=1; char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
int mn[4000005],mx[4000005],lzy[4000005],prefmn[500005],prefmx[500005],suffmn[500005],suffmx[500005];
vector<int> pos[500005];
void push(int id){
if(lzy[id]==0) return;
mn[id<<1]+=lzy[id];
mx[id<<1]+=lzy[id];
mn[id<<1|1]+=lzy[id];
mx[id<<1|1]+=lzy[id];
lzy[id<<1]+=lzy[id];
lzy[id<<1|1]+=lzy[id];
lzy[id]=0;
}
void build(int id,int l,int r){
if(l==r){
mn[id]=l;
mx[id]=l;
return;
}
int mid=(l+r)/2;
build(id<<1,l,mid);
build(id<<1|1,mid+1,r);
mn[id]=min(mn[id<<1],mn[id<<1|1]);
mx[id]=max(mx[id<<1],mx[id<<1|1]);
}
void change(int id,int l,int r,int ql,int qr,int v){
if(ql<=l&&r<=qr){
lzy[id]+=v;
mn[id]+=v;
mx[id]+=v;
push(id);
return;
}
push(id);
int mid=(l+r)/2;
if(qr<=mid) change(id<<1,l,mid,ql,qr,v);
else if(ql>mid) change(id<<1|1,mid+1,r,ql,qr,v);
else change(id<<1,l,mid,ql,qr,v),change(id<<1|1,mid+1,r,ql,qr,v);
mn[id]=min(mn[id<<1],mn[id<<1|1]);
mx[id]=max(mx[id<<1],mx[id<<1|1]);
}
pii query(int id,int l,int r,int ql,int qr){
if(l>r||l>qr||r<ql||ql>qr) return mp((int)1e9,(int)-1e9);
if(ql<=l&&r<=qr) return mp(mn[id],mx[id]);
push(id);
int mid=(l+r)/2;
pii L=query(id<<1,l,mid,ql,qr);
pii R=query(id<<1|1,mid+1,r,ql,qr);
mn[id]=min(mn[id<<1],mn[id<<1|1]);
mx[id]=max(mx[id<<1],mx[id<<1|1]);
return mp(min(L.fi,R.fi),max(L.se,R.se));
}
int sgn(int x){return (x==0?0:(x<0?-1:1));}
int sequence(int n,vector<int> a){
for(int i=1;i<=n;i++) pos[a[i-1]].pb(i);
build(1,0,n);
int ans=1;
for(int v=1;v<=n;v++){
for(int i:pos[v]) prefmn[i]=query(1,0,n,0,i-1).fi,suffmx[i]=query(1,0,n,i,n).se;
for(int i:pos[v]) change(1,0,n,i,n,-2);
for(int i:pos[v]) prefmx[i]=query(1,0,n,0,i-1).se,suffmn[i]=query(1,0,n,i,n).fi;
int ptr=0;
for(int i=0;i<pos[v].size();i++){
while(ptr+1<pos[v].size()&&sgn(suffmx[pos[v][ptr+1]]-prefmn[pos[v][i]])!=sgn(suffmn[pos[v][ptr+1]]-prefmx[pos[v][i]])) ptr++;
ans=max(ans,ptr-i+1);
}
}
return ans;
}
Compilation message
sequence.cpp: In function 'int sequence(int, std::vector<int>)':
sequence.cpp:88:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
88 | for(int i=0;i<pos[v].size();i++){
| ~^~~~~~~~~~~~~~
sequence.cpp:89:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
89 | while(ptr+1<pos[v].size()&&sgn(suffmx[pos[v][ptr+1]]-prefmn[pos[v][i]])!=sgn(suffmn[pos[v][ptr+1]]-prefmx[pos[v][i]])) ptr++;
| ~~~~~^~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
23132 KB |
Output is correct |
2 |
Correct |
4 ms |
23164 KB |
Output is correct |
3 |
Correct |
4 ms |
23132 KB |
Output is correct |
4 |
Correct |
4 ms |
23128 KB |
Output is correct |
5 |
Correct |
4 ms |
23132 KB |
Output is correct |
6 |
Correct |
4 ms |
23132 KB |
Output is correct |
7 |
Correct |
5 ms |
23132 KB |
Output is correct |
8 |
Correct |
4 ms |
23132 KB |
Output is correct |
9 |
Correct |
4 ms |
23132 KB |
Output is correct |
10 |
Correct |
4 ms |
23128 KB |
Output is correct |
11 |
Correct |
4 ms |
23132 KB |
Output is correct |
12 |
Correct |
4 ms |
23132 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
23132 KB |
Output is correct |
2 |
Correct |
4 ms |
23164 KB |
Output is correct |
3 |
Correct |
4 ms |
23132 KB |
Output is correct |
4 |
Correct |
4 ms |
23128 KB |
Output is correct |
5 |
Correct |
4 ms |
23132 KB |
Output is correct |
6 |
Correct |
4 ms |
23132 KB |
Output is correct |
7 |
Correct |
5 ms |
23132 KB |
Output is correct |
8 |
Correct |
4 ms |
23132 KB |
Output is correct |
9 |
Correct |
4 ms |
23132 KB |
Output is correct |
10 |
Correct |
4 ms |
23128 KB |
Output is correct |
11 |
Correct |
4 ms |
23132 KB |
Output is correct |
12 |
Correct |
4 ms |
23132 KB |
Output is correct |
13 |
Correct |
6 ms |
23132 KB |
Output is correct |
14 |
Correct |
6 ms |
23132 KB |
Output is correct |
15 |
Correct |
6 ms |
23128 KB |
Output is correct |
16 |
Correct |
5 ms |
23132 KB |
Output is correct |
17 |
Correct |
5 ms |
23132 KB |
Output is correct |
18 |
Correct |
6 ms |
23132 KB |
Output is correct |
19 |
Correct |
6 ms |
23176 KB |
Output is correct |
20 |
Correct |
6 ms |
23132 KB |
Output is correct |
21 |
Correct |
6 ms |
23132 KB |
Output is correct |
22 |
Correct |
6 ms |
23128 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
23132 KB |
Output is correct |
2 |
Correct |
548 ms |
63512 KB |
Output is correct |
3 |
Correct |
570 ms |
67288 KB |
Output is correct |
4 |
Correct |
516 ms |
57004 KB |
Output is correct |
5 |
Correct |
558 ms |
66040 KB |
Output is correct |
6 |
Correct |
574 ms |
65872 KB |
Output is correct |
7 |
Correct |
534 ms |
57708 KB |
Output is correct |
8 |
Correct |
539 ms |
57840 KB |
Output is correct |
9 |
Correct |
506 ms |
56852 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
23164 KB |
Output is correct |
2 |
Correct |
542 ms |
55744 KB |
Output is correct |
3 |
Correct |
533 ms |
56856 KB |
Output is correct |
4 |
Correct |
550 ms |
56868 KB |
Output is correct |
5 |
Correct |
549 ms |
56988 KB |
Output is correct |
6 |
Correct |
528 ms |
56936 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
761 ms |
69248 KB |
Output is correct |
2 |
Correct |
762 ms |
72700 KB |
Output is correct |
3 |
Correct |
782 ms |
72124 KB |
Output is correct |
4 |
Correct |
752 ms |
72068 KB |
Output is correct |
5 |
Correct |
652 ms |
68724 KB |
Output is correct |
6 |
Correct |
616 ms |
68692 KB |
Output is correct |
7 |
Correct |
515 ms |
67476 KB |
Output is correct |
8 |
Correct |
530 ms |
67080 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
23132 KB |
Output is correct |
2 |
Correct |
4 ms |
23164 KB |
Output is correct |
3 |
Correct |
4 ms |
23132 KB |
Output is correct |
4 |
Correct |
4 ms |
23128 KB |
Output is correct |
5 |
Correct |
4 ms |
23132 KB |
Output is correct |
6 |
Correct |
4 ms |
23132 KB |
Output is correct |
7 |
Correct |
5 ms |
23132 KB |
Output is correct |
8 |
Correct |
4 ms |
23132 KB |
Output is correct |
9 |
Correct |
4 ms |
23132 KB |
Output is correct |
10 |
Correct |
4 ms |
23128 KB |
Output is correct |
11 |
Correct |
4 ms |
23132 KB |
Output is correct |
12 |
Correct |
4 ms |
23132 KB |
Output is correct |
13 |
Correct |
6 ms |
23132 KB |
Output is correct |
14 |
Correct |
6 ms |
23132 KB |
Output is correct |
15 |
Correct |
6 ms |
23128 KB |
Output is correct |
16 |
Correct |
5 ms |
23132 KB |
Output is correct |
17 |
Correct |
5 ms |
23132 KB |
Output is correct |
18 |
Correct |
6 ms |
23132 KB |
Output is correct |
19 |
Correct |
6 ms |
23176 KB |
Output is correct |
20 |
Correct |
6 ms |
23132 KB |
Output is correct |
21 |
Correct |
6 ms |
23132 KB |
Output is correct |
22 |
Correct |
6 ms |
23128 KB |
Output is correct |
23 |
Correct |
107 ms |
32676 KB |
Output is correct |
24 |
Correct |
110 ms |
32672 KB |
Output is correct |
25 |
Correct |
104 ms |
32604 KB |
Output is correct |
26 |
Correct |
104 ms |
31580 KB |
Output is correct |
27 |
Correct |
105 ms |
31768 KB |
Output is correct |
28 |
Correct |
100 ms |
31580 KB |
Output is correct |
29 |
Correct |
79 ms |
31492 KB |
Output is correct |
30 |
Correct |
87 ms |
31548 KB |
Output is correct |
31 |
Correct |
80 ms |
31432 KB |
Output is correct |
32 |
Correct |
78 ms |
33364 KB |
Output is correct |
33 |
Correct |
101 ms |
32336 KB |
Output is correct |
34 |
Correct |
104 ms |
32452 KB |
Output is correct |
35 |
Correct |
105 ms |
32340 KB |
Output is correct |
36 |
Correct |
108 ms |
32436 KB |
Output is correct |
37 |
Correct |
103 ms |
32348 KB |
Output is correct |
38 |
Correct |
113 ms |
32504 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
23132 KB |
Output is correct |
2 |
Correct |
4 ms |
23164 KB |
Output is correct |
3 |
Correct |
4 ms |
23132 KB |
Output is correct |
4 |
Correct |
4 ms |
23128 KB |
Output is correct |
5 |
Correct |
4 ms |
23132 KB |
Output is correct |
6 |
Correct |
4 ms |
23132 KB |
Output is correct |
7 |
Correct |
5 ms |
23132 KB |
Output is correct |
8 |
Correct |
4 ms |
23132 KB |
Output is correct |
9 |
Correct |
4 ms |
23132 KB |
Output is correct |
10 |
Correct |
4 ms |
23128 KB |
Output is correct |
11 |
Correct |
4 ms |
23132 KB |
Output is correct |
12 |
Correct |
4 ms |
23132 KB |
Output is correct |
13 |
Correct |
6 ms |
23132 KB |
Output is correct |
14 |
Correct |
6 ms |
23132 KB |
Output is correct |
15 |
Correct |
6 ms |
23128 KB |
Output is correct |
16 |
Correct |
5 ms |
23132 KB |
Output is correct |
17 |
Correct |
5 ms |
23132 KB |
Output is correct |
18 |
Correct |
6 ms |
23132 KB |
Output is correct |
19 |
Correct |
6 ms |
23176 KB |
Output is correct |
20 |
Correct |
6 ms |
23132 KB |
Output is correct |
21 |
Correct |
6 ms |
23132 KB |
Output is correct |
22 |
Correct |
6 ms |
23128 KB |
Output is correct |
23 |
Correct |
548 ms |
63512 KB |
Output is correct |
24 |
Correct |
570 ms |
67288 KB |
Output is correct |
25 |
Correct |
516 ms |
57004 KB |
Output is correct |
26 |
Correct |
558 ms |
66040 KB |
Output is correct |
27 |
Correct |
574 ms |
65872 KB |
Output is correct |
28 |
Correct |
534 ms |
57708 KB |
Output is correct |
29 |
Correct |
539 ms |
57840 KB |
Output is correct |
30 |
Correct |
506 ms |
56852 KB |
Output is correct |
31 |
Correct |
542 ms |
55744 KB |
Output is correct |
32 |
Correct |
533 ms |
56856 KB |
Output is correct |
33 |
Correct |
550 ms |
56868 KB |
Output is correct |
34 |
Correct |
549 ms |
56988 KB |
Output is correct |
35 |
Correct |
528 ms |
56936 KB |
Output is correct |
36 |
Correct |
761 ms |
69248 KB |
Output is correct |
37 |
Correct |
762 ms |
72700 KB |
Output is correct |
38 |
Correct |
782 ms |
72124 KB |
Output is correct |
39 |
Correct |
752 ms |
72068 KB |
Output is correct |
40 |
Correct |
652 ms |
68724 KB |
Output is correct |
41 |
Correct |
616 ms |
68692 KB |
Output is correct |
42 |
Correct |
515 ms |
67476 KB |
Output is correct |
43 |
Correct |
530 ms |
67080 KB |
Output is correct |
44 |
Correct |
107 ms |
32676 KB |
Output is correct |
45 |
Correct |
110 ms |
32672 KB |
Output is correct |
46 |
Correct |
104 ms |
32604 KB |
Output is correct |
47 |
Correct |
104 ms |
31580 KB |
Output is correct |
48 |
Correct |
105 ms |
31768 KB |
Output is correct |
49 |
Correct |
100 ms |
31580 KB |
Output is correct |
50 |
Correct |
79 ms |
31492 KB |
Output is correct |
51 |
Correct |
87 ms |
31548 KB |
Output is correct |
52 |
Correct |
80 ms |
31432 KB |
Output is correct |
53 |
Correct |
78 ms |
33364 KB |
Output is correct |
54 |
Correct |
101 ms |
32336 KB |
Output is correct |
55 |
Correct |
104 ms |
32452 KB |
Output is correct |
56 |
Correct |
105 ms |
32340 KB |
Output is correct |
57 |
Correct |
108 ms |
32436 KB |
Output is correct |
58 |
Correct |
103 ms |
32348 KB |
Output is correct |
59 |
Correct |
113 ms |
32504 KB |
Output is correct |
60 |
Correct |
964 ms |
67008 KB |
Output is correct |
61 |
Correct |
961 ms |
67004 KB |
Output is correct |
62 |
Correct |
935 ms |
67020 KB |
Output is correct |
63 |
Correct |
853 ms |
58856 KB |
Output is correct |
64 |
Correct |
877 ms |
58848 KB |
Output is correct |
65 |
Correct |
809 ms |
58704 KB |
Output is correct |
66 |
Correct |
541 ms |
56988 KB |
Output is correct |
67 |
Correct |
541 ms |
57180 KB |
Output is correct |
68 |
Correct |
516 ms |
59072 KB |
Output is correct |
69 |
Correct |
531 ms |
72676 KB |
Output is correct |
70 |
Correct |
881 ms |
66100 KB |
Output is correct |
71 |
Correct |
869 ms |
65984 KB |
Output is correct |
72 |
Correct |
880 ms |
65480 KB |
Output is correct |
73 |
Correct |
877 ms |
65760 KB |
Output is correct |
74 |
Correct |
877 ms |
65416 KB |
Output is correct |
75 |
Correct |
860 ms |
65796 KB |
Output is correct |