双指针。
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std; const int maxn=+;
int n,k,a[maxn],b[maxn];
int cnt[maxn]; int get(int val)
{
int l=,r=n;
int pos=-;
while(l<=r)
{
int mid=(l+r)/;
if(b[mid]>val) r=mid-;
else if(b[mid]<val) l=mid+;
else pos=mid, r=mid-;
}
return pos;
} int main()
{
scanf("%d%d",&n,&k);
for(int i=;i<=n;i++)
{
scanf("%d",&a[i]);
b[i]=a[i];
}
sort(b+,b++n);
for(int i=;i<=n;i++) a[i]=get(a[i]); memset(cnt,,sizeof cnt); int l=,r=; cnt[a[]]++;
long long ans=;
while()
{
if(cnt[a[r]]==k)
{
ans=ans+(long long)(n-r+);
cnt[a[l]]--;
l++;
} else
{
r++;
cnt[a[r]]++;
}
if(l>n||r>n) break; }
printf("%lld\n",ans); return ;
}