#include<bits/stdc++.h>
#include "sequence.h"
using namespace std;
#define all(fl) fl.begin(),fl.end()
#define pb push_back
#define fi first
#define se second
#define for1(i,j,k) for(int i=j;i<=k;i++)
#define for2(i,j,k) for(int i=j;i>=k;i--)
#define for3(i,j,k,l) for(int i=j;i<=k;i+=l)
#define lb lower_bound
#define ub upper_bound
#define sz(a) (int)a.size()
#define pii pair<int,int>
#define pli pair<long long,int>
#define gcd __gcd
#define lcm(x,y) x*y/__gcd(x,y)
const int maxn=5e5+9;
struct IT_max{
int st[maxn*4],lazy[maxn*4];
void resz(int n){
for1(i,1,4*n)st[i]=lazy[i]=0;
}
void push(int id){
if (lazy[id]){
st[id<<1]+=lazy[id];
st[id<<1|1]+=lazy[id];
lazy[id<<1]+=lazy[id];
lazy[id<<1|1]+=lazy[id];
lazy[id]=0;
}
}
void update(int id,int l,int r,int u,int v,int val){
if (l>v||r<u||u>v)return;
if (u<=l&&r<=v){
st[id]+=val;
lazy[id]+=val;
return;
}
push(id);
int mid=(l+r)>>1;
update(id<<1,l,mid,u,v,val);
update(id<<1|1,mid+1,r,u,v,val);
st[id]=max(st[id<<1],st[id<<1|1]);
}
int get(int id,int l,int r,int u,int v){
if (l>v||r<u||u>v)return -maxn;
if (u<=l&&r<=v)return st[id];
push(id);
int mid=(l+r)>>1;
return max(get(id<<1,l,mid,u,v),get(id<<1|1,mid+1,r,u,v));
}
};
struct IT_min{
int st[maxn*4],lazy[maxn*4];
void resz(int n){
for1(i,1,4*n)st[i]=lazy[i]=0;
}
void push(int id){
if (lazy[id]){
st[id<<1]+=lazy[id];
st[id<<1|1]+=lazy[id];
lazy[id<<1]+=lazy[id];
lazy[id<<1|1]+=lazy[id];
lazy[id]=0;
}
}
void update(int id,int l,int r,int u,int v,int val){
if (l>v||r<u||u>v)return;
if (u<=l&&r<=v){
st[id]+=val;
lazy[id]+=val;
return;
}
push(id);
int mid=(l+r)>>1;
update(id<<1,l,mid,u,v,val);
update(id<<1|1,mid+1,r,u,v,val);
st[id]=min(st[id<<1],st[id<<1|1]);
}
int get(int id,int l,int r,int u,int v){
if (l>v||r<u||u>v)return maxn;
if (u<=l&&r<=v)return st[id];
push(id);
int mid=(l+r)>>1;
return min(get(id<<1,l,mid,u,v),get(id<<1|1,mid+1,r,u,v));
}
};
struct IT{
int st[maxn*4];
void update(int id,int l,int r,int u,int val){
if(l>u||r<u)return;
if (l==r){
st[id]=val;
return;
}
int mid=(l+r)>>1;
if (mid>=u)update(id<<1,l,mid,u,val);
else update(id<<1|1,mid+1,r,u,val);
st[id]=max(st[id<<1],st[id<<1|1]);
}
int walk(int id,int l,int r,int val){
if (st[id]<val)return -1;
if (l==r)return l;
int mid=(l+r)>>1;
int ans=walk(id<<1,l,mid,val);
if (ans==-1)return walk(id<<1|1,mid+1,r,val);
return ans;
}
};
int n;
int a[maxn];
vector<int>value[maxn];
int mx_l[maxn],mn_l[maxn],mn_r[maxn],mx_r[maxn];
int pf[maxn];
void pre(){
IT_max st1;
IT_min st2;
st1.resz(n),st2.resz(n);
auto up=[&](int id,int val){
st1.update(1,1,n,id,n,val);
st2.update(1,1,n,id,n,val);
};
for1(i,1,n)up(i,-1);
for1(i,1,n){
for (auto v:value[i]){
up(v,2);
}
for (auto v:value[i]){
pf[v]=st1.get(1,1,n,v,v);
}
for1(j,0,sz(value[i])-1){
int v=value[i][j];
if (j==sz(value[i])-1)mx_r[v]=st1.get(1,1,n,v,n)-pf[v],mn_r[v]=st2.get(1,1,n,v,n)-pf[v];
else mx_r[v]=st1.get(1,1,n,v,value[i][j+1]-1)-pf[v],mn_r[v]=st2.get(1,1,n,v,value[i][j+1]-1)-pf[v];
}
}
st1.resz(n),st2.resz(n);
auto rup=[&](int id,int val){
st1.update(1,1,n,1,id,val);
st2.update(1,1,n,1,id,val);
};
for1(i,1,n)rup(i,-1);
for1(i,1,n){
for (auto v:value[i])rup(v,2);
for1(j,0,sz(value[i])-1){
int v=value[i][j];
int suffix=st1.get(1,1,n,v,v);
if (j==0){
mx_l[v]=st1.get(1,1,n,1,v)-suffix,mn_l[v]=st2.get(1,1,n,1,v)-suffix;
}
else {
mx_l[v]=st1.get(1,1,n,value[i][j-1]+1,v)-suffix,mn_l[v]=st2.get(1,1,n,value[i][j-1]+1,v)-suffix;
}
}
}
}
struct type{
int id,val;
bool fl;
bool operator < (const type &p){
if (val==p.val)return fl<p.fl;
return val<p.val;
}
};
int sequence(int N, std::vector<int> A) {
n=N;
for1(i,1,n)a[i]=A[i-1];
for1(i,1,n)value[a[i]].pb(i);
int ans=0;
pre();
IT st;
for1(i,1,n){
vector<type>t;
for1(j,0,sz(value[i])-1){
int v=value[i][j];
t.pb({j+1,pf[v]-mx_l[v],0});
t.pb({j+1,pf[v]+1+mx_r[v],1});
}
sort(all(t));
int m=sz(value[i]);
for1(j,1,4*m)st.st[j]=-n*10;
for (auto v:t){
int id=value[i][v.id-1];
if (v.fl==0){
st.update(1,1,m,v.id,pf[id]-mn_l[id]-2*v.id);
}
else {
int pos=st.walk(1,1,m,pf[id]+1+mn_r[id]-2*v.id-2);
if (pos!=-1&&pos<=v.id){
ans=max(ans,v.id-pos+1);
}
}
}
}
return ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
27 ms |
62044 KB |
Output is correct |
2 |
Correct |
27 ms |
62044 KB |
Output is correct |
3 |
Correct |
27 ms |
62016 KB |
Output is correct |
4 |
Correct |
29 ms |
62036 KB |
Output is correct |
5 |
Correct |
28 ms |
62036 KB |
Output is correct |
6 |
Correct |
27 ms |
62044 KB |
Output is correct |
7 |
Correct |
27 ms |
62032 KB |
Output is correct |
8 |
Correct |
26 ms |
62044 KB |
Output is correct |
9 |
Correct |
28 ms |
62032 KB |
Output is correct |
10 |
Correct |
27 ms |
61892 KB |
Output is correct |
11 |
Correct |
28 ms |
62044 KB |
Output is correct |
12 |
Correct |
29 ms |
62036 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
27 ms |
62044 KB |
Output is correct |
2 |
Correct |
27 ms |
62044 KB |
Output is correct |
3 |
Correct |
27 ms |
62016 KB |
Output is correct |
4 |
Correct |
29 ms |
62036 KB |
Output is correct |
5 |
Correct |
28 ms |
62036 KB |
Output is correct |
6 |
Correct |
27 ms |
62044 KB |
Output is correct |
7 |
Correct |
27 ms |
62032 KB |
Output is correct |
8 |
Correct |
26 ms |
62044 KB |
Output is correct |
9 |
Correct |
28 ms |
62032 KB |
Output is correct |
10 |
Correct |
27 ms |
61892 KB |
Output is correct |
11 |
Correct |
28 ms |
62044 KB |
Output is correct |
12 |
Correct |
29 ms |
62036 KB |
Output is correct |
13 |
Correct |
33 ms |
62144 KB |
Output is correct |
14 |
Correct |
30 ms |
62044 KB |
Output is correct |
15 |
Correct |
31 ms |
61980 KB |
Output is correct |
16 |
Correct |
30 ms |
62032 KB |
Output is correct |
17 |
Correct |
33 ms |
62104 KB |
Output is correct |
18 |
Correct |
33 ms |
62036 KB |
Output is correct |
19 |
Correct |
30 ms |
62040 KB |
Output is correct |
20 |
Correct |
34 ms |
62036 KB |
Output is correct |
21 |
Correct |
31 ms |
62044 KB |
Output is correct |
22 |
Correct |
33 ms |
62032 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
27 ms |
62044 KB |
Output is correct |
2 |
Correct |
960 ms |
76896 KB |
Output is correct |
3 |
Correct |
1018 ms |
76716 KB |
Output is correct |
4 |
Correct |
917 ms |
74468 KB |
Output is correct |
5 |
Correct |
1012 ms |
76164 KB |
Output is correct |
6 |
Correct |
948 ms |
75772 KB |
Output is correct |
7 |
Correct |
919 ms |
69988 KB |
Output is correct |
8 |
Correct |
920 ms |
70112 KB |
Output is correct |
9 |
Correct |
948 ms |
79716 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
27 ms |
62044 KB |
Output is correct |
2 |
Correct |
1018 ms |
84044 KB |
Output is correct |
3 |
Correct |
1004 ms |
80996 KB |
Output is correct |
4 |
Correct |
1025 ms |
79896 KB |
Output is correct |
5 |
Correct |
1004 ms |
89144 KB |
Output is correct |
6 |
Correct |
963 ms |
80816 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1485 ms |
82812 KB |
Output is correct |
2 |
Correct |
1507 ms |
82608 KB |
Output is correct |
3 |
Correct |
1491 ms |
82260 KB |
Output is correct |
4 |
Correct |
1437 ms |
82040 KB |
Output is correct |
5 |
Correct |
1240 ms |
78700 KB |
Output is correct |
6 |
Correct |
1267 ms |
78796 KB |
Output is correct |
7 |
Correct |
996 ms |
77520 KB |
Output is correct |
8 |
Correct |
1028 ms |
77192 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
27 ms |
62044 KB |
Output is correct |
2 |
Correct |
27 ms |
62044 KB |
Output is correct |
3 |
Correct |
27 ms |
62016 KB |
Output is correct |
4 |
Correct |
29 ms |
62036 KB |
Output is correct |
5 |
Correct |
28 ms |
62036 KB |
Output is correct |
6 |
Correct |
27 ms |
62044 KB |
Output is correct |
7 |
Correct |
27 ms |
62032 KB |
Output is correct |
8 |
Correct |
26 ms |
62044 KB |
Output is correct |
9 |
Correct |
28 ms |
62032 KB |
Output is correct |
10 |
Correct |
27 ms |
61892 KB |
Output is correct |
11 |
Correct |
28 ms |
62044 KB |
Output is correct |
12 |
Correct |
29 ms |
62036 KB |
Output is correct |
13 |
Correct |
33 ms |
62144 KB |
Output is correct |
14 |
Correct |
30 ms |
62044 KB |
Output is correct |
15 |
Correct |
31 ms |
61980 KB |
Output is correct |
16 |
Correct |
30 ms |
62032 KB |
Output is correct |
17 |
Correct |
33 ms |
62104 KB |
Output is correct |
18 |
Correct |
33 ms |
62036 KB |
Output is correct |
19 |
Correct |
30 ms |
62040 KB |
Output is correct |
20 |
Correct |
34 ms |
62036 KB |
Output is correct |
21 |
Correct |
31 ms |
62044 KB |
Output is correct |
22 |
Correct |
33 ms |
62032 KB |
Output is correct |
23 |
Correct |
229 ms |
64340 KB |
Output is correct |
24 |
Correct |
227 ms |
64340 KB |
Output is correct |
25 |
Correct |
248 ms |
64524 KB |
Output is correct |
26 |
Correct |
221 ms |
63560 KB |
Output is correct |
27 |
Correct |
211 ms |
63596 KB |
Output is correct |
28 |
Correct |
215 ms |
63572 KB |
Output is correct |
29 |
Correct |
172 ms |
64032 KB |
Output is correct |
30 |
Correct |
171 ms |
64020 KB |
Output is correct |
31 |
Correct |
157 ms |
66628 KB |
Output is correct |
32 |
Correct |
164 ms |
65432 KB |
Output is correct |
33 |
Correct |
239 ms |
64792 KB |
Output is correct |
34 |
Correct |
244 ms |
64796 KB |
Output is correct |
35 |
Correct |
229 ms |
64720 KB |
Output is correct |
36 |
Correct |
243 ms |
64784 KB |
Output is correct |
37 |
Correct |
257 ms |
64756 KB |
Output is correct |
38 |
Correct |
224 ms |
64696 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
27 ms |
62044 KB |
Output is correct |
2 |
Correct |
27 ms |
62044 KB |
Output is correct |
3 |
Correct |
27 ms |
62016 KB |
Output is correct |
4 |
Correct |
29 ms |
62036 KB |
Output is correct |
5 |
Correct |
28 ms |
62036 KB |
Output is correct |
6 |
Correct |
27 ms |
62044 KB |
Output is correct |
7 |
Correct |
27 ms |
62032 KB |
Output is correct |
8 |
Correct |
26 ms |
62044 KB |
Output is correct |
9 |
Correct |
28 ms |
62032 KB |
Output is correct |
10 |
Correct |
27 ms |
61892 KB |
Output is correct |
11 |
Correct |
28 ms |
62044 KB |
Output is correct |
12 |
Correct |
29 ms |
62036 KB |
Output is correct |
13 |
Correct |
33 ms |
62144 KB |
Output is correct |
14 |
Correct |
30 ms |
62044 KB |
Output is correct |
15 |
Correct |
31 ms |
61980 KB |
Output is correct |
16 |
Correct |
30 ms |
62032 KB |
Output is correct |
17 |
Correct |
33 ms |
62104 KB |
Output is correct |
18 |
Correct |
33 ms |
62036 KB |
Output is correct |
19 |
Correct |
30 ms |
62040 KB |
Output is correct |
20 |
Correct |
34 ms |
62036 KB |
Output is correct |
21 |
Correct |
31 ms |
62044 KB |
Output is correct |
22 |
Correct |
33 ms |
62032 KB |
Output is correct |
23 |
Correct |
960 ms |
76896 KB |
Output is correct |
24 |
Correct |
1018 ms |
76716 KB |
Output is correct |
25 |
Correct |
917 ms |
74468 KB |
Output is correct |
26 |
Correct |
1012 ms |
76164 KB |
Output is correct |
27 |
Correct |
948 ms |
75772 KB |
Output is correct |
28 |
Correct |
919 ms |
69988 KB |
Output is correct |
29 |
Correct |
920 ms |
70112 KB |
Output is correct |
30 |
Correct |
948 ms |
79716 KB |
Output is correct |
31 |
Correct |
1018 ms |
84044 KB |
Output is correct |
32 |
Correct |
1004 ms |
80996 KB |
Output is correct |
33 |
Correct |
1025 ms |
79896 KB |
Output is correct |
34 |
Correct |
1004 ms |
89144 KB |
Output is correct |
35 |
Correct |
963 ms |
80816 KB |
Output is correct |
36 |
Correct |
1485 ms |
82812 KB |
Output is correct |
37 |
Correct |
1507 ms |
82608 KB |
Output is correct |
38 |
Correct |
1491 ms |
82260 KB |
Output is correct |
39 |
Correct |
1437 ms |
82040 KB |
Output is correct |
40 |
Correct |
1240 ms |
78700 KB |
Output is correct |
41 |
Correct |
1267 ms |
78796 KB |
Output is correct |
42 |
Correct |
996 ms |
77520 KB |
Output is correct |
43 |
Correct |
1028 ms |
77192 KB |
Output is correct |
44 |
Correct |
229 ms |
64340 KB |
Output is correct |
45 |
Correct |
227 ms |
64340 KB |
Output is correct |
46 |
Correct |
248 ms |
64524 KB |
Output is correct |
47 |
Correct |
221 ms |
63560 KB |
Output is correct |
48 |
Correct |
211 ms |
63596 KB |
Output is correct |
49 |
Correct |
215 ms |
63572 KB |
Output is correct |
50 |
Correct |
172 ms |
64032 KB |
Output is correct |
51 |
Correct |
171 ms |
64020 KB |
Output is correct |
52 |
Correct |
157 ms |
66628 KB |
Output is correct |
53 |
Correct |
164 ms |
65432 KB |
Output is correct |
54 |
Correct |
239 ms |
64792 KB |
Output is correct |
55 |
Correct |
244 ms |
64796 KB |
Output is correct |
56 |
Correct |
229 ms |
64720 KB |
Output is correct |
57 |
Correct |
243 ms |
64784 KB |
Output is correct |
58 |
Correct |
257 ms |
64756 KB |
Output is correct |
59 |
Correct |
224 ms |
64696 KB |
Output is correct |
60 |
Execution timed out |
2037 ms |
76884 KB |
Time limit exceeded |
61 |
Halted |
0 ms |
0 KB |
- |