#include #include #include #include #include #define _USE_MATH_DEFINES #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; #define FOR(i,m,n) for(int i=(m);i<(n);++i) #define REP(i,n) FOR(i,0,n) #define ALL(v) (v).begin(),(v).end() const int INF = 0x3f3f3f3f; const long long LINF = 0x3f3f3f3f3f3f3f3fLL; const double EPS = 1e-8; const int MOD = 1000000007; // const int MOD = 998244353; const int dy[] = {1, 0, -1, 0}, dx[] = {0, -1, 0, 1}; // const int dy[] = {1, 1, 0, -1, -1, -1, 0, 1}, // dx[] = {0, -1, -1, -1, 0, 1, 1, 1}; struct IOSetup { IOSetup() { cin.tie(nullptr); ios_base::sync_with_stdio(false); cout << fixed << setprecision(20); cerr << fixed << setprecision(10); } } iosetup; /*-------------------------------------------------*/ template struct BIT { BIT(int n, const Abelian &UNITY = 0) : n(n), UNITY(UNITY), dat(n, UNITY) {} void add(int idx, const Abelian &value) { while (idx < n) { dat[idx] += value; idx |= idx + 1; } } Abelian sum(int idx) { Abelian res = UNITY; while (idx >= 0) { res += dat[idx]; idx = (idx & (idx + 1)) - 1; } return res; } Abelian sum(int left, int right) { if (right < left) return UNITY; return sum(right) - sum(left - 1); } Abelian operator[](const int idx) { return sum(idx, idx); } int lower_bound(Abelian value) { if (value <= UNITY) return 0; int res = 0, exponent = 1; while (exponent <= n) exponent <<= 1; for (int mask = exponent >> 1; mask > 0; mask >>= 1) { if (res + mask - 1 < n && dat[res + mask - 1] < value) { value -= dat[res + mask - 1]; res += mask; } } return res; } private: int n; const Abelian UNITY; vector dat; }; long long solve(vector &a) { int n = a.size(); vector b(a); FOR(i, 1, n) a[i] += a[i - 1]; map cnt; REP(i, n) ++cnt[a[i]]; int m = cnt.size(); BIT bit(m); int idx = 0; for (auto it = cnt.begin(); it != cnt.end(); ++it, ++idx) { bit.add(idx, (*it).second); } int fro = (*cnt.begin()).first, border = 1 - fro; long long ans = 0; REP(i, n) { ans += bit.sum(max(border, 0), m - 1); bit.add(a[i] - fro, -1); border += b[i]; } return ans; } int main() { const int A = 100000; int n; cin >> n; vector > idx(A); REP(i, n) { int a; cin >> a; --a; idx[a].emplace_back(i); } long long ans = 0; REP(now, A) { if (idx[now].empty()) continue; vector conc; int cnt = 0; REP(i, idx[now].size()) { conc.emplace_back(idx[now][i]); ++cnt; int j = idx[now][i] + 1; while (cnt > 0 && j < n && (i + 1 == idx[now].size() || j < idx[now][i + 1])) { conc.emplace_back(j++); --cnt; } } cnt = 0; for (int i = idx[now].size() - 1; i >= 0; --i) { ++cnt; int j = idx[now][i] - 1; while (cnt > 0 && j >= 0 && (i == 0 || idx[now][i - 1] < j)) { conc.emplace_back(j--); --cnt; } } sort(ALL(conc)); conc.erase(unique(ALL(conc)), conc.end()); set st; for (int e : idx[now]) st.emplace(e); for (int i = 0; i < conc.size();) { vector sub{st.count(conc[i]) == 1 ? 1 : -1}; int j = i + 1; while (j < conc.size() && conc[j] - conc[j - 1] == 1) { sub.emplace_back(st.count(conc[j]) == 1 ? 1 : -1); ++j; } ans += solve(sub); i = j; } } cout << ans << '\n'; return 0; }