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
|
#include <cstdio>
#include <algorithm>
#include <map>
using namespace std;
const int MAXN = 200002;
struct node {
int sum, l, r;
} hjt[MAXN*40];
int a[MAXN], root[MAXN];
int cnt;
map<int, int> mp;
void Init() {
cnt = 0;
mp.clear();
}
inline int CreateNode(int sum, int l, int r) {
int idx = ++cnt;
hjt[idx].sum = sum;
hjt[idx].l = l;
hjt[idx].r = r;
return idx;
}
void Insert(int &root, int pre_rt, int pos, int v, int l, int r) {
root = CreateNode(hjt[pre_rt].sum+v, hjt[pre_rt].l, hjt[pre_rt].r);
if(l == r) return;
int m = (l+r) >> 1;
if(pos <= m)
Insert(hjt[root].l, hjt[pre_rt].l, pos, v, l, m);
else Insert(hjt[root].r, hjt[pre_rt].r, pos, v, m+1, r);
}
int QueryNum(int s, int R, int l, int r) {
if(r <= R) return hjt[s].sum;
int m = (l+r) >> 1;
if(R <= m)
return QueryNum(hjt[s].l, R, l, m);
else return hjt[hjt[s].l].sum + QueryNum(hjt[s].r, R, m+1, r);
}
int QueryKth(int s, int k, int l, int r) {
if(l == r) return l;
int m = (l+r) >> 1;
int sum = hjt[hjt[s].l].sum;
if(k <= sum)
return QueryKth(hjt[s].l, k, l, m);
else return QueryKth(hjt[s].r, k-sum, m+1, r);
}
int main(int argc, char const *argv[]) {
int t, n, m, l, r, tmp;
scanf("%d", &t);
for(int cs=1; cs<=t; ++cs) {
Init();
scanf("%d %d", &n, &m);
for(int i=1; i<=n; ++i) {
scanf("%d", &a[i]);
}
for(int i=n; i>=1; --i) {
if(!mp[a[i]])
Insert(root[i], root[i+1], i, 1, 1, n);
else {
Insert(tmp, root[i+1], mp[a[i]], -1, 1, n);
Insert(root[i], tmp, i, 1, 1, n);
}
mp[a[i]] = i;
}
printf("Case #%d:", cs);
int ans = 0;
for(int i=0; i<m; ++i) {
scanf("%d %d", &l, &r);
l = (l+ans)%n + 1;
r = (r+ans)%n + 1;
if(r < l) swap(l, r);
int k = (QueryNum(root[l], r, 1, n)+1) / 2;
ans = QueryKth(root[l], k, 1, n);
printf(" %d", ans);
}
printf("\n");
}
return 0;
}
|