small_john @ 2024-02-17 19:22:03
rt,大块出的问题
#include <bits/stdc++.h>
using namespace std;
template<typename T> inline void read(T &x)
{
x = 0;
T f = 1;char ch = getchar();
while(ch<'0'||ch>'9')
{
if(ch=='-')
{
f = -1,ch = getchar();
break;
}
ch = getchar();
}
while(ch>='0'&&ch<='9')
x = (x<<3)+(x<<1)+ch-48,ch = getchar();
x*=f;
}
template<typename T = int> inline T read()
{
T x;read(x);return x;
}
template<typename T> void write(T x)
{
if(x<0) x = -x,putchar('-');
if(x>9) write(x/10);
putchar(x%10+48);
}
template<typename T> inline void writen(T x)
{
write(x);
putchar(10);
}
const int N = 4e4+5,B = 200,C = 205;
int n,m,a[N],kk[N],tt,b[N],lt[C],rt[C],cnt[C][N],num[C][C];
int sum[N];
inline void init()
{
for(int i = 1;i<=n;i++)
b[i] = (i-1)/B+1;
for(int i = 1;i<=b[n];i++)
lt[i] = rt[i-1]+1,rt[i] = lt[i]+B-1;
rt[b[n]] = n;
for(int i = 1;i<=b[n];i++)
{
cnt[i][0] = -1;
for(int j = lt[i];j<=rt[i];j++) cnt[i][a[j]]++;
for(int j = 1;j<=n;j++) cnt[i][j]+=cnt[i-1][j];
}
for(int i = 1;i<=b[n];i++)
{
for(int j = i;j<=b[n];j++)
for(int k = lt[j];k<=rt[j];k++)
{
sum[a[k]]++;
if(sum[a[k]]>sum[num[i][j]]||(sum[a[k]]==sum[num[i][j]]&&a[k]<num[i][j])) num[i][j] = a[k];
}
memset(sum,0,sizeof sum);
}
}
inline int ask(int l,int r)
{
// memset(sum,0,sizeof sum);
int res = 0;
sum[res] = -114514;
if(b[r]-b[l]<=1)
{
for(int i = l;i<=r;i++)
{
sum[a[i]]++;
if(sum[a[i]]>sum[res]||(sum[a[i]]==sum[res]&&a[i]<res)) res = a[i];
}
for(int i = l;i<=r;i++) sum[a[i]] = 0;
return kk[res];
}
res = num[b[l]+1][b[r]-1];
for(int i = l;i<=rt[b[l]];i++)
sum[a[i]]++;
for(int i = lt[b[r]];i<=r;i++)
sum[a[i]]++;
for(int i = l;i<=rt[b[l]];i++)
{
int x = sum[a[i]]+cnt[b[r]-1][a[i]]-cnt[b[l]][a[i]],y = sum[res]+cnt[b[r]-1][res]-cnt[b[l]][res];
if(x>y||(x==y&&a[i]<res)) res = a[i];
}
for(int i = lt[b[r]];i<=r;i++)
{
int x = sum[a[i]]+cnt[b[r]-1][a[i]]-cnt[b[l]][a[i]],y = sum[res]+cnt[b[r]-1][res]-cnt[b[l]][res];
if(x>y||(x==y&&a[i]<res)) res = a[i];
}
for(int i = l;i<=rt[b[l]];i++) sum[a[i]] = 0;
for(int i = lt[b[r]];i<=r;i++) sum[a[i]] = 0;
return kk[res];
}
signed main()
{
// freopen("1.in","r",stdin);
// freopen("1.out","w",stdout);
read(n),read(m);
for(int i = 1;i<=n;i++)
read(a[i]),kk[++tt] = a[i];
sort(kk+1,kk+tt+1),tt = unique(kk+1,kk+tt+1)-kk-1;
for(int i = 1;i<=n;i++)
a[i] = lower_bound(kk+1,kk+tt+1,a[i])-kk;
init();
int las = 0;
while(m--)
{
int l,r;
read(l),read(r);
l = (l+las-1)%n+1,r = (r+las-1)%n+1;
if(l>r) swap(l,r);
writen(las = ask(l,r));
}
return 0;
}
by small_john @ 2024-02-17 19:34:30
已 AC,预处理出错。警示后人,记得初始化
by Adchory @ 2024-02-17 19:45:53
@pyy1 还在卷????????
by small_john @ 2024-02-17 19:49:33
@MoriyaSuwako 喜欢卷