记录编号 251118 评测结果 WWWWWWWWWWWWWWWWWWWW
题目名称 数字查询 最终得分 0
用户昵称 Gravatar咸鱼二号 是否通过 未通过
代码语言 C++ 运行时间 6.017 s
提交时间 2016-04-16 21:38:34 内存使用 65.33 MiB
显示代码纯文本
  1. #include<iostream>
  2. #include<cstring>
  3. #include<cstdio>
  4. #include<string>
  5. #include<algorithm>
  6. #include<cmath>
  7. #include<utility>
  8. #include<cstdlib>
  9. #include<iomanip> //cout<<setiosflags(ios::fixed)<<setprecision(2);
  10. #include<ctime> //double a=(double)clock(); cout<<a<<endl;
  11. #include<vector>
  12. #include<queue>
  13. using namespace std;
  14. const int maxn=40010;
  15. inline int read(){
  16. int x=0,ff=1;char ch=getchar();
  17. while(ch>'9'||ch<'0'){if(ch=='-')ff=-1; ch=getchar();}
  18. while(ch<='9'&&ch>='0'){x=x*10+ch-'0';ch=getchar();}
  19. return ff*x;
  20. }
  21. inline int mymin(int xx,int yy)
  22. {if(xx<yy)return xx;return yy;}
  23. inline int mymax(int xx,int yy)
  24. {if(xx<yy)return yy;return xx;}
  25. inline void myswap(int &xx,int &yy)
  26. {xx^=yy,yy^=xx,xx^=yy;}
  27. int N,M,cnt=0,len=200,block,L,R,x=0,maxx,now,k;
  28. int a[maxn],s[210][40010],ans[210][40010],vis[40010],p[40010],check[40010],tim;
  29. struct stru{
  30. int id,v;
  31. }c[maxn];
  32. inline bool mycmp1(const stru &A,const stru &B)
  33. {return A.id<B.id;}
  34. inline bool mycmp2(const stru &A,const stru &B)
  35. {return A.v<B.v;}
  36. void pre(){
  37. //len=(int)sqrt(1.0*N);
  38. k=N/len;
  39. if(N%len)k++;
  40. for(int i=1;i<=k;i++){
  41. for(int j=1;j<=cnt;j++)
  42. s[i][j]=s[i-1][j];
  43. L=(i-1)*len+1,R=mymin(i*len,N);
  44. for(int j=L;j<=R;j++)
  45. s[i][c[j].v]++;
  46. }
  47. for(int i=1;i<=k;i++){
  48. memset(vis,0,sizeof(vis));
  49. maxx=0,now=cnt+1;
  50. for(int j=i;j<=k;j++){
  51. L=(j-1)*len+1,R=mymin(i*len,N);
  52. for(int o=L;o<=R;o++){
  53. vis[c[o].v]++;
  54. if(vis[c[o].v]>maxx||(vis[c[o].v]==maxx&&c[o].v<now))
  55. maxx=vis[c[o].v],now=c[o].v;
  56. }
  57. ans[i][j]=now;
  58. }
  59. }
  60. }
  61. void query(int X,int Y){
  62. int A=(X-1)/len+1,B=(Y-1)/len+1;
  63. tim++;
  64. if(A==B||A==B-1){
  65. maxx=0,now=cnt+1;
  66. for(int i=X;i<=Y;i++){
  67. if(check[c[i].v]!=tim)check[c[i].v]=tim,vis[c[i].v]=0;
  68. vis[c[i].v]++;
  69. if(vis[c[i].v]>maxx||(vis[c[i].v]==maxx&&now>c[i].v))
  70. maxx=vis[c[i].v],now=c[i].v;
  71. }
  72. }
  73. else{
  74. now=ans[A+1][B-1];
  75. maxx=s[B-1][now]-s[A][now];//不计算L和R
  76. L=A*len,R=(B-1)*len+1;
  77. for(int i=X;i<=L;i++){
  78. if(check[c[i].v]!=tim)check[c[i].v]=tim,vis[c[i].v]=0;
  79. vis[c[i].v]++;
  80. }
  81. for(int i=R;i<=Y;i++){
  82. if(check[c[i].v]!=tim)check[c[i].v]=tim,vis[c[i].v]=0;
  83. vis[c[i].v]++;
  84. }
  85. for(int i=X;i<=L;i++){
  86. int cur=vis[c[i].v]+s[B-1][c[i].v]-s[A][c[i].v];
  87. if(cur>maxx||(cur==maxx&&now>c[i].v))
  88. maxx=cur,now=c[i].v;
  89. }
  90. for(int i=R;i<=Y;i++){
  91. int cur=vis[c[i].v]+s[B-1][c[i].v]-s[A][c[i].v];
  92. if(cur>maxx||(cur==maxx&&now>c[i].v))
  93. maxx=cur,now=c[i].v;
  94. }
  95. }
  96. x=p[c[now].v];
  97. printf("%d\n",x);
  98. }
  99. int main(){
  100. freopen("numquery.in","r",stdin);
  101. freopen("numquery.out","w",stdout);
  102. N=read(),M=read();
  103. for(int i=1;i<=N;i++)
  104. c[i].v=a[i]=read(),c[i].id=i;
  105. sort(c+1,c+1+N,mycmp2);
  106. for(int i=1;i<=N;i++){
  107. if(c[i].v!=c[i-1].v)
  108. c[i].v=++cnt;
  109. else c[i].v=cnt;
  110. p[cnt]=c[i].v;
  111. }
  112. sort(c+1,c+1+N,mycmp1);
  113. pre();
  114. while(M--){
  115. L=read(),R=read();
  116. L=(L+x-1)%N+1,R=(R+x-1)%N+1;
  117. if(L>R)
  118. myswap(L,R);
  119. query(L,R);
  120. }
  121. return 0;
  122. }