Erina @ 2018-11-04 21:58:38
#include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
using namespace std;
int n, m;
int presum[205][40005], bes[205][205]; //前缀和 众数
int arr[100005]; //读入的数组
int mp[100005]; //离散化
int cnt[40005];
int bsiz, lst;
int belong(int x) { return x / bsiz; }//从0开始编号方便分块
int st(int x) { return x * bsiz; }//照着有图那篇题解的思想做的......
int ed(int x) { return st(x + 1) - 1; }
int lastans;
inline char gc()
{
static char bb[1000000], *s = bb, *t = bb;
return s == t && (t = (s = bb) + fread(bb, 1, 1000000, stdin), s == t) ? EOF : *s++;
}
inline int read()
{
int x = 0;
char ch = gc();
while (ch < 48)
ch = gc();
while (ch >= 48)
x = x * 10 + ch - 48, ch = gc();
return x;
}
int main()
{
n = read(), m = read();
bsiz = ceil(sqrt(n));
lst = belong(n - 1);
for (int i = 0; i < n; i++)
mp[i] = arr[i] = read();
sort(mp, mp + n);
int dsiz = unique(mp, mp + n) - mp;
for (int i = 0; i < n; i++)
arr[i] = lower_bound(mp, mp + dsiz, arr[i]) - mp;
for (int i = 0; i < n; i++)
{
if (i && (!(i % bsiz)))
for (int u = 0; u < dsiz; u++)
presum[i / bsiz - 1][u] = presum[lst][u];
presum[lst][arr[i]]++;
}
for (int i = 0; i <= lst; i++)
for (int u = i; u <= lst; u++)
{
memset(cnt, 0, sizeof(cnt));
int maxp = 0;
for (int j = st(i); j <= min(ed(u), n - 1); j++)
{
cnt[arr[j]]++;
if (cnt[maxp] == cnt[arr[j]])
maxp = min(maxp, arr[j]);
if (cnt[maxp] < cnt[arr[j]])
maxp = arr[j];
}
bes[i][u] = maxp;
}
for (int i = 0; i < m; i++)
{
int l = (read() + lastans - 1) % n, r = (read() + lastans - 1) % n; //l--,r--;
if (l > r)
swap(l, r);
if (belong(r) - belong(l) < 2)
{
int maxp = 0;
memset(cnt, 0, sizeof(cnt));
for (int u = l; u <= r; u++)
{
cnt[arr[u]]++;
if (cnt[arr[u]] == cnt[maxp])
maxp = min(maxp, arr[u]);
else if (cnt[arr[u]] > cnt[maxp])
maxp = arr[u];
}
lastans = mp[maxp];
cout << mp[maxp] << endl;
}
else
{
memset(cnt, 0, sizeof(cnt));
int bell = belong(l), belr = belong(r);
cnt[bes[bell + 1][belr - 1]] = presum[belr - 1][bes[bell + 1][belr - 1]] - presum[bell][bes[bell + 1][belr - 1]];
int maxp = 0;
for (int u = l; u <= ed(bell); u++)
{
if (!cnt[arr[u]])
cnt[arr[u]] = presum[belr - 1][arr[u]] - presum[bell][arr[u]];
cnt[arr[u]]++;
if (cnt[arr[u]] == cnt[maxp])
maxp = min(maxp, arr[u]);
else if (cnt[arr[u]] > cnt[maxp])
maxp = arr[u];
}
for (int u = st(belr); u <= r; u++)
{
if (!cnt[arr[u]])
cnt[arr[u]] = presum[belr - 1][arr[u]] - presum[bell][arr[u]];
cnt[arr[u]]++;
if (cnt[arr[u]] == cnt[maxp])
maxp = min(maxp, arr[u]);
else if (cnt[arr[u]] > cnt[maxp])
maxp = arr[u];
}
lastans = mp[maxp];
cout << mp[maxp] << endl;
}
}
}
自己的样例和其他人的样例都能过,求差错......