1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
| #include <cstdio>
#include <cstring>
#include <cctype>
#include <cmath>
#include <algorithm>
#include <vector>
using namespace std;
//快读模版
namespace fast_io {
inline char read() {...}
inline void read(int &x) {...}
inline void read(char *a){...}
const int OUT_LEN = 1000000;
char obuf[OUT_LEN], *ooh = obuf;
inline void print(char c) {...}
inline void print(int x) {...}
inline void print(char *a){...}
inline void flush() {...}
}using namespace fast_io;
const int MAXN = 101000,MAXQ = 1000;
vector<int> pos[MAXN];
int n,m,Q;
struct pu{
int col,id;
}pgy[MAXN];
//在[i,j]块中的众数
int z[MAXQ][MAXQ];
int id_to[MAXN];
bool cmp1(pu a,pu b){
return a.col < b.col;
}
bool cmp2(pu a,pu b){
return a.id < b.id;
}
void init(){
read(n),read(m);Q = sqrt(n*5);
for(int i = 1;i<=n;i++)
read(pgy[i].col),pgy[i].id = i;
sort(pgy+1,pgy+n+1,cmp1);
int lastc = 0,numc = 0;
for(int i = 1;i<=n;i++){
if(pgy[i].col!=lastc)
numc++,id_to[numc] = pgy[i].col;
lastc = pgy[i].col;
pgy[i].col = numc;
}
sort(pgy+1,pgy+n+1,cmp2);
for(int i = 1;i<=n;i++){
pos[pgy[i].col].push_back(i);
}
}
void build(){
static int t[MAXN];
for(int i = 1;i<=n;i+=Q){
memset(t,0,sizeof(t));
int maxn = 0;
for(int j = i;j<=n;j++){
int nowc = pgy[j].col;
t[nowc]++;
if(t[nowc] > t[maxn] ||(t[nowc] == t[maxn] && nowc < maxn))
maxn = nowc;
if(j%Q == 0)
z[(i-1)/Q+1][j/Q] = maxn;
}
}
}
int count_num(int lb,int rb,int num){
return lower_bound(pos[num].begin(),pos[num].end(),rb+1)-lower_bound(pos[num].begin(),pos[num].end(),lb);
}
int answer(int ql,int qr){
int lb = floor(double(ql-2)/Q)+2,rb = qr/Q,maxn = 0,maxnum = 0;
if(lb <= rb) maxn = z[lb][rb],maxnum = count_num(ql,qr,maxn);
//printf("lblock:%d rblock:%d\n",lb,rb);
lb = (lb-1)*Q+1,rb = rb*Q;
//printf("lbound:%d rbound:%d maxn:%d\n",lb,rb,maxn);
while(ql < lb){
--lb;
int c = pgy[lb].col,w = count_num(ql,qr,c);
if(w > maxnum || (w == maxnum && c < maxn))
maxn = c,maxnum = w;
}
while(rb < qr){
rb++;
int c = pgy[rb].col,w = count_num(ql,qr,c);
if(w > maxnum || (w == maxnum && c < maxn))
maxn = c,maxnum = w;
}
return id_to[maxn];
}
void solve(){
int a,b,lastans = 0;
for(int i = 1;i<=m;i++){
read(a),read(b);
a = (a+lastans-1)%n+1,b = (b+lastans-1)%n+1;
if(a > b) swap(a,b);
lastans = answer(a,b);
print(lastans),print('\n');
}
}
int main(){
init();
build();
solve();
flush();
return 0;
}
|