11qiuz @ 2023-07-20 10:41:45
#include<bits/stdc++.h>
using namespace std;
inline int read(){
int x=0;bool f=1;char c=getchar();
for(;!isdigit(c);c=getchar())if(c=='-')f=0;
for(;isdigit(c);c=getchar())x=(x<<3)+(x<<1)+c-'0';
return f?x:(~(x-1));
}
int n,m,a[40010],t[40010],siz;
int sum[300][40010],most[300][300];
//sum±íʾǰi¿éÖÐjÑÕÉ«¸öÊý
int len,tot,bel[40010],l[300],r[300];
inline void build(){
len=sqrt(n);tot=n/len+(n%len?1:0);
for(int i=1;i<=n;++i){
bel[i]=(i-1)/len+1;
sum[bel[i]][a[i]]++;
}
for(int i=1;i<=tot;++i){
l[i]=(i-1)*len+1;r[i]=i*len;
for(int j=1;j<=siz;++j)
sum[i][j]+=sum[i-1][j];
}
r[tot]=n;
for(int i=1;i<=tot;++i){
for(int j=i;j<=tot;++j){
int MAX=most[i][j-1];
for(int k=l[bel[j]];k<=r[bel[j]];++k)
if((sum[j][a[k]]-sum[i-1][a[k]]>sum[j][MAX]-sum[i-1][MAX])||(sum[j][a[k]]-sum[i-1][a[k]]==sum[j][MAX]-sum[i-1][MAX]&&a[k]<MAX))
MAX=a[k];
most[i][j]=MAX;
}
}
}
int ans[40010];
inline int query(int x,int y){
int MAX=0;
if(bel[y]-bel[x]<=1){
for(int i=x;i<=y;++i)ans[a[i]]=0;
for(int i=x;i<=y;++i)ans[a[i]]++;
for(int i=x;i<=y;++i)
if((ans[a[i]]>ans[MAX])||(ans[a[i]]==ans[MAX]&&a[i]<MAX))
MAX=a[i];
}else{
for(int i=x;i<=r[bel[x]];++i)ans[a[i]]=0;
for(int i=l[bel[y]];i<=y;++i)ans[a[i]]=0;
for(int i=x;i<=r[bel[x]];++i)ans[a[i]]++;
for(int i=l[bel[y]];i<=y;++i)ans[a[i]]++;
MAX=most[bel[x]+1][bel[y]-1];
for(int i=x;i<=r[bel[x]];++i){
int pre=sum[bel[y]-1][MAX]-sum[bel[x]][MAX]+ans[MAX];
int X=ans[a[i]]+sum[bel[y]-1][a[i]]-sum[bel[x]][a[i]];
if((X>pre)||(X==pre&&a[i]<MAX))MAX=a[i];
}
for(int i=l[bel[y]];i<=y;++i){
int pre=sum[bel[y]-1][MAX]-sum[bel[x]][MAX]+ans[MAX];
int X=ans[a[i]]+sum[bel[y]-1][a[i]]-sum[bel[x]][a[i]];
if((X>pre)||(X==pre&&a[i]<MAX))MAX=a[i];
}
}
return MAX;
}
signed main(){
n=read(),m=read();
for(int i=1;i<=n;++i)t[i]=a[i]=read();
sort(t+1,t+n+1);siz=unique(t+1,t+n+1)-t-1;
for(int i=1;i<=n;++i)a[i]=lower_bound(t+1,t+siz+1,a[i])-t;
//ÀëÉ¢»¯
build();//·Ö¿é´¦Àí³öÑÕÉ«¸öÊý
for(int i=1,l,r,x=0;i<=m;++i){
l=(read()+x-1)%n+1,r=(read()+x-1)%n+1;
if(l>r)swap(l,r);
x=t[query(l,r)];
printf("%d\n",x);
}
}
麻了