Algorithm


D. Non-Secret Cypher
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Berland starts to seize the initiative on the war with Flatland. To drive the enemy from their native land, the berlanders need to know exactly how many more flatland soldiers are left in the enemy's reserve. Fortunately, the scouts captured an enemy in the morning, who had a secret encrypted message with the information the berlanders needed so much.

The captured enemy had an array of positive integers. Berland intelligence have long been aware of the flatland code: to convey the message, which contained a number m, the enemies use an array of integers a. The number of its subarrays, in which there are at least k equal numbers, equals m. The number k has long been known in the Berland army so General Touristov has once again asked Corporal Vasya to perform a simple task: to decipher the flatlanders' message.

Help Vasya, given an array of integers a and number k, find the number of subarrays of the array of numbers a, which has at least k equal numbers.

Subarray a[i... j] (1 ≤ i ≤ j ≤ n) of array a = (a1, a2, ..., an) is an array, made from its consecutive elements, starting from the i-th one and ending with the j-th one: a[i... j] = (ai, ai + 1, ..., aj).

Input

The first line contains two space-separated integers nk (1 ≤ k ≤ n ≤ 4·105), showing how many numbers an array has and how many equal numbers the subarrays are required to have, correspondingly.

The second line contains n space-separated integers ai (1 ≤ ai ≤ 109) — elements of the array.

Output

Print the single number — the number of such subarrays of array a, that they have at least k equal integers.

Please do not use the %lld specifier to read or write 64-bit integers in ะก++. In is preferred to use the cincout streams or the %I64d specifier.

Examples
input
Copy
4 2
1 2 1 2
output
Copy
3
input
Copy
5 3
1 2 1 1 3
output
Copy
2
input
Copy
3 1
1 1 1
output
Copy
6
Note

In the first sample are three subarrays, containing at least two equal numbers: (1,2,1), (2,1,2) and (1,2,1,2).

In the second sample are two subarrays, containing three equal numbers: (1,2,1,1,3) and (1,2,1,1).

In the third sample any subarray contains at least one 1 number. Overall they are 6: (1), (1), (1), (1,1), (1,1) and (1,1,1).



 

Code Examples

#1 Code Example with C++ Programming

Code - C++ Programming

#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;
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
4 2
1 2 1 2

Output

x
+
cmd
3
Advertisements

Demonstration


Codeforcess Solution Non-Secret Cypher, D. Non-Secret Cypher ,C,C++, Java, Js and Python ,Non-Secret Cypher,Codeforcess Solution

Previous
Codeforces solution 1080-B-B. Margarite and the best present codeforces solution
Next
CodeChef solution DETSCORE - Determine the Score CodeChef solution C,C+