#include<iostream> #include<algorithm> #include<vector> #include<cmath> #define maxn 2000010 #define lowbit(i) ((i) & (-i)) #define ll long long usingnamespacestd;
int n, type;
structFenwick { ll B1[maxn], B2[maxn], B3[maxn]; int n; voidinit(int _n){ n = _n; } voidadd(int x, ll v){ for (int i = x; i <= n; i += lowbit(i)) B1[i] += v, B2[i] += v * x, B3[i] += v * x * x; }
ll get_sum(int x){ ll s = 0; for (int i = x; i; i -= lowbit(i)) s += B1[i] * (x + 1) * (x + 2) - B2[i] * (2 * x + 3) + B3[i]; return s / 2; }
voidupdate(int l, int r, int v){ add(l, v); add(r + 1, -v); }
cin >> n >> type; Bit.init(2 * n + 1); for (int i = 1, x; i <= n; ++i) cin >> x, A[x].push_back(i); for (int i = 0; i < n; ++i) A[i].push_back(n + 1); ll ans = 0; for (int i = 0; i < n; ++i) { int last = 0, pre = n + 2; Bit.update(pre, pre, 1); for (auto t : A[i]) { int d = t - last - 1; ans += Bit.query(pre - d - 1, pre - 1); Bit.update(pre - d, pre - 1, 1); pre += 1 - d; Bit.update(pre, pre, 1); last = t; } last = 0; pre = n + 2; Bit.update(pre, pre, -1); for (auto t : A[i]) { int d = t - last - 1; Bit.update(pre - d, pre - 1, -1); pre += 1 - d; Bit.update(pre, pre, -1); last = t; } } cout << ans << "\n"; return0; }
cin >> n >> type; for (int i = 1, x; i <= n; ++i) cin >> a[i], A[a[i]].push_back(i); for (int i = 0; i < n; ++i) A[i].push_back(n + 1); ll ans = 0; for (int v = 0; v < n; ++v) { unordered_map<int, int> s, d; int Min = 0, k = 0, pre = 0; ll res = 0; for (int i = 1; i <= n; ++i) { if (i > A[v][k]) ++k; if (a[i] != v && pre == Min) { ll len = A[v][k] - i - 1; --d[pre + 1]; ++d[pre - len]; i += len; pre -= len + 1; } elseif (a[i] == v) { s[pre] += d[pre]; ++s[pre]; d[pre + 1] += d[pre]; d[pre] = 0; res += s[pre++]; ans += res; } else ++s[pre--], res -= s[pre], ans += res; Min = min(Min, pre); } } cout << ans << "\n"; return0; }