用队列维护一下即可
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std; const int maxn=+;
int n,k;
int a[maxn];
int tot[+];
struct Node
{
int id;
int val;
} node[maxn];
queue<Node>Q; int main()
{
scanf("%d%d",&n,&k);
for(int i=; i<=n; i++)
{
scanf("%d",&a[i]);
node[i].id=i;
node[i].val=a[i];
} memset(tot,,sizeof tot);
int ans=,ansl,ansr;
int now=; for(int i=; i<=n; i++)
{
Q.push(node[i]);
if(tot[node[i].val]==) now++;
tot[node[i].val]++;
if(now<=k)
{
Node head=Q.front();
if(node[i].id-head.id+>ans)
{
ans=node[i].id-head.id+;
ansl=head.id;
ansr=node[i].id;
}
}
else if(now>k)
{
while()
{
Node head=Q.front();
if(tot[head.val]==) now--;
tot[head.val]--;
Q.pop();
if(now==k) break;
} Node head=Q.front();
if(node[i].id-head.id+>ans)
{
ans=node[i].id-head.id+;
ansl=head.id;
ansr=node[i].id;
}
}
}
printf("%d %d\n",ansl,ansr);
return ;
}