Advertisement
Ritam_C

Two_pointers_step2E

Sep 26th, 2021
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.95 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. typedef long long ll;
  3. typedef unsigned long long ull;
  4. #define vi vector<int>
  5. #define vll vector<ll>
  6. #define tests int t; cin >> t; while(t--)
  7. #define pb push_back
  8. using namespace std;
  9.  
  10. template <typename T>
  11. istream& operator >> (istream& i, vector<T>& v) {
  12.     for(T& j : v) i >> j;
  13.     return i;
  14. }
  15.  
  16. template <typename T>
  17. ostream& operator << (ostream& o, const vector<T>& v) {
  18.     for(T j : v) o << j;
  19.     return o;
  20. }
  21.  
  22. int main() {
  23.     ios_base::sync_with_stdio(false);
  24.     cin.tie(NULL); cout.tie(NULL);
  25.     int n, k; cin >> n >> k;
  26.     vi v(n); cin >> v;
  27.     int arr[(int)1e5+5] = {0};
  28.     ll i = 0, j = 0, count = 0, prev = 0;
  29.     int elem = 0;
  30.     while(j < n) {
  31.         while(j < n) {
  32.             if(arr[v[j]] == 0) {
  33.                 if(elem == k) {
  34.                     break;
  35.                 }
  36.                 elem++;
  37.             }
  38.             arr[v[j++]]++;
  39.         }
  40.         count += (j-prev)*(j-prev+1)/2+(j-prev)*(prev-i);
  41.         prev = j;
  42.         if(arr[v[i]] == 1) {
  43.             elem--;
  44.         }
  45.         arr[v[i++]]--;
  46.     }
  47.     cout << count;
  48.     return 0;
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement