#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
#include <vector>
#include <cmath>
#include <map>
using namespace std;
int n, m, bel[40005], a[40005], blc, f[1005][1005], cnt[40005], idx, ans;
int val[40005], uu, vv;
map<int,int> mp;
vector<int> vec[40005];
int mycnt(int uu, int vv, int ww){
return upper_bound(vec[ww].begin(), vec[ww].end(), vv)-lower_bound(vec[ww].begin(), vec[ww].end(), uu);
}
int query(int uu, int vv){
int tmpmax=f[bel[uu]+1][bel[vv]-1], tmpcnt=mycnt(uu, vv, tmpmax);
if(bel[uu]==bel[vv]){
for(int i=uu; i<=vv; i++){
int t=mycnt(uu, vv, a[i]);
if(t>tmpcnt || (t==tmpcnt && a[i]<tmpmax)){
tmpmax = a[i];
tmpcnt = t;
}
}
}
else{
for(int i=uu; i<=bel[uu]*blc; i++){
int t=mycnt(uu, vv, a[i]);
if(t>tmpcnt || (t==tmpcnt && a[i]<tmpmax)){
tmpmax = a[i];
tmpcnt = t;
}
}
for(int i=(bel[vv]-1)*blc+1; i<=vv; i++){
int t=mycnt(uu, vv, a[i]);
if(t>tmpcnt || (t==tmpcnt && a[i]<tmpmax)){
tmpmax = a[i];
tmpcnt = t;
}
}
}
return tmpmax;
}
int main(){
cin>>n>>m;
blc = sqrt(n*log(2)/log(n));
for(int i=1; i<=n; i++){
scanf("%d", &a[i]);
val[++idx] = a[i];
bel[i] = (i - 1) / blc + 1;
}
sort(val+1, val+1+n);
idx = unique(val+1, val+1+idx) - val - 1;
for(int i=1; i<=n; i++){
a[i] = lower_bound(val+1, val+1+idx, a[i]) - val;
vec[a[i]].push_back(i);
}
for(int i=1; i<=n; i=bel[i]*blc+1){
memset(cnt, 0, sizeof(cnt));
int tmpmax=0, tmpcnt=0;
for(int j=i; j<=n; j++){
cnt[a[j]]++;
if(cnt[a[j]]>tmpcnt || (cnt[a[j]]==tmpcnt && a[j]<tmpmax)){
tmpmax = a[j];
tmpcnt = cnt[a[j]];
}
f[bel[i]][bel[j]] = tmpmax;
}
}
while(m--){
scanf("%d %d", &uu, &vv);
uu = (uu + ans - 1) % n + 1;
vv = (vv + ans - 1) % n + 1;
if(uu>vv) swap(uu, vv);
ans = val[query(uu, vv)];
printf("%d\n", ans);
}
return 0;
}

最新文章

  1. node fs lstat 如何区别文件和文件夹
  2. linux命令格式及基础命令(一)
  3. CentOS 7下源码安装MySQL 5.6
  4. 【zepto学习笔记02】零碎点
  5. Decimal To Fraction 小数转换成分数
  6. Code(组合数学)
  7. :Hibernate逍遥游记-第16管理session和实现对话
  8. mysql计算连续天数,mysql连续登录天数,连续天数统计
  9. Print! Print! Print!
  10. arcpy.mapping常用四大件-MapsurroundElement
  11. JAVA常用集合源码解析系列-ArrayList源码解析(基于JDK8)
  12. iOS 可高度自定义的底部弹框
  13. 使用docker redis-cluster集群搭建
  14. java script入门之知识
  15. .NET跨平台实践:再谈用C#开发Linux守护进程 — 完整篇
  16. PowerBuilder编程新思维5:包装(界面美化与WebUI+React)
  17. JS参差不齐的数组
  18. 【转】Ubuntu12.04 LTS下环境变量设置
  19. 使用base64转码的方式上传图片
  20. Spring 工作流程简单介绍

热门文章

  1. git导出代码
  2. hibernate Day2
  3. HttpURLConnection 发送PUT请求 json请求体 与服务端接收
  4. 基于python的request库,模拟登录csdn博客
  5. C. Arcade dp二维费用背包 + 滚动数组 玄学
  6. Ionic开发-如何在ion-content形成上下结构 上面固定下层可滚动
  7. Docker容器相关技术
  8. 移除sql数据所有链接用户
  9. 1   开发一个注重性能的JDBC应用程序不是一件容易的事. 当你的代码运行很慢的时候JDBC驱动程序并不会抛出异常告诉你。   本系列的性能提示将为改善JDBC应用程序的性能介绍一些基本的指导原则,这其中的原则已经被许多现有的JDBC应用程序编译运行并验证过。 这些指导原则包括:    正确的使用数据库MetaData方法    只获取需要的数据    选用最佳性能的功能    管理连
  10. JavaScript 的垃圾回收与内存泄露