D.小黑的区间

用一个数组记录每一种颜色上一次出现的位置,再用一个变量记录所有不合法颜色的位置中的最大下标,遍历一遍用当前下标减去这个变量求和就是答案。因为枚举区间右端点 i,左端点就是 i 之前的最大的不合法位置。

#include<bits/stdc++.h>

using namespace std;
const int N = 2e5 + 10, mod = 998244353, INF = 0x3f3f3f3f;
typedef unsigned long long ULL;
typedef long long LL;
typedef pair<LL, LL> PLL;
typedef pair<int, int> PII;

LL n, m;
LL a[N];
void solve()
{
	cin >> n >> m;
	map<LL, LL> mp;
	LL res = 0, x = 0;
	for(int i = 1; i <= n; i ++)
	{
		cin >> a[i];
		if(mp[a[i]] && i - mp[a[i]] > m){
			x = max(x, mp[a[i]]);
		}
		res += i - x;
		mp[a[i]] = i;
	}
	cout << res << '\n';
}
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	LL _T;
	//cin >> _T;
	_T = 1;
	while(_T --)
	{
		solve();
	}
	return 0;
}