#include using namespace std; struct fast_ios { fast_ios(){ cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(20); }; } fast_ios_; #define FOR(i, begin, end) for(int i=(begin),i##_end_=(end);i=i##_begin_;i--) #define REP(i, n) FOR(i,0,n) #define IREP(i, n) IFOR(i,0,n) template istream &operator>>(istream &is, vector &vec){ for (auto &v : vec) is >> v; return is; } #define dbg(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ") " << __FILE__ << endl; // Binary lifting / `Doubling` // Complexity: O(NlogN) precalculation / O(logN) per query // struct BinaryLifting { int N, INVALID, lgD; std::vector> mat; BinaryLifting() : N(0), lgD(0) {} BinaryLifting(const std::vector &vec_nxt, int INVALID = -1, int lgd = 0) : N(vec_nxt.size()), INVALID(INVALID), lgD(lgd) { while ((1 << lgD) < N) lgD++; mat.assign(lgD, std::vector(N, INVALID)); mat[0] = vec_nxt; for (int i = 0; i < N; i++) if (mat[0][i] < 0 or mat[0][i] >= N) mat[0][i] = INVALID; for (int d = 0; d < lgD - 1; d++) { for (int i = 0; i < N; i++) if (mat[d][i] != INVALID) mat[d + 1][i] = mat[d][mat[d][i]]; } } int kth_next(int now, int k) { if (k >= (1 << lgD)) exit(8); for (int d = 0; k and now != INVALID; d++, k >>= 1) if (k & 1) now = mat[d][now]; return now; } // Distance from l to [r, \infty) // Requirement: mat[0][i] > i for all i (monotone increasing) int distance(int l, int r) { if (l >= r) return 0; int ret = 0; for (int d = lgD - 1; d >= 0; d--) { if (mat[d][l] < r and mat[d][l] != INVALID) ret += 1 << d, l = mat[d][l]; } if (mat[0][l] == INVALID or mat[0][l] >= r) return ret + 1; else return -1; // Unable to reach } }; struct RangeMinimumQuery { using T = int; int N; T defaultT = 1e9; int head; vector x; T _get(int begin, int end, int pos, int l, int r) const { if (r <= begin or l >= end) return defaultT; if (l >= begin and r <= end) return x[pos]; return min(_get(begin, end, 2 * pos + 1, l, (l + r) / 2), _get(begin, end, 2 * pos + 2, (l + r) / 2, r)); } RangeMinimumQuery(int N, T defaultT) : N(N), defaultT(defaultT) { int N_tmp = 1; while (N_tmp < N) N_tmp <<= 1; x.assign(N_tmp*2, defaultT), head = N_tmp - 1; } void build(const vector &vals) { copy(vals.begin(), vals.end(), x.begin() + head); for (int i = head - 1; i >= 0; i--) { x[i] = min(x[i * 2 + 1], x[i * 2 + 2]); } } RangeMinimumQuery(const vector &vals, T defaultT) : N(vals.size()), defaultT(defaultT) { int N_tmp = 1; while (N_tmp < N) N_tmp <<= 1; x.assign(N_tmp*2, defaultT), head = N_tmp - 1; build(vals); } RangeMinimumQuery() : RangeMinimumQuery(0, T()) {} void update(int pos, T val) { pos += head, x[pos] = val; while (pos) pos = (pos - 1) / 2, x[pos] = min(x[pos * 2 + 1], x[pos * 2 + 2]); } T get(int begin, int end) const { return _get(begin, end, 0, 0, (int)x.size() / 2); } friend ostream &operator<<(ostream &os, const RangeMinimumQuery &s) { os << "["; for (int i = 0; i < s.N; i++) os << s.get(i, i + 1) << ","; os << "]"; return os; } }; int main() { int N; cin >> N; vector P(N + 1); REP(i, N) cin >> P[i]; long long ret = 0; REP(_, 2) { vector Pinv(N + 1); REP(i, N) Pinv[P[i]] = i; vector nxt(N + 1, N); RangeMinimumQuery rmq(N + 1, N); IREP(i, N) { nxt[i] = rmq.get(0, P[i]); rmq.update(P[i], i); } BinaryLifting bl(nxt); set ban{N}; IFOR(v, 1, N + 1) { int i = Pinv[v]; int hi = *ban.lower_bound(i); ret += bl.distance(i, hi) - 1; ban.insert(i); } reverse(P.begin(), P.begin() + N); } cout << ret << '\n'; }