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 cin, cout streams or the %I64d specifier.

Examples

Input
4 2
1 2 1 2
Output
3
Input
5 3
1 2 1 1 3
Output
2
Input
3 1
1 1 1
Output
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).

思路:双指针问题,和A题相似,一个指针每次递增一,另一个指针随条件改变不止一,一般是维护第一个指针的极值,然后计算数量

const int maxm = 4e5+;

int buf[maxm], n, k;
map<int, int> vis; int main() {
ios::sync_with_stdio(false), cin.tie();
cin >> n >> k;
for(int i = ; i < n; ++i)
cin >> buf[i];
LL ans = ;
int l = , r = ;
vis[buf[]]++;
while(l + k - < n) {
while(r < n) {
if(vis[buf[r]] >= k) break;
r++;
vis[buf[r]]++;
}
if(r == n) break;
ans += (LL)(n - r);
vis[buf[l]]--;
l++;
}
cout << ans << "\n";
return ;
}

Day8 - B - Non-Secret Cypher CodeForces - 190D的更多相关文章

  1. CodeForces 190D Non-Secret Cypher

    双指针. #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> ...

  2. Day8 - A - Points on Line CodeForces - 251A

    Little Petya likes points a lot. Recently his mom has presented him n points lying on the line OX. N ...

  3. [Codeforces] #603 (Div. 2) A-E题解

    [Codeforces]1263A Sweet Problem [Codeforces]1263B PIN Code [Codeforces]1263C Everyone is a Winner! [ ...

  4. CodeForces 490C Hacking Cypher

    Hacking Cypher Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Sub ...

  5. CodeForces 496B Secret Combination

    Secret Combination Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u ...

  6. Codeforces Round #279 (Div. 2) C. Hacking Cypher 前缀+后缀

    C. Hacking Cypher time limit per test 1 second memory limit per test 256 megabytes input standard in ...

  7. Codecraft-17 and Codeforces Round #391 (Div. 1 + Div. 2, combined)D. Felicity's Big Secret Revealed

    题目连接:http://codeforces.com/contest/757/problem/D D. Felicity's Big Secret Revealed time limit per te ...

  8. Codeforces Round #279 (Div. 2) C. Hacking Cypher (大数取余)

    题目链接 C. Hacking Cyphertime limit per test1 secondmemory limit per test256 megabytesinputstandard inp ...

  9. 构造+暴力 Codeforces Round #283 (Div. 2) B. Secret Combination

    题目传送门 /* 构造+暴力:按照题目意思,只要10次加1就变回原来的数字,暴力枚举所有数字,string大法好! */ /************************************** ...

随机推荐

  1. TimeSeriesEditor时间序列编辑软件之实战ReoGrid表格控件和Zedgraph绘图控件

    最近用ReoGrid表格控件和Zedgraph绘图控件写了一个TimeSeriesEditor时间序列编辑软件,如下图. 目的就是体验一下这两个空间的用法,感觉还是挺好用的, 关于软件的使用说明可以访 ...

  2. vue父孙组件传值($attr及$listeners)的使用

    父组件 <template> <div> <!-- 将值传给子组件 子组件可以获取自己想要的值 也可以忽视掉需要传给孙子组件的值 --> <!-- 同时获取通 ...

  3. python学习记录(持续更新)--最最最基础的一部分(方法,异常处理,注释,类)

    写在前面 本系列教程针对有一定编程经验的伙伴快速入门python基础,一些涉及开发的常识问题,本文并不涉及. 方法 function def greet_user(name): print(f'Hi ...

  4. 八 Struts2访问Servlet的API方式三:接口注入

    Struts2访问Servlet的API方式三:接口注入 通过实现ServletRequestAware, ServletContextAware 接口,拿到Request.ServletContex ...

  5. PPT页面动画制作

    因为武汉新型冠状肺炎的影响,今年自从2月3号开工以来,就在家办公.我的任务刚好是安排做PPT,虽说之前做过PPT,但大家都知道,作为一个IT测试工程师,更多的是测试工作,只有在培训,还有年终汇报的时候 ...

  6. 本机配置集群主机名访问(Windows配置hosts)

    Windows配置hosts C:\Windows\System32\drivers\etc\hosts 主机IP 主机名 示例: 192.168.1.1 master 192.168.1.2 sla ...

  7. centos7中redis安装

    官网地址:http://redis.io/ 官网下载地址:http://redis.io/download 1. 下载Redis源码(tar.gz),并上传到Linux:或 wget http://d ...

  8. C++中函数访问数组的方式

    在书写C++代码时,往往为了令代码更加简洁高效.提高代码可读性,会对定义的函数有一些特殊的要求:比如不传递不必要的参数,以此来让函数的参数列表尽可能简短. 当一个函数需要访问一个数组元素时,出于上述原 ...

  9. RHEL6配置CentOS yum源

    RHEL6配置CentOS yum源

  10. 9.2.3 hadoop reduce端连接-分区分组聚合

    1.1.1         reduce端连接-分区分组聚合 reduce端连接则是利用了reduce的分区功能将stationid相同的分到同一个分区,在利用reduce的分组聚合功能,将同一个st ...