#include using namespace std; typedef signed long long ll; #undef _P #define _P(...) (void)printf(__VA_ARGS__) #define FOR(x,to) for(x=0;x> entry; typedef pair node; node comp(vector v,node def) { sort(ALL(v)); reverse(ALL(v)); for(int i=v.size()-1;i>=1;i--) if(v[i].second.first==v[i-1].second.first) v.erase(v.begin()+i); while(v.size()<2) v.push_back(def.first); return {v[0],v[1]}; } node comp(node n1,node n2,node def) { vector v; if(n1.first.first>0) v.push_back(n1.first); if(n1.second.first>0) v.push_back(n1.second); if(n2.first.first>0) v.push_back(n2.first); if(n2.second.first>0) v.push_back(n2.second); return comp(v,def); } template class SegTree_1 { public: vector val; V def; void init(V def_){def=def_;val=vector(NV*2,def);}; V getval(int x,int y,int l=0,int r=NV,int k=1) { if(r<=x || y<=l || x>=y) return def; if(x<=l && r<=y) return val[k]; return comp(getval(x,y,l,(l+r)/2,k*2),getval(x,y,(l+r)/2,r,k*2+1),def); } void update(int entry, V v) { entry += NV; val[entry]=comp(val[entry],v,def); while(entry>1) entry>>=1, val[entry]=comp(val[entry*2],val[entry*2+1],def); } }; SegTree_1 st_inc,st_dec; int N; int A[101010],rev[101010]; map M; void solve() { int i,j,k,l,r,x,y; string s; cin>>N; FOR(i,N) cin>>A[i],M[A[i]]=0; x=0; FORR(r,M) r.second=++x,rev[x]=r.first; int ma=0; st_inc.init({{0,{0,x+1}},{0,{0,x+1}}}); st_dec.init({{0,{0,0}},{0,{0,0}}}); FOR(i,N) { A[i]=M[A[i]]; { // dec node hoge=st_dec.def, hoge2=st_dec.def; auto r=st_inc.getval(A[i]+1,N+2); int m=r.first.second.second; vector e,e2; e.push_back(r.first); e.push_back(r.second); e.push_back(st_inc.val[m+(1<<18)].second); r=comp(st_inc.getval(A[i]+1,m),st_inc.getval(m+1,N+2),st_inc.def); m=r.first.second.second; e.push_back(r.first); e.push_back(r.second); m=r.first.second.second; e.push_back(st_inc.val[m+(1<<18)].second); FORR(rr,e) { if(A[i]!=rr.second.first) e2.push_back({rr.first+1,{rr.second.second,A[i]}}), ma=max(ma,rr.first+1); } st_dec.update(A[i],comp(e2,st_dec.def)); } { // inc node hoge=st_inc.def, hoge2=st_inc.def; auto r=st_dec.getval(0,A[i]); int m=r.first.second.second; vector e,e2; e.push_back(r.first); e.push_back(r.second); e.push_back(st_dec.val[m+(1<<18)].second); r=comp(st_dec.getval(0,m),st_dec.getval(m+1,A[i]),st_dec.def); e.push_back(r.first); e.push_back(r.second); m=r.first.second.second; e.push_back(st_dec.val[m+(1<<18)].second); FORR(rr,e) { if(A[i]!=rr.second.first) e2.push_back({rr.first+1,{rr.second.second,A[i]}}), ma=max(ma,rr.first+1); } st_inc.update(A[i],comp(e2,st_inc.def)); } /* if(i>N-10) { _P("%d:%d\n",i,A[i]); _P(" dec:: "); for(j=1;j<=4;j++) { auto r=st_dec.val[(1<<18)+j]; auto f=r.first, s=r.second; _P("(%d,%d,%d),",f.first,rev[f.second.first],rev[f.second.second]); _P("(%d,%d,%d) ",s.first,rev[s.second.first],rev[s.second.second]); } _P("\n"); _P(" inc:: "); for(j=1;j<=4;j++) { auto r=st_inc.val[(1<<18)+j]; auto f=r.first, s=r.second; _P("(%d,%d,%d),",f.first,rev[f.second.first],rev[f.second.second]); _P("(%d,%d,%d) ",s.first,rev[s.second.first],rev[s.second.second]); } _P("\n"); } */ } if(ma<3) ma=0; cout<