fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. using namespace std;
  5.  
  6. using ll = long long;
  7.  
  8. int main(){
  9. ios::sync_with_stdio(false);
  10. cin.tie(nullptr);
  11.  
  12. int n;
  13. cin >> n;
  14. vector<int>a(n);
  15. for(int i=0;i<n;i++) cin>>a[i];
  16.  
  17. ll tot = 1LL*n*(n+1)/2;
  18. ll bad = 0;
  19.  
  20. for(int v=1; v<=500000; v++){
  21. int cur = 0;
  22. vector<int> pre;
  23. pre.reserve(n+1);
  24. pre.push_back(0);
  25.  
  26. for(int i=0;i<n;i++){
  27. cur += (a[i]==v ? 1 : -1);
  28. pre.push_back(cur);
  29. }
  30.  
  31. vector<int> s = pre;
  32. sort(s.begin(), s.end());
  33.  
  34. for(int i=0;i<(int)pre.size();i++){
  35. int x = pre[i];
  36. bad += lower_bound(s.begin(), s.end(), x) - s.begin();
  37. }
  38. }
  39.  
  40. cout << tot - bad;
  41. }
Success #stdin #stdout 0.06s 5320KB
stdin
4
2 1 1 3
stdout
-4999985