yaoxi-std 的博客

$\text{开}\mathop{\text{卷}}\limits^{ju\check{a}n}\text{有益}$

0%

P5048 [Ynoi2019 模拟赛] Yuno loves sqrt technology III

P5048 [Ynoi2019 模拟赛] Yuno loves sqrt technology III

题面

题目链接

解法

区间众数的新 trick !

首先离散化后用 vector 储存每个数的出现位置,并且 $id[i]$ 表示 $a[i]$ 在 $vec[a[i]]$ 中的哪个位置。

假设已经处理出 $dp[x][y]$ 表示块 $x$ 到块 $y$ 之间的众数出现次数。考虑一次询问中向两边拓展,枚举左右的散块,以右边的散块为例(左边同理),枚举到 $i$ 时,如果 $a[i]$ 在 $[l,r]$ 中的出现次数 $> ans$,则有 $vec[a[i]][id[i]-ans] \in [l,r]$。所以可以暴力将 $ans$ 加一。

这样一来,也就知道了 $dp$ 数组的求法了。所以能够在 $O(n \sqrt n)$ 的时间复杂度内以及 $O(n)$ 的空间复杂度求出静态区间众数个数了。

AC代码

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
/**
* @file: P5048.cpp
* @author: yaoxi-std
* @url: https://www.luogu.com.cn/problem/P5048
*/
// #pragma GCC optimize ("O2")
// #pragma GCC optimize ("Ofast", "inline", "-ffast-math")
// #pragma GCC target ("avx,sse2,sse3,sse4,mmx")
#include <bits/stdc++.h>
using namespace std;
#define resetIO(x) \
freopen(#x ".in", "r", stdin), freopen(#x ".out", "w", stdout)
#define debug(fmt, ...) \
fprintf(stderr, "[%s:%d] " fmt "\n", __FILE__, __LINE__, ##__VA_ARGS__)
template <class _Tp>
inline _Tp& read(_Tp& x) {
bool sign = false; char ch = getchar();
for (; !isdigit(ch); ch = getchar()) sign |= (ch == '-');
for (x = 0; isdigit(ch); ch = getchar()) x = x * 10 + (ch ^ 48);
return sign ? (x = -x) : x;
}
template <class _Tp>
inline void write(_Tp x) {
if (x < 0) putchar('-'), x = -x;
if (x > 9) write(x / 10);
putchar((x % 10) ^ 48);
}
bool m_be;
using ll = long long;
const int MAXN = 5e5 + 10;
const int MAXB = 710;
const int INF = 0x3f3f3f3f;
int n, m, q, a[MAXN], id[MAXN], val[MAXN], dp[MAXB][MAXB];
int blk, bn, bl[MAXB], br[MAXB], pos[MAXN];
vector<int> vec[MAXN];
inline bool inseq(int k, int p, int l, int r) {
return p >= 0 && p < vec[k].size() && l <= vec[k][p] && vec[k][p] <= r;
}
bool m_ed;
signed main() {
// debug("Mem %.5lfMB.", fabs(&m_ed - &m_be) / 1048576);
read(n), read(q), blk = ceil(sqrt(n));
for (int i = 1; i <= n; ++i) read(a[i]), val[i] = a[i];
sort(val + 1, val + n + 1);
m = unique(val + 1, val + n + 1) - val - 1;
for (int i = 1; i <= n; ++i) {
a[i] = lower_bound(val + 1, val + m + 1, a[i]) - val;
vec[a[i]].push_back(i), id[i] = vec[a[i]].size() - 1;
}
for (int l = 1; l <= n; l += blk) {
int r = min(n, l + blk - 1);
++bn, bl[bn] = l, br[bn] = r;
for (int i = l; i <= r; ++i) pos[i] = bn;
}
for (int i = 1; i <= bn; ++i)
for (int j = i; j <= bn; ++j) {
dp[i][j] = dp[i][j - 1];
for (int k = bl[j]; k <= br[j]; ++k)
while (inseq(a[k], id[k] - dp[i][j], bl[i], br[j])) ++dp[i][j];
}
int ans = 0;
while (q--) {
int l, r; read(l), read(r);
l ^= ans, r ^= ans, ans = 0;
if (pos[l] == pos[r]) {
for (int i = l; i <= r; ++i)
while (inseq(a[i], id[i] - ans, l, r)) ++ans;
} else {
ans = dp[pos[l] + 1][pos[r] - 1];
for (int i = l; i <= br[pos[l]]; ++i)
while (inseq(a[i], id[i] + ans, l, r)) ++ans;
for (int i = bl[pos[r]]; i <= r; ++i)
while (inseq(a[i], id[i] - ans, l, r)) ++ans;
}
write(ans), putchar('\n');
}
return 0;
}