# bzoj2724 [Violet 6]蒲公英

【题解】

# include <vector>
# include <stdio.h>
# include <string.h>
# include <algorithm>
// # include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
const int M = 4e4 + 10, N = 310, BLOCK = 200;
const int mod = 1e9+7;

# define RG register
# define ST static

int n, m, a[M], bl[M], B, q;
int ans[N][N];
int bst[N], bnd[N];
vector<int> ps, tt, g[M];
int t[M], mx, mxid, lst;

inline void prepare(int x) {
int tem = x;
mx = 0; mxid = 2333333;
for (int i=(x-1)*BLOCK+1; i<=n; ++i) {
t[a[i]] ++;
if(t[a[i]] > mx) {
mx = t[a[i]];
mxid = a[i];
} else if(t[a[i]] == mx) {
if(a[i] < mxid) mxid = a[i];
}
if(i == bnd[tem]) {
ans[x][tem] = mxid;
++tem;
}
}
for (int i=(x-1)*BLOCK+1; i<=n; ++i) --t[a[i]];
}

int main() {
scanf("%d%d", &n, &q);
for (int i=1; i<=n; ++i) {
scanf("%d", &a[i]);
ps.push_back(a[i]);
}
sort(ps.begin(), ps.end());
ps.erase(unique(ps.begin(), ps.end()), ps.end());
for (int i=1; i<=n; ++i) {
a[i] = lower_bound(ps.begin(), ps.end(), a[i]) - ps.begin() + 1;
//        printf("%d ", a[i]);
g[a[i]].push_back(i);
}
//    puts("\n================");
m = ps.size();
//    for (int i=1; i<=m; ++i) {
//        printf("==size = %d\n", g[i].size());
//        for (int j=0; j<g[i].size(); ++j) printf("%d ", g[i][j]);
//        puts("");
//    }
for (int i=1; i<=n; ++i) bl[i] = (i-1)/BLOCK+1;
B = bl[n];
for (int i=1; i<=B; ++i) bst[i] = (i-1)*BLOCK+1, bnd[i] = min(n, i*BLOCK);
for (int i=1; i<=B; ++i) prepare(i);
lst = 0;
while(q--) {
int l, r; scanf("%d%d", &l, &r);
l = (l+lst-1+n)%n+1;
r = (r+lst-1+n)%n+1;
if(l > r) swap(l, r);
if(bl[l] == bl[r]) {
mx = 0, mxid = 2333333;
for (int i=l; i<=r; ++i) {
t[a[i]] ++;
if(t[a[i]] > mx) {
mx = t[a[i]];
mxid = a[i];
} else if(t[a[i]] == mx) {
if(a[i] < mxid) mxid = a[i];
}
}
for (int i=l; i<=r; ++i) --t[a[i]];
printf("%d\n", lst = ps[mxid-1]);
} else {
tt.clear();
if(bl[l] + 1 != bl[r]) tt.push_back(ans[bl[l]+1][bl[r]-1]);
for (int i=l; i<=bnd[bl[l]]; ++i) tt.push_back(a[i]);
for (int i=bst[bl[r]]; i<=r; ++i) tt.push_back(a[i]);
sort(tt.begin(), tt.end());
tt.erase(unique(tt.begin(), tt.end()), tt.end());
//            for (int i=0, sz=tt.size(); i<sz; ++i) printf("%d ", tt[i]);
//            puts("\n======");
mx = 0, mxid = 2333333;
for (int i=0, sz=tt.size(); i<sz; ++i) {
int x = tt[i];
int xi = upper_bound(g[x].begin(), g[x].end(), r)-g[x].begin(), xj = lower_bound(g[x].begin(), g[x].end(), l)-g[x].begin()+1;
//                printf("l = %d, r = %d\n", xj, xi);
int times = upper_bound(g[x].begin(), g[x].end(), r)-lower_bound(g[x].begin(), g[x].end(), l);
//                printf("x=%d, times=%d\n", tt[i], times);
if(times > mx) mx = times, mxid = x;
}
printf("%d\n", lst = ps[mxid-1]);
}
}
return 0;
}
View Code

posted @ 2017-05-03 16:58  Galaxies  阅读(419)  评论(0编辑  收藏  举报