#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(node n1,node n2,node def) { int i,x=0,y=0; node r; vector v; v.push_back(n1.first); v.push_back(n2.first); v.push_back(n1.second); v.push_back(n2.second); v.push_back(def.first); v.push_back(def.second); sort(ALL(v)); reverse(ALL(v)); for(i=5;i>=1;i--) if(v[i].second.first==v[i-1].second.first) v.erase(v.begin()+i); r = {v[0],v[1]}; return r; } 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) 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]=v; 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]; 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; 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 auto r=st_inc.getval(A[i]+1,N+1); auto f=r.first; auto s=r.second; node hoge=st_dec.def; if(A[i] ",A[i],f.first,f.second.first,f.second.second,s.first,s.second.first,s.second.second); _P("(%d,%d,%d),(%d,%d,%d)\n",hoge.first.first,hoge.first.second.first,hoge.first.second.second, hoge.second.first,hoge.second.second.first,hoge.second.second.second);*/ st_dec.update(A[i],hoge); } { // inc auto r=st_dec.getval(0,A[i]-1); auto f=r.first; auto s=r.second; node hoge=st_inc.def; if(A[i]>f.second.second && A[i]!=f.second.first) { hoge.first = {f.first+1,{f.second.second,A[i]}}; ma=max(ma,f.first+1); } if(A[i]>s.second.second && A[i]!=s.second.first) { hoge.second = {s.first+1,{s.second.second,A[i]}}; ma=max(ma,s.first+1); } if(hoge.first ",A[i],f.first,f.second.first,f.second.second,s.first,s.second.first,s.second.second); _P("(%d,%d,%d),(%d,%d,%d)\n",hoge.first.first,hoge.first.second.first,hoge.first.second.second, hoge.second.first,hoge.second.second.first,hoge.second.second.second);*/ st_inc.update(A[i],hoge); } } if(ma<3) ma=0; cout<