Advertisement
matheus_monteiro

The Diversity of the Library of Alexandria

Dec 26th, 2021
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.31 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define fastio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
  5. #define Max(a, b) (a > b ? a : b)
  6. #define Min(a, b) (a < b ? a : b)
  7. #define SZ(a) (int)a.size()
  8.  
  9. inline void add(int x, unordered_map<int, int> &cnt) {
  10.     cnt[ x ]++;
  11. }
  12.  
  13. inline void rem(int x, unordered_map<int, int> &cnt) {
  14.     cnt[ x ]--;
  15.     if(cnt[ x ] == 0) cnt.erase(x);
  16. }
  17.  
  18. inline int count(int x, unordered_map<int, int> &cnt) {
  19.     add(x, cnt);
  20.     int r = SZ(cnt);
  21.     rem(x, cnt);
  22.     return r;
  23. }
  24.  
  25. void solve(){
  26.     int n, k;
  27.     cin >> n >> k;
  28.     vector<int> arr(n);
  29.    
  30.     unordered_map<int, int> cnt, cnt_w;
  31.  
  32.     for(int &w : arr) cin >> w;
  33.     long long ans = 0;
  34.     int w = 0;
  35.  
  36.     for(int i = 0, j = 0; i < n; ++i) {
  37.  
  38.         w = Max(w, i);
  39.         while(j < n and count(arr[j], cnt) <= k) {
  40.             if(SZ(cnt_w) != k) {
  41.                 add(arr[j], cnt_w);
  42.                 w = j;
  43.             }
  44.             add(arr[j], cnt), ++j;
  45.         }
  46.         if(SZ(cnt_w) == k) ans += (j - w);
  47.  
  48.         rem(arr[i], cnt);
  49.         rem(arr[i], cnt_w);
  50.  
  51.         while(w < j and SZ(cnt_w) != k) {
  52.             ++w;
  53.             add(arr[w], cnt_w);
  54.         }
  55.     }
  56.  
  57.     cout << ans << '\n';
  58. }
  59.  
  60. int32_t main() {
  61.  
  62.     fastio;
  63.     solve();
  64.    
  65.     return 0;
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement