#include #include #include using namespace std; namespace{ typedef long long LL; void main2(){ int n; scanf("%d", &n); vector as(n); for(int i = 0; i < n; ++i){ scanf("%d", &as[i]); } int mval = *max_element(as.begin(), as.end()); vector > postbl(mval + 1); for(int i = 0; i < n; ++i){ postbl[as[i]].push_back(i); } vector cnts; cnts.reserve(2 * n + 10); LL ans = 0; for(int x = 0; x <= mval; ++x){ const vector &poss = postbl[x]; int sz = poss.size(); for(int l = 0; l < sz; ){ int r; for(r = l + 1; r < sz && poss[r] - poss[r - 1] <= sz; ++r); int beg = max(poss[l] - (r - l) + 1, 0); int en = min(poss[r - 1] + (r - l), n); cnts.assign((en - beg) * 2 + 8, 0); int h = en - beg + 4; cnts[h] = 1; LL low = 0; for(int i = beg; i < en; ++i){ if(x == as[i]){ low += cnts[h++]; } else{ low -= cnts[--h]; } ++cnts[h]; ans += low; } l = r; } } printf("%lld\n", ans); } } int main(){ main2(); }