kingofpupil @ 2019-04-13 17:53:47
WA声一片
#include<bits/stdc++.h>
#include<cctype>
using namespace std;
const int MAXN=40003;
const int SQRT=209;
int n,m,b[MAXN],pos[MAXN],ys[MAXN],sum[SQRT][MAXN],f[SQRT][SQRT],cnt[MAXN],ans;
//sum[i][j]保存前i块,第j种的数量
//f[i][j]是第i块..第j块的众数(离散化后的值)
//pos[i]是指第i个数在第几块
//ys[i]指排名第i的数
//b为离散化后的数组
inline char gt(){
static char buf[1000000],*p1=buf,*p2=buf;
return p1==p2&&(p2=(p1=buf)+fread(buf,1,1000000,stdin),p1==p2)?EOF:*p1++;
}
inline void read(int &ret){
ret=0; char ch=gt(); bool flg=0;
while(!isdigit(ch)) flg^=!(ch^'-'),ch=gt();
while(isdigit(ch)) ret=(ret<<3)+(ret<<1)+ch-48,ch=gt();
ret=flg?-ret:ret;
}
struct IN{int num,id;}a[MAXN];
inline bool cmp(IN x,IN y){return x.num<y.num;}
int main(){
scanf("%d%d",&n,&m);
// read(n), read(m);
for (int i=1;i<=n;i++) scanf("%d",&a[i].num),a[i].id=i;
// read(a[i].num),a[i].id=i;
sort(a+1,a+n+1,cmp);
int kkk=0;
for (int i=1;i<=n;i++){
if (a[i].num!=a[i-1].num) kkk++;
b[a[i].id]=kkk;
ys[kkk]=a[i].num;
}// 离散化,b就表示排名,ys[i]表示第i大的数是什么.
int p=1,len=sqrt(n)+1; //一块长度为len,共p块
for (int i=1;i<=n;i++){
if (i>p*len) p++;
sum[p][b[i]]++;
pos[i]=p;
}
for (int i=1;i<=p;i++)//这里求sum
for (int j=1;j<=kkk;j++) sum[i][j]+=sum[i-1][j];
for (int i=1;i<=p;i++){//这里求f
int k=0; //k是众数
for (int j=(i-1)*len+1;j<=n;j++){
cnt[b[j]]++;
if (cnt[b[j]]>cnt[k]) k=b[j]; else
if (cnt[b[j]]==cnt[k]) k=min(k,b[j]);
f[i][pos[j]]=k;
}
for (int j=(i-1)*len+1;j<=n;j++) cnt[b[j]]--;//清零
}
ans=0;
while (m--){
int l,r;
scanf("%d%d",&l,&r);
// read(l), read(r);
l=(l+ans-1)%n+1,r=(r+ans-1)%n+1;
if (l>r) swap(l,r);
int k=0;
if (pos[l]+1>=pos[r]){
for (int i=l;i<=r;i++){
cnt[b[i]]++;
if (cnt[b[i]]>cnt[k]) k=b[i]; else
if(cnt[b[i]]==cnt[k]) k=min(k,b[i]);
}
for (int i=l;i<=r;i++) cnt[b[i]]--;
} else{
k=f[pos[l]+1][pos[r]-1];//当前众数.
for (int i=l;i<=min(len*pos[l],n);i++){
cnt[b[i]]++;
int tot=cnt[b[i]]+sum[pos[r]-1][b[i]]-sum[pos[l]+1][b[i]];
if (tot>cnt[k]+sum[pos[r]-1][k]-sum[pos[l]+1][k]) k=b[i]; else
if (tot==cnt[k]+sum[pos[r]-1][k]-sum[pos[l]+1][k]) k=min(k,b[i]);
}
for (int i=(pos[r]-1)*len+1;i<=r;i++){
cnt[b[i]]++;
int tot=cnt[b[i]]+sum[pos[r]-1][b[i]]-sum[pos[l]+1][b[i]];
if (tot>cnt[k]+sum[pos[r]-1][k]-sum[pos[l]+1][k]) k=b[i]; else
if (tot==cnt[k]+sum[pos[r]-1][k]-sum[pos[l]+1][k]) k=min(k,b[i]);
}
//清零
for (int i=l;i<=min(len*pos[l],n);i++) cnt[b[i]]--;
for (int i=(pos[r]-1)*len+1;i<=r;i++) cnt[b[i]]--;
}
ans=ys[k]; ys[k]是排名第k的值
printf("%d\n",ans);
}
return 0;
}
by NKL丶 @ 2019-04-13 19:06:59
某变量名亮了
by kingofpupil @ 2019-04-13 19:12:02
@NKL丶炎 ?
by kingofpupil @ 2019-04-13 19:17:47
大佬们救救孩子吧..
by TRZ_2007 @ 2019-04-13 19:23:51
再《呐喊》,鲁迅就被你《呐喊》醒了
by kingofpupil @ 2019-04-13 19:52:57
找到错了,此贴终结