#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #define llint long long #define inf 1e18 #define rep(x, s, t) for(llint (x) = (s); (x) < (t); (x)++) #define Rep(x, s, t) for(llint (x) = (s); (x) <= (t); (x)++) #define chmin(x, y) (x) = min((x), (y)) #define chmax(x, y) (x) = max((x), (y)) using namespace std; typedef pair P; llint n; llint p[100005]; vector vec; llint calc(llint l, llint r) { if(r-l+1 <= 1) return 0; if(r-l+1 == 2){ if(p[l] > p[r]) return 1; else return 0; } llint ret = 0, m = (l+r)/2; vec.clear(); llint mn = inf; for(int i = m; i <= r; i++){ if(mn > p[i]) vec.push_back(i); mn = min(mn, p[i]); } llint lmax = -inf, lmin = inf, rmax = -inf, rmin = inf, mpos = m, Mpos = m; for(int i = m; i >= l; i--){ bool flag = false; if(lmax < p[i]){ lmax = p[i]; flag = true; } lmin = min(lmin, p[i]); while(mpos <= r && rmin >= lmin){ mpos++; rmin = min(rmin, p[mpos]); } while(Mpos <= r && rmax <= lmax){ Mpos++; rmax = max(rmax, p[Mpos]); } if(flag && Mpos > mpos){ ret += lower_bound(vec.begin(), vec.end(), Mpos) - lower_bound(vec.begin(), vec.end(), mpos); cout << lower_bound(vec.begin(), vec.end(), Mpos) - lower_bound(vec.begin(), vec.end(), mpos) << endl; } } ret += calc(l, m); ret += calc(m+1, r); return ret; } int main(void) { ios::sync_with_stdio(0); cin.tie(0); cin >> n; for(int i = 1; i <= n; i++) cin >> p[i]; llint ans = calc(1, n); reverse(p+1, p+n+1); ans += calc(1, n); cout << ans << endl; return 0; }