http://www.docin.com/p-679227660.html
可以用冰点文库下载..
用分块+可持久化线段树统计答案..
有点毒瘤..
#include <bits/stdc++.h>
using namespace std;
#define N 40005
struct lisan
{
int x,y;
}b[N];
bool cmpli(lisan x,lisan y)
{
return x.x<y.x;
}
struct tree
{
int l,r,s;
}tr[666666];
int tottr,rt[N];
int a[N],to[N];
int lb[N],rb[N],f[2005][2005];
int n,block,totblock,ans;
void ins(int &k,int l,int r,int x)
{
tr[++tottr]=tr[k];
k=tottr;
++tr[k].s;
if (l==r) return;
int mid=(l+r)>>1;
if (x<=mid) ins(tr[k].l,l,mid,x);else ins(tr[k].r,mid+1,r,x);
}
int query(int x,int y,int l,int r,int t)
{
if (l==r) return tr[y].s-tr[x].s;
int mid=(l+r)>>1;
if (t<=mid) return query(tr[x].l,tr[y].l,l,mid,t);
return query(tr[x].r,tr[y].r,mid+1,r,t);
}
inline int calc(int x,int y,int t)
{
return query(rt[x-1],rt[y],1,n,t);
}
inline void solve()
{
int l,r,blol=1,blor=totblock,maxx;
scanf("%d%d",&l,&r);
l=(l+ans-1)%n+1;r=(r+ans-1)%n+1;
if (l>r) swap(l,r);
if (r-l+1<=2*block)
{
maxx=calc(l,r,a[l]);
ans=a[l];
for (int i=l+1;i<=r;++i)
{
int t=calc(l,r,a[i]);
if (t>maxx || t==maxx && a[i]<ans) maxx=t,ans=a[i];
}
ans=to[ans];
printf("%d\n",ans);
return;
}
while (lb[blol]<l) ++blol;
while (rb[blor]>r) --blor;
ans=f[blol][blor];
maxx=calc(l,r,ans);
for (int i=l;i<lb[blol];++i)
{
int t=calc(l,r,a[i]);
if (t>maxx || t==maxx && a[i]<ans) maxx=t,ans=a[i];
}
for (int i=rb[blor]+1;i<=r;++i)
{
int t=calc(l,r,a[i]);
if (t>maxx || t==maxx && a[i]<ans) maxx=t,ans=a[i];
}
ans=to[ans];
printf("%d\n",ans);
}
int buc[N];
int main()
{
int Q;
cin>>n>>Q;block=200;totblock=(n-1)/block+1;
for (int i=1;i<=n;++i) scanf("%d",&b[i].x),b[i].y=i;
sort(b+1,b+1+n,cmpli);int t=0;for (int i=1;i<=n;++i) {if (b[i].x!=b[i-1].x || i==1) ++t;a[b[i].y]=t;to[t]=b[i].x;}
//for (int i=1;i<=n;++i) cout<<a[i]<<' ';cout<<endl;
for (int i=1;i<=n;++i) rt[i]=rt[i-1],ins(rt[i],1,n,a[i]);
//cout<<"block="<<block<<' '<<totblock<<endl;
for (int blo=1;blo<=totblock;++blo) lb[blo]=rb[blo-1]+1,rb[blo]=min(n,lb[blo]+block-1);
//for (int i=1;i<=totblock;++i) cout<<"lbrb="<<lb[i]<<' '<<rb[i]<<endl;
for (int i=1;i<=totblock;++i)
{
for (int j=1;j<=n;++j) buc[j]=0;
for (int j=i;j<=totblock;++j)
{
for (int k=lb[j];k<=rb[j];++k) ++buc[a[k]];
int x=lb[i],y=rb[j],t;
int maxx=calc(x,y,a[lb[j]]);
f[i][j]=a[lb[j]];
for (int k=lb[j]+1;k<=rb[j];++k)
{
t=buc[a[k]];
if (t>maxx || t==maxx && a[k]<f[i][j]) maxx=t,f[i][j]=a[k];
}
if (i<j)
{
t=calc(x,y,f[i][j-1]);
if (t>maxx || t==maxx && f[i][j-1]<f[i][j]) maxx=t,f[i][j]=f[i][j-1];
}
//cout<<"ij="<<i<<' '<<j<<' '<<f[i][j]<<" xy="<<x<<' '<<y<<" mx="<<maxx<<endl;
}
}
while (Q--) solve();
}