fork download
  1. // #pragma GCC optimize("Ofast")
  2. // #pragma GCC optimize("unroll-loops")
  3. // #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
  4.  
  5. #include <bits/stdc++.h>
  6. #include <ext/pb_ds/assoc_container.hpp>
  7. #include <ext/pb_ds/tree_policy.hpp>
  8. using namespace std;
  9. using namespace __gnu_pbds;
  10. #define endl '\n'
  11. #define ll long long
  12. #define mod 1000000007
  13. #define all(x) x.begin(), x.end()
  14. template <typename T>
  15. using super_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
  16. #define O_O() ({ \
  17.   ios_base::sync_with_stdio(false); \
  18.   cin.tie(NULL); \
  19. })
  20. // int dx[]= {-1, 1, 0, 0, -1,-1, 1, 1};
  21. // int dy[]= { 0, 0,-1, 1, -1, 1,-1, 1};
  22.  
  23. const int N = 2e5 + 9;
  24. int n, k, arr[N], fq[N], distinct;
  25.  
  26. void solve()
  27. {
  28. cin >> n >> k;
  29. for(int i = 1; i <= n; i++) {
  30. cin >> arr[i];
  31. }
  32. ll ans = 0, i = 1, j = 1;
  33. while(j <= n) {
  34.  
  35. fq[arr[j]]++;
  36. if (fq[arr[j]] == 1) {
  37. distinct++;
  38. }
  39. if(distinct > k) {
  40. while(distinct > k) {
  41. fq[arr[i]]--;
  42. if(!fq[arr[i]]) distinct--;
  43. i++;
  44. }
  45. }
  46. if (distinct == k)
  47. {
  48. ans += fq[arr[i]];
  49. }
  50. j++;
  51. }
  52. cout << ans << endl;
  53. }
  54.  
  55. int main()
  56. {
  57. // freopen("input.txt","r",stdin);
  58. O_O();
  59. int test = 1;
  60. // cin >> test;
  61. for (int tc = 1; tc <= test; tc++)
  62. {
  63. solve();
  64. }
  65.  
  66. return 0;
  67. }
  68.  
Success #stdin #stdout 0s 5304KB
stdin
Standard input is empty
stdout
0