Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- #define fastio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
- #define Max(a, b) (a > b ? a : b)
- #define Min(a, b) (a < b ? a : b)
- #define SZ(a) (int)a.size()
- inline void add(int x, unordered_map<int, int> &cnt) {
- cnt[ x ]++;
- }
- inline void rem(int x, unordered_map<int, int> &cnt) {
- cnt[ x ]--;
- if(cnt[ x ] == 0) cnt.erase(x);
- }
- inline int count(int x, unordered_map<int, int> &cnt) {
- add(x, cnt);
- int r = SZ(cnt);
- rem(x, cnt);
- return r;
- }
- void solve(){
- int n, k;
- cin >> n >> k;
- vector<int> arr(n);
- unordered_map<int, int> cnt, cnt_w;
- for(int &w : arr) cin >> w;
- long long ans = 0;
- int w = 0;
- for(int i = 0, j = 0; i < n; ++i) {
- w = Max(w, i);
- while(j < n and count(arr[j], cnt) <= k) {
- if(SZ(cnt_w) != k) {
- add(arr[j], cnt_w);
- w = j;
- }
- add(arr[j], cnt), ++j;
- }
- if(SZ(cnt_w) == k) ans += (j - w);
- rem(arr[i], cnt);
- rem(arr[i], cnt_w);
- while(w < j and SZ(cnt_w) != k) {
- ++w;
- add(arr[w], cnt_w);
- }
- }
- cout << ans << '\n';
- }
- int32_t main() {
- fastio;
- solve();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement