bzoj2724: [Violet 6]蒲公英
Description
Input
修正一下
l = (l_0 + x - 1) mod n + 1, r = (r_0 + x - 1) mod n + 1
Output
Sample Input
6 3
1 2 3 2 1 2
1 5
3 6
1 5
Sample Output
1
2
1
HINT
修正下:
n <= 40000, m <= 50000
分析
应该是分块的经典题了。维护区间众数。
先离散化一下。预处理两两块之间的答案。
答案应该是完整块内答案以及不完整块的所有数的答案。
对于不完整块的答案,我们对于每个数维护其出现的位置。
查询的时候lower_bound一下即可。
代码
/**************************************************************
Problem: 2724
User: 2014lvzelong
Language: C++
Result: Accepted
Time:16924 ms
Memory:4320 kb
****************************************************************/
#include<iostream>
#include<cstdlib>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<map>
#include<vector>
using namespace std;
const int N = 50005, M = 505;
int read() {
char ch = getchar(); int x = 0, f = 1;
while(ch < '0' || ch > '9') {if(ch == '-') f = -1; ch = getchar();}
while(ch >= '0' && ch <= '9') {x = x * 10 + ch - '0'; ch = getchar();}
return x * f;
}
int v[N], cnt[N], val[N], bl[N], f[M][M], blo, n, id;
map<int, int>mp;
vector<int>ve[N];
int Upd(int x, int t, int &ans, int &mx) {
if(t > mx || (t == mx && val[x] < val[ans])) ans = x, mx = t;
}
void pre(int x) {
memset(cnt, 0, sizeof(cnt));
int mx = 0, ans = 0;
for(int i = (x - 1) * blo + 1;i <= n; ++i) {
Upd(v[i], ++cnt[v[i]], ans, mx);
f[x][bl[i]] = ans;
}
}
int query(int x, int l, int r) {
return upper_bound(ve[x].begin(), ve[x].end(), r) - lower_bound(ve[x].begin(), ve[x].end(), l);
}
int query(int l, int r) {
int ans = f[bl[l] + 1][bl[r] - 1], mx;
mx = query(ans, l, r);
for(int i = min(bl[l] * blo, r); i >= l; --i)
Upd(v[i], query(v[i], l, r), ans, mx);
if(bl[l] != bl[r])
for(int i = (bl[r] - 1) * blo + 1; i <= r; ++i)
Upd(v[i], query(v[i], l, r), ans, mx);
return ans;
}
int main() {
n = read(); int m = read(); blo = 200;
int ans = 0;
for(int i = 1;i <= n; ++i) {
v[i] = read();
if(!mp[v[i]]) val[mp[v[i]] = ++id] = v[i];
ve[v[i] = mp[v[i]]].push_back(i);
}
for(int i = 1;i <= n; ++i) bl[i] = (i - 1) / blo + 1;
for(int i = 1;i <= bl[n]; ++i) pre(i);
while(m--) {
int l = (read() + ans - 1) % n + 1, r = (read() + ans - 1) % n + 1;
if(l > r) swap(l, r);
printf("%d\n", ans = val[query(l, r)]);
}
return 0;
}