元夕 @ 2021-02-05 14:33:02
/*************************************************
Copyright: [email protected]
Author: Jack
*************************************************/
#include <bits/stdc++.h>
#define int long long
#define il inline
#define rg register
using namespace std;
il void chkmax(int &a, int b) {a = a > b ? a : b;}
il void chkmin(int &a, int b) {a = a < b ? a : b;}
il int read() {
int x = 0, f = 1; char c = getchar();
while(!isdigit(c)) {
if(c == '-') f = -f;
c = getchar();
}
while(isdigit(c)) {
x = (x << 1) + (x << 3) + c - '0';
c = getchar();
}
return x * f;
}
il void write(int x) {
char c[33] = {0}, tot = 0;
if(x == 0) {putchar('0'); return;}
while(x) {c[++ tot] = x % 10 + '0'; x /= 10;}
while(tot) {putchar(c[tot --]);}
return ;
}
const int maxn = 205;
int s, t, n, m, pub[maxn][maxn], fron[maxn][maxn * maxn];
int a[maxn * maxn], b[maxn * maxn];
map <int, int> rev;
void discrete() {
memcpy(b, a, sizeof(a));
sort(b + 1, b + 1 + n);
int N = unique(b + 1, b + 1 + n) - b - 1;
for(int i = 1; i <= n; i ++) {
int t = lower_bound(b + 1, b + 1 + N, a[i]) - b;
rev[t] = a[i]; a[i] = t;
}
}
int L[maxn], R[maxn], P[maxn * maxn];
int Count(int x, int l, int r) {
return fron[r][x] - fron[l - 1][x];
}
void build() {
t = sqrt(n);
for(int i = 1; i <= t; i ++)
L[i] = R[i - 1] + 1, R[i] = i * t;
if(R[t] < n) L[++ t] = R[t - 1] + 1, R[t] = n;
for(int i = 1; i <= t; i ++) {
for(int j = L[i]; j <= R[i]; j ++)
P[j] = i, fron[i][a[j]] ++;
for(int j = 1; j < maxn * maxn; j ++)
fron[i][j] += fron[i - 1][j];
}
for(int i = 1; i <= t; i ++) {
for(int j = i; j <= t; j ++) {
int Pub = pub[i][j - 1], num = Count(Pub, i, j - 1);
for(int k = L[j]; k <= R[j]; k ++) {
int Num = fron[j][a[k]] - fron[i - 1][a[k]];
if(Num > num || Num == num && a[k] < Pub) {
Pub = a[k]; num = Num;
}
}
pub[i][j] = Pub;
}
}
}
int cnt[maxn * maxn], Lans;
void brute(int l, int r) {
if(l > r) swap(l, r);
memset(cnt, 0, sizeof(cnt));
int id = 0, maxim = 0;
for(int i = l; i <= r; i ++) {
cnt[a[i]] ++;
if(cnt[a[i]] > maxim || cnt[a[i]] == maxim && a[i] < id) {
id = a[i];
maxim = cnt[a[i]];
}
}
printf("%lld\n", rev[id]); Lans = rev[id];
}
void solve(int x, int y) {
memset(cnt, 0, sizeof(cnt));
int Pans = pub[P[x] + 1][P[y] - 1];
int num = Count(Pans, (P[x] + 1), (P[y] - 1));
for(int i = x; i <= R[P[x]]; i ++)
cnt[a[i]] ++;
for(int i = L[P[y]]; i <= y; i ++)
cnt[a[i]] ++;
for(int i = x; i <= R[P[x]]; i ++) {
int newans = cnt[a[i]] + Count(a[i], P[x] + 1, P[y] - 1);
if(newans > num || (newans == num && Pans > a[i]))
Pans = a[i], num = newans;
}
for(int i = L[P[y]]; i <= y; i ++) {
int newans = cnt[a[i]] + Count(a[i], P[x] + 1, P[y] - 1);
if(newans > num || (newans == num && Pans > a[i]))
Pans = a[i], num = newans;
}
printf("%lld\n", rev[Pans]); Lans = rev[Pans];
}
signed main() {
// freopen("in.in", "r", stdin);
// freopen("out.out", "w", stdout);
cin >> n >> m;
for(int i = 1; i <= n; i ++) a[i] = read();
discrete(); build();
for(int i = 1, x, y; i <= m; i ++) {
x = read(), y = read();
x = (x + Lans - 1) % n + 1;
y = (y + Lans - 1) % n + 1;
if(x > y) swap(x, y);
if(P[y] - P[x] <= 1) brute(x, y);
else solve(x, y);
}
return 0;
}
by zixiangzhan @ 2022-02-15 15:36:37
我也遇到了……