fork download
  1. #include<iostream>
  2. #include<unordered_set>
  3. #include<vector>
  4.  
  5. using namespace std;
  6.  
  7. int main() {
  8. unordered_set<int> cnt;
  9. int n;
  10. cin >> n;
  11. vector<int> x(n);
  12. for(int i = 0; i < n; i++) {
  13. cin >> x[i];
  14. }
  15. long long ans = 0;
  16. int l = 0;
  17. for(int r = 0; r < n; r++) {
  18. while(cnt.count(x[r])) {
  19. cnt.erase(x[l++]);
  20. }
  21. cnt.insert(x[r]);
  22. ans += r - l + 1;
  23. }
  24. cout << ans << '\n';
  25. return 0;
  26. }
  27.  
Success #stdin #stdout 0s 5316KB
stdin
4
1 2 1 3
stdout
8