#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; typedef pair P; typedef long long ll; typedef vector vi; typedef vector vll; #define pu push #define pb push_back #define mp make_pair #define eps 1e-9 #define INF 2000000000 #define sz(x) ((int)(x).size()) #define fi first #define sec second #define SORT(x) sort((x).begin(),(x).end()) #define all(x) (x).begin(),(x).end() #define rep(i,n) for(int (i)=0;(i)<(int)(n);(i)++) #define repn(i,a,n) for(int (i)=(a);(i)<(int)(n);(i)++) #define EQ(a,b) (abs((a)-(b)) b.len; if(a.l!=b.l)return a.l > b.l; return a.m < b.m; } typedef pair PD; void print(PD x){ printf("len %d l %d m %d\n",x.fi.len,x.fi.l,x.fi.m); printf("len %d l %d m %d\n",x.sec.len,x.sec.l,x.sec.m); } Data input_data(){ Data res; scanf("%d %d %d",&(res.len),&(res.l),&(res.m)); return res; } PD upd(PD a,PD b,int f){ vector v; v.pb(a.fi);v.pb(a.sec); if(b.fi.l!=f)v.pb(Data(b.fi.len+1,b.fi.m,f)); if(b.fi.len!=0&&b.sec.l!=f)v.pb(Data(b.sec.len+1,b.sec.m,f)); sort(all(v),comp); PD res; res.fi = v[0]; int id = 1; while(id<(int)v.size()&&v[id].l==v[0].l)id++; res.sec = v[id]; return res; } PD merge(PD a,PD b){ vector v; v.pb(a.fi);v.pb(a.sec);v.pb(b.fi);v.pb(b.sec); sort(all(v),comp); PD res; res.fi = v[0]; int id = 1; while(id<(int)v.size()&&v[id].l==v[0].l)id++; res.sec = v[id]; return res; } const int SIZE = 1<<17; struct segtree{ PD seg[SIZE*2]; segtree(){ for(int i=0;i=b)return PD(Data(0,-1,-1),Data(0,-2,-2)); if(r<=a||b<=l)return PD(Data(0,-1,-1),Data(0,-2,-2)); else if(a<=l&&r<=b)return seg[k]; else{ PD lch = query(a,b,k*2+1,l,(l+r)/2); PD rch = query(a,b,k*2+2,(l+r)/2,r); return merge(lch,rch); } } }; segtree up,down; int N,M,A[100100]; vector vs; int main(){ /*while(1){ PD a,b; a.fi = input_data(); a.sec = input_data(); b.fi = input_data(); b.sec = input_data(); print(merge(a,b)); }*/ scanf("%d",&N); for(int i=0;ird.sec.len)rd.sec=tmp.fi; if(tmp.sec.l!=A[i]&&tmp.sec.len>rd.sec.len)rd.sec=tmp.sec; tmp = up.query(A[i]+1,M); /*printf("tmp1:\n"); print(tmp);*/ if(tmp.fi.l!=A[i])ri.fi=tmp.fi; else ri.fi = tmp.sec; tmp = up.query(A[i]+1,ri.fi.m); /*printf("tmp2:\n"); print(tmp);*/ if(tmp.fi.l!=A[i])ri.sec=tmp.fi; else ri.sec=tmp.sec; tmp = up.query(ri.fi.m+1,M); /*printf("tmp3:\n"); print(tmp);*/ if(tmp.fi.l!=A[i]&&tmp.fi.len>ri.sec.len)ri.sec=tmp.fi; if(tmp.sec.l!=A[i]&&tmp.sec.len>ri.sec.len)ri.sec=tmp.sec; /*printf("rd:\n"); print(rd); printf("ri:\n"); print(ri);*/ up.update(A[i],rd); down.update(A[i],ri); } int ans = (down.query(0,M)).fi.len; ans = max(ans,(up.query(0,M)).fi.len); if(ans<3)ans = 0; printf("%d\n",ans); return 0; }