-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy path190D. Non-Secret Cypher.cpp
81 lines (63 loc) · 1.5 KB
/
190D. Non-Secret Cypher.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/*
Idea:
- Using segment tree and two pointers we can track the maximum number of
repetitions in the current subarray.
- Each time the maximum number of repetitions reach value greater than or
equal to `k`, then add `n - r` to the answer and try to leave the left
element in the current subarray.
*/
#include <bits/stdc++.h>
using namespace std;
int const N = 4e5 + 1;
int n, k, a[N], seg[4 * N], tar, val;
vector<int> all;
void update(int at, int l, int r) {
if(l == r) {
seg[at] += val;
return;
}
int mid = (l + r) >> 1;
if(mid > tar)
update(at << 1, l, mid);
else
update(at << 1 | 1, mid + 1, r);
seg[at] = max(seg[at << 1], seg[at << 1 | 1]);
}
int get(int at, int l, int r) {
if(l == r)
return seg[at];
int mid = (l + r) >> 1;
if(mid > tar)
return get(at << 1, l, mid);
else
return get(at << 1 | 1, mid + 1, r);
}
int main() {
scanf("%d %d", &n, &k);
for(int i = 0; i < n; ++i) {
scanf("%d", a + i);
all.push_back(a[i]);
}
sort(all.begin(), all.end());
all.resize(unique(all.begin(), all.end()) - all.begin());
for(int i = 0; i < n; ++i)
a[i] = lower_bound(all.begin(), all.end(), a[i]) - all.begin();
long long res = 0;
int l = 0, r = 0;
while(r < n) {
tar = a[r];
val = 1;
update(1, 1, n);
int tmp = seg[1];
while(tmp >= k) {
res += n - r;
tar = a[l++];
val = -1;
update(1, 1, n);
tmp = seg[1];
}
++r;
}
printf("%lld\n", res);
return 0;
}