fork(1) download
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. const int N = (1 << 20) + 10;
  6. int a[N];
  7. int n;
  8.  
  9. long long calc(int x) {
  10. if (x == 0) return 0;
  11. map<int, int> mp;
  12. long long res = 0; int cnt = 0;
  13. for (int r = 1, l = 1; r <= n; r++) {
  14. cnt += ++mp[a[r]] == 1;
  15. while (l < r && cnt > x)
  16. cnt -= --mp[a[l++]] == 0;
  17. res += r - l + 1;
  18. }
  19. return res;
  20. }
  21.  
  22. int main() {
  23. ios::sync_with_stdio(false);
  24. cin.tie(nullptr);
  25. #ifndef ONLINE_JUDGE
  26. freopen("test.in", "r", stdin);
  27. freopen("test.out", "w", stdout);
  28. #endif
  29. int l, u;
  30. cin >> n >> l >> u;
  31. for (int i = 1; i <= n; i++)
  32. cin >> a[i];
  33. cout << calc(u) - calc(l - 1);
  34. cerr << "Time elapsed: " << (1.0 * clock() / CLOCKS_PER_SEC) << " s.\n";
  35. return 0;
  36. }
Success #stdin #stdout #stderr 0.01s 5404KB
stdin
4 1 2
231
19
7
19
stdout
8
stderr
Time elapsed: 0.005233 s.