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
| #include <bits/stdc++.h> using namespace std; const int N = 4e4 + 5, B = 250; int n, m, a[N], v[N], tot; int f[B][B], g[B][B], cnt[N]; int blo, num, bl[N], l[B], r[B], pos[N]; vector<int> p[N]; void lsh() { static int tmp[N]; for(int i=1; i<=n; i++) tmp[i] = a[i]; sort(tmp+1, tmp+n+1); tot = unique(tmp+1, tmp+n+1) - tmp - 1; for(int i=1; i<=n; i++) { int pre = a[i]; a[i] = lower_bound(tmp+1, tmp+tot+1, a[i]) - tmp; v[a[i]] = pre; } } void build(int x) { int res = 0; for(int i=1; i<=tot; i++) cnt[i] = 0; for(int y=x; y<=num; y++) { for(int i=l[y]; i<=r[y]; i++) { int cur = a[i]; cnt[cur]++; if(!res || cnt[cur]>cnt[res] || (cnt[cur]==cnt[res] && cur<res)) { res = cur; } } f[x][y] = res; g[x][y] = cnt[res]; } } void init() { blo = sqrt(n); num = (n+blo-1)/blo; for(int i=1; i<=n; i++) bl[i] = (i-1)/blo + 1; for(int i=1; i<=num; i++) l[i] = (i-1)*blo + 1, r[i] = i*blo; r[num] = n; for(int i=1; i<=num; i++) build(i); for(int i=1; i<=n; i++) { p[a[i]].push_back(i); pos[i] = (int)p[a[i]].size() - 1; } } int qry(int x, int y) { int res = 0, cnt = 0; if(bl[x]+1<=bl[y]-1) res = f[bl[x]+1][bl[y]-1], cnt = g[bl[x]+1][bl[y]-1]; for(int i=x; i<=min(r[bl[x]], y); i++) { int nxt = (a[i]<res ? cnt : cnt+1); while(pos[i]+nxt-1<(int)p[a[i]].size() && p[a[i]][pos[i]+nxt-1]<=y) { res = a[i]; cnt = nxt++; } } if(bl[x]!=bl[y]) { for(int i=l[bl[y]]; i<=y; i++) { int nxt = (a[i]<res ? cnt : cnt+1); while(pos[i]-nxt+1>=0 && p[a[i]][pos[i]-nxt+1]>=x) { res = a[i]; cnt = nxt++; } } } return res; } int main() { scanf("%d%d", &n, &m); for(int i=1; i<=n; i++) scanf("%d", a+i); lsh(); init(); int lstans = 0; while(m--) { int l, r; scanf("%d%d", &l, &r); l = (l + lstans - 1)%n + 1, r = (r + lstans - 1)%n + 1; if(l>r) swap(l, r); printf("%d\n", lstans = v[qry(l, r)]); } return 0; }
|