#pragma GCC target("avx2") #pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") #include #include #include #include #include #include #include #include #include #include #define mkp make_pair #define mkt make_tuple #define rep(i,n) for(int i = 0; i < (n); ++i) #define all(v) v.begin(),v.end() using namespace std; typedef long long ll; const ll MOD=1e9+7; template void chmin(T &a,const T &b){if(a>b) a=b;} template void chmax(T &a,const T &b){if(a #include //SegmentTree seg(N,[](int a,int b){return min(a,b);},INT_MAX); template< typename T> class SegmentTree{ private: void resize(int size){ n=1; while(n; int n; vector tree; F operation; T def; SegmentTree(){} SegmentTree(int size,F _operation,T _def):operation(_operation),def(_def){ resize(size); } void embody(int size,F _operation,T _def){ operation=_operation; def=_def; resize(size); } void initialize(const vector &v){ int size=v.size(); resize(size); for(int i=0;i=0;i--) tree[i]=operation(tree[2*i+1],tree[2*i+2]); } void update(int index,T value){ index+=n-1; tree[index]=value; while(index>0){ index=(index-1)/2; tree[index]=operation(tree[2*index+1],tree[2*index+2]); } } T query(int a,int b,int k=0,int l=0,int r=-1){//[a,b) if(r<0) r=n; if(r<=a||b<=l) return def; else if(a<=l&&r<=b) return tree[k]; else{ T lval=query(a,b,2*k+1,l,(l+r)/2); T rval=query(a,b,2*k+2,(l+r)/2,r); return operation(lval,rval); } } T get(int index){ return tree[index+n-1]; } int find(int x,int k=0,int l=0,int r=-1){// a[0]+...+a[i]>=x (i:minimal) if(r<0) r=n; if(tree[k]=x) return find(x,2*k+1,l,(l+r)/2); else return find(x-tree[2*k+1],2*k+2,(l+r)/2,r); } //right -> change vl to vr (notice doesn't contain b. range [a,b)) int find_left(int a,int b,T x,int k=0,int l=0,int r=-1){// max(a[a],...,a[i])>=x (i:minimal) if(r<0) r=n; if(tree[k]=0) return vl; return find_left(a,b,x,2*k+2,(l+r)/2,r); } }; const ll INF=1e15; int main(){ cin.tie(0); ios::sync_with_stdio(false); int N; cin>>N; vector A(N); rep(i,N) cin>>A[i]; typedef pair Pli; SegmentTree maseg(N,[](Pli a,Pli b){ if(a.first<=b.first) return b; return a; },mkp(-INF,N)); { vector init(N); rep(i,N) init[i]=mkp(A[i],i); maseg.initialize(init); } typedef pair Pll; SegmentTree miseg(N,[](Pll a,Pll b){ vector p={a.first,a.second,b.first,b.second}; sort(all(p)); return mkp(p[0],p[1]); },mkp(INF,INF)); { vector init(N); rep(i,N) init[i]=mkp(A[i],INF); miseg.initialize(init); } ll ans=0; auto solve = [&](auto &&solve,int l,int r)->void{//[l,r) if(r-l<=1) return; auto [ma,mat] = maseg.query(l,r); solve(solve,l,mat); solve(solve,mat+1,r); if(mat-l=l;i--){ if(mi1>A[i]){ mi2=mi1; mi1=A[i]; }else if(mi2>A[i]){ mi2=A[i]; } if(mi1+mi21){ int mid=(ok+ng)/2; auto [va,vb]=miseg.query(mat,mid+1); vector p={mi1,mi2,va,vb}; sort(all(p)); if(p[0]+p[1]>=ma) ok=mid; else ng=mid; } if(i==mat) ans--; ans+=ok-mat+1; last=ok+1; } }else{ ll mi1=ma; ll mi2=ma; int last=l-1; for(int i=mat;iA[i]){ mi2=mi1; mi1=A[i]; }else if(mi2>A[i]){ mi2=A[i]; } if(mi1+mi21){ int mid=(ok+ng)/2; auto [va,vb]=miseg.query(mid,mat+1); vector p={mi1,mi2,va,vb}; sort(all(p)); if(p[0]+p[1]>=ma) ok=mid; else ng=mid; } if(i==mat) ans--; ans+=mat-ok+1; last=ok-1; } } }; solve(solve,0,N); cout<