和区间众数几乎一模一样的套路。
// luogu-judger-enable-o2
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<cstring>
#include<algorithm>
using namespace std;
int read()
{
int x=,f=;char c=getchar();
while (c<''||c>'') {if (c=='-') f=-;c=getchar();}
while (c>=''&&c<='') x=(x<<)+(x<<)+(c^),c=getchar();
return x*f;
}
#define N 100010
#define BLOCK 350
int n,m,c,a[N],lastans=;
int block,tot,L[N],R[N],pos[N];
int cnt[N],f[BLOCK][BLOCK],sum[BLOCK][N];
int main()
{
freopen("bzoj2821.in","r",stdin);
freopen("bzoj2821.out","w",stdout);
n=read(),c=read(),m=read();
for (int i=;i<=n;i++) a[i]=read();
block=sqrt(n);tot=n/block+(n%block>);
for (int i=;i<=n/block;i++)
L[i]=(i-)*block+,R[i]=(i-)*block+block;
if (n/block<tot) L[tot]=n/block*block+,R[tot]=n;
for (int i=;i<=tot;i++)
{
memset(cnt,,sizeof(cnt));
for (int j=i;j<=tot;j++)
{
f[i][j]=f[i][j-];
for (int k=L[j];k<=R[j];k++)
{
cnt[a[k]]++;
if (!(cnt[a[k]]&)) f[i][j]++;
else if (cnt[a[k]]>) f[i][j]--;
}
}
memcpy(sum[i],sum[i-],sizeof(sum[i]));
for (int j=L[i];j<=R[i];j++)
pos[j]=i,sum[i][a[j]]++;
}
memset(cnt,,sizeof(cnt));
while (m--)
{
int x=read(),y=read();
x=(x+lastans)%n+,y=(y+lastans)%n+;
if (x>y) swap(x,y);
int num=;
if (pos[x]==pos[y])
{
for (int i=x;i<=y;i++)
{
cnt[a[i]]++;
if (!(cnt[a[i]]&)) num++;
else if (cnt[a[i]]>) num--;
}
for (int i=x;i<=y;i++) cnt[a[i]]--;
}
else
{
num=f[pos[x]+][pos[y]-];
for (int i=x;i<=R[pos[x]];i++)
{
cnt[a[i]]++;
if (!(cnt[a[i]]+sum[pos[y]-][a[i]]-sum[pos[x]][a[i]]&)) num++;
else if (cnt[a[i]]+sum[pos[y]-][a[i]]-sum[pos[x]][a[i]]>) num--;
}
for (int i=L[pos[y]];i<=y;i++)
{
cnt[a[i]]++;
if (!(cnt[a[i]]+sum[pos[y]-][a[i]]-sum[pos[x]][a[i]]&)) num++;
else if (cnt[a[i]]+sum[pos[y]-][a[i]]-sum[pos[x]][a[i]]>) num--;
}
for (int i=x;i<=R[pos[x]];i++) cnt[a[i]]--;
for (int i=L[pos[y]];i<=y;i++) cnt[a[i]]--;
}
lastans=num;
printf("%d\n",num);
}
return ;
}