Advertisement
7oSkaaa

Segments with Small Set

Aug 11th, 2021
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.49 KB | None | 0 0
  1.   #include <bits/stdc++.h>
  2.   using namespace std;
  3.  
  4.   #define cin(vec) for(auto& i : vec) cin >> i
  5.   #define cin_2d(vec, n, m) for(int i = 0; i < n; i++) for(int j = 0; j < m && cin >> vec[i][j]; j++);
  6.   #define cout(vec) for(auto& i : vec) cout << i << " "; cout << "\n";
  7.   #define cout_map(mp) for(auto& [f, s] : mp) cout << f << "  " << s << "\n";
  8.   #define loop(a, b, c) for(int i = a ; i < (b); i += c)
  9.   #define fixed(n) cout << fixed << setprecision(n);
  10.   #define ceil(n, m) (((n) / (m)) + (n % m ? 1 : 0))
  11.   #define all(vec) vec.begin(),vec.end()
  12.   #define rall(vec) vec.rbegin(),vec.rend()
  13.   #define sz size()
  14.   #define fi first
  15.   #define se second
  16.   #define Pair pair <int,int>
  17.   #define ll long long
  18.   #define ull unsigned long long
  19.   #define Mod  1000'000'007
  20.   #define INF 2000'000'000
  21.   #define PI 3.14159265359
  22.  
  23.   void Code_Crush(){
  24.     ios_base::sync_with_stdio(false);   cin.tie(nullptr);   cout.tie(nullptr);
  25.     #ifndef ONLINE_JUDGE
  26.       freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout);
  27.     #endif
  28.   }
  29.  
  30.   int main(){
  31.     Code_Crush();
  32.     int n, k;                                    cin >>  n >> k;
  33.     vector <int> nums(n);
  34.     map <int, int> occ;
  35.     cin(nums);
  36.     ll count = 0, r = 0, l = 0;
  37.     while(r < n){
  38.       occ[nums[r]]++;
  39.       while(occ.sz > k){
  40.         occ[nums[l]]--;
  41.         if(occ[nums[l]] == 0) occ.erase(nums[l]);
  42.         l++;
  43.       }
  44.       count += r - l + 1;
  45.       r++;
  46.     }
  47.     cout << count;
  48.     return 0;
  49.   }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement