fork download
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4.  
  5. using ll = long long;
  6.  
  7. int main() {
  8. ios::sync_with_stdio(false);
  9. cin.tie(nullptr);
  10.  
  11. int n;
  12. cin >> n;
  13.  
  14. vector<int> a(n);
  15. for (int i = 0; i < n; i++) cin >> a[i];
  16.  
  17. const int MAXA = 500000;
  18. vector<vector<int>> pos(MAXA + 1);
  19.  
  20. for (int i = 0; i < n; i++)
  21. pos[a[i]].push_back(i);
  22.  
  23. ll bad = 0;
  24.  
  25. for (int v = 1; v <= MAXA; v++) {
  26. auto &p = pos[v];
  27. int m = p.size();
  28. if (m <= 1) continue;
  29.  
  30. int j = 0;
  31. for (int i = 0; i < m; i++) {
  32. if (j < i) j = i;
  33. while (j + 1 < m && p[j + 1] - p[i] + 1 < 2 * (j - i + 2))
  34. j++;
  35. bad += (j - i);
  36. }
  37. }
  38.  
  39. ll total = 1LL * n * (n + 1) / 2;
  40. cout << total - bad;
  41. }
Success #stdin #stdout 0.01s 14904KB
stdin
4
2 1 1 3
stdout
9