Griseo_nya @ 2021-09-29 17:24:09
其实这个原来是我5048的代码,改了一下
查不出错啊求大佬帮忙查错(
#include<bits/stdc++.h>
using namespace std;
#ifdef ONLINE_JUDGE
#define getchar()(p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
char buf[1<<21],*p1=buf,*p2=buf;
#endif
//#define int long long
template<typename T>inline void redi(T& ret) {
ret=0;T f=1;char ch=getchar();
while (ch<'0'||ch>'9') {if (ch=='-') f=-f;ch=getchar();}
while (ch>='0'&&ch<='9') ret=ret*10+ch-'0',ch=getchar();
ret*=f;
}
template <typename T,typename... Args> inline void redi(T& t, Args&... args)
{
redi(t);redi(args...);
}
char buffer[1<<21];
int p11=-1;
const int p22=(1<<21)-1;
inline void flush(){
fwrite(buffer,1,p11+1,stdout),p11=-1;
}
inline void putc(const char &x){
if (p11==p22)flush();
buffer[++p11]=x;
}
template<typename T>inline void wrtn(T x){
static char buf[15];
static T len=-1;
if (x>=0){
do{
buf[++len]=x%10+48,x/=10;
}while(x);
} else{
putc('-');
do{
buf[++len]=-(x%10)+48,x/=10;
}while(x);
}
while(len>=0){
putc(buf[len]),--len;
}
}
template<typename T>inline void wrti(T x){
wrtn(x),putc('\n');
}
const int bs=300, maxn=4e4+10;
int pl[maxn],a[maxn],tot,m[maxn],L[bs],R[bs],qu[bs][bs],la,n,q,cnt[maxn],cl[bs][bs];
bool fnd[maxn];
vector<int>v[maxn];
inline int getpos(int i){
return (i-1)/bs+1;
}
int query(int l,int r){
int res=0,ans=1e9;
int Lb=getpos(l),Rb=getpos(r);
if(Lb==Rb){
for(int i=l;i<=r;i++){
cnt[a[i]]=0;
}
for(int i=l;i<=r;i++){
++cnt[a[i]];
if(cnt[a[i]]>res)res=cnt[a[i]],ans=a[i];
else if(cnt[a[i]]==res)ans=min(ans,a[i]);
}
} else{
memset(fnd,0,sizeof fnd);
res=qu[Lb+1][Rb-1];ans=cl[Lb+1][Rb-1];
for(int i=l;i<=R[Lb];i++){
if(fnd[a[i]])continue;
fnd[a[i]]=1;
int ak=lower_bound(v[a[i]].begin(),v[a[i]].end(),r)-lower_bound(v[a[i]].begin(),v[a[i]].end(),l)+1;
if(ak>res)res=ak,ans=a[i];
else if(ak==res)ans=min(ans,a[i]);
}
for(int i=L[Rb];i<=r;i++){
if(fnd[a[i]])continue;
fnd[a[i]]=1;
int ak=lower_bound(v[a[i]].begin(),v[a[i]].end(),r)-lower_bound(v[a[i]].begin(),v[a[i]].end(),l)+1;
if(ak>res)res=ak,ans=a[i];
else if(ak==res)ans=min(ans,a[i]);
}
}
return ans;
}
signed main(){
redi(n,q);
tot=getpos(n);
for(int i=1;i<=n;i++){
redi(a[i]);
m[i]=a[i];
}
sort(m+1,m+1+n);
int u=unique(m+1,m+1+n)-m-1;
for(int i=1;i<=n;i++){
a[i]=lower_bound(m+1,m+u+1,a[i])-m;
}
for(int i=1;i<=n;i++){
v[a[i]].emplace_back(i);
pl[i]=v[a[i]].size()-1;
}
for(int i=1;i<=tot;i++){
L[i]=R[i-1]+1;
R[i]=i*bs;
}
R[tot]=n;
memset(cl,0x3f,sizeof cl);
for(int i=1;i<=tot;i++){
memset(cnt,0,sizeof cnt);
for(int j=i;j<=tot;j++){
qu[i][j]=qu[i][j-1];cl[i][j]=cl[i][j-1];
for(int k=L[j];k<=R[j];k++){
++cnt[a[k]];
if(qu[i][j]<cnt[a[k]])qu[i][j]=cnt[a[k]],cl[i][j]=a[k];
else if(qu[i][j]==cnt[a[k]])cl[i][j]=min(cl[i][j],a[k]);
}
}
}
while(q--){
int l,r;
redi(l,r);
l=((l+la-1)%n)+1,r=((r+la-1)%n)+1;if(l>r)swap(l,r);
int ans=m[query(l,r)];
wrti(ans);
la=ans;
}
flush();
return 0;
}