https://codeforc.es/contest/616/problem/D
二分答案区间的长度,在判断的时候用莫队l+1,l-1,r+1,r-1 O(1)时间复杂度的情况下判断
#include<stdio.h>
#include<algorithm>
#include<iostream>
#include<set>
#include<map>
#include<vector>
#include<string.h>
#include<queue>
#include<cmath>
#define mem(a, b) memset(a, b, sizeof(a))
#define LL long long
#define N 500001
#define MOD
using namespace std;
int n, k, a[N], l, r;
int cnt[1000001];
bool judge(int x) {
mem(cnt, 0);
int ans=0;
l=1, r=x;
for(int i=1; i<=x; i++) {
cnt[a[i]]++;
if(cnt[a[i]]==1){
ans++;
}
}
while(r<=n){
if(k>=ans) return 1;
if(cnt[a[l]]==1) ans--;
cnt[a[l]]--;
l++;
r++;
if(r>n)
return 0;
cnt[a[r]]++;
if(cnt[a[r]]==1) ans++;
}
return 0;
}
int main() {
while(scanf("%d%d",&n, &k)!=EOF) {
for(int i=1; i<=n; i++) {
scanf("%d",&a[i]);
}
int L=0, R=n;
while(L<R) {
int mid=(L+R+1)>>1;
if(judge(mid))
L=mid;
else R=mid-1;
}
judge(L);
cout<<l<<" "<<r<<endl;
}
}