#define rep(i, l, r) for (auto i = (l); i < (r); i++) #define chmin(dest, src) if ((dest) > (src)) dest = (src) #define chmax(dest, src) if ((dest) < (src)) dest = (src) #include #include #include #include using namespace std; using namespace atcoder; using ll = long long; const ll INF = 1001001001001; // a'[i] = N * a[i] + i // a[i] = a'[i] / N using S1 = pair; S1 op1(S1 x, S1 y) { vector xs{x.first, x.second, y.first, y.second}; sort(xs.begin(), xs.end()); unique(xs.begin(), xs.end()); return {xs[0], xs[1]}; } S1 e1() { return {INF, INF}; } using S2 = ll; S2 op2(S2 x, S2 y) { return max(x, y); } S2 e2() { return 0; } int main() { ll N; cin >> N; vector a1(N); vector a2(N); rep(i, 0, N) { int x; cin >> x; int y = x * N + i; a1[i] = {y, y}; a2[i] = x; } segtree seg1(a1); segtree seg2(a2); ll ans = 0; rep(l, 0, N - 1) { ll ac = l + 2, wa = N + 1; while (ac + 1 < wa) { ll wj = (ac + wa) / 2; S1 x = seg1.prod(l, wj); ll m1 = x.first / N, m2 = x.second / N; S2 M = seg2.prod(l, wj); if (M <= m1 + m2) { ac = wj; } else { wa = wj; } } ll r = ac; S1 y = seg1.prod(l, r); ll m1 = y.first / N, m2 = y.second / N; S2 M = seg2.prod(l, r); if (M <= m1 + m2) { ans += r - l - 1; } } cout << ans << endl; return 0; }