【笔记】浅谈LOJ数列分块九合一

前言

笔者刚学分块,最近写完了LOJ的数列分块九合一,在此写一下题解报告与分块的基础思想。

分块基础知识

分块其实就是一种优美的暴力,以最基础的数列分块为例,我们一般把数列分为若干块(块长一般为 \(\sqrt{n}\) ),然后我们维护每个块中的一些信息,从而在查询时整块处理,时间复杂度一般为 \(O(n\sqrt{n})\)。下面以LOJ数列分块入门为例,讲解分块在竞赛题目当中的应用。

数列分块例题

LOJ6277

题目传送门

这是数列分块最基础的一道题目,涉及区间修改,单点查询。我们可以先把原序列分成若干个块长为 \(\sqrt{n}\) 的块,然后我们对每个块维护如下信息:

\(tag[i]\) 第 \(i\) 块一共被加上了多少(类似线段树中的 \(lazytag\) )。

那么对于每个修改操作,我们分两种情况讨论

  1. 若 \(l\) 和 \(r\) 在同一块中,那么我们直接暴力对原序列中的每个数进行修改。
  2. 若 \(l\) 和 \(r\) 在不同块中,那么我们先对 \(l\) 和 \(r\) 所在的块分别进行暴力修改,然后对于夹在中间的块,我们只需要修改它们的 \(tag\) 即可。

对于每个查询操作,我们直接输出原序列的值加上当前块的 \(tag\) 即可,具体代码如下

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
#include <bits/stdc++.h>

using namespace std;
const int N = 5e4 + 10, B = 225;
int a[N], tag[B], id[N];

int read()
{
int x = 0, y = 1; char c = getchar();
while (c < '0' || c > '9') {if (c == '-') y = -1; c = getchar();}
while (c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
return x * y;
}

void modify(int l, int r, int c)
{
if (id[l] == id[r])
{
for (int i = l; i <= r; i++)
a[i] += c;
return;
}
for (int i = l; id[i] == id[l]; i++) a[i] += c;
for (int i = r; id[i] == id[r]; i--) a[i] += c;
for (int i = id[l] + 1; i <= id[r] - 1; i++) tag[i] += c;
}

int query(int x)
{
return a[x] + tag[id[x]];
}

int main()
{
int n = read();
for (int i = 1; i <= n; i++)
a[i] = read(), id[i] = (i - 1) / B + 1;
for (int i = 1; i <= n; i++)
{
int op = read(), l = read(), r = read(), c = read();
if (op == 0) modify(l, r, c);
else printf("%d\n", query(r));
}
return 0;
}

LOJ6278

题目传送门

对于一般的分块题,我们有一个非常重要的思路: 怎么暴力怎么来。那让我们想一下怎么暴力求出区间比 \(x\) 小的数的个数呢? 很显然,我们只需要先对区间排序,然后再二分查找即可。那么我们就有了一个最基本的思路。

首先我们对原序列进行分块,然后我们对每个块分别排序,因为操作涉及区间加法,所以我们还是维护每个块的 \(tag\)。

对于每个修改操作,我们处理方式和上题一致,不过在修改不完整块时,要对当前块进行重排。

对于每个查询操作,我们也分两种情况讨论

  1. 若 \(l\) 和 \(r\) 在一个块中,我们直接暴力统计。
  2. 若 \(l\) 和 \(r\) 在不同块中,我们先对 \(l\) 和 \(r\) 所在块分别进行暴力统计,然后对于中间块,我们直接二分查找即可。

注意在查询时,要注意 \(tag\) 带来的影响。

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
79
#include <bits/stdc++.h>
#define int long long

using namespace std;
const int N = 5e4 + 10, B = 225;
int a[N], tag[B], id[N];
vector<int> b[B];
int n;

int read()
{
int x = 0, y = 1; char c = getchar();
while (c < '0' || c > '9') {if (c == '-') y = -1; c = getchar();}
while (c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
return x * y;
}

void update(int x)
{
b[x].clear();
for (int i = (x - 1) * B + 1; i <= min(n, x * B); i++)
b[x].push_back(a[i]);
sort(b[x].begin(), b[x].end());
}

void modify(int l, int r, int c)
{
if (id[l] == id[r])
{
for (int i = l; i <= r; i++)
a[i] += c;
update(id[l]);
return;
}
for (int i = l; id[i] == id[l]; i++) a[i] += c;
for (int i = r; id[i] == id[r]; i--) a[i] += c;
update(id[l]);
update(id[r]);
for (int i = id[l] + 1; i <= id[r] - 1; i++) tag[i] += c;
}

int query(int l, int r, int c)
{
if (id[l] == id[r])
{
int res = 0;
for (int i = l; i <= r; i++)
if (a[i] < c - tag[id[i]]) res++;
return res;
}
int res = 0;
for (int i = l; id[i] == id[l]; i++)
if (a[i] < c - tag[id[i]]) res++;
for (int i = r; id[i] == id[r]; i--)
if (a[i] < c - tag[id[i]]) res++;
for (int i = id[l] + 1; i <= id[r] - 1; i++)
res += lower_bound(b[i].begin(), b[i].end(), c - tag[i]) - b[i].begin();
return res;
}

signed main()
{
n = read();
for (int i = 1; i <= n; i++)
{
a[i] = read();
id[i] = (i - 1) / B + 1;
b[id[i]].push_back(a[i]);
}
for (int i = 1; i <= id[n]; i++)
sort(b[i].begin(), b[i].end());
for (int i = 1; i <= n; i++)
{
int op = read(), l = read(), r = read(), c = read();
if (op == 0) modify(l, r, c);
else printf("%lld\n", query(l, r, c * c));
}
return 0;
}

LOJ6279

题目传送门

这题和上一题的思路基本一致,也是对每个块分别排序,维护一个 \(tag\),在查询时逐块比较前驱大小即可,思路相像,不过多赘述。

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
79
80
81
82
83
84
85
86
#include <bits/stdc++.h>

using namespace std;
const int N = 1e5 + 10, B = 320;
int a[N], id[N], tag[N];
vector<int> b[N];
int n;

int read()
{
int x = 0, y = 1; char c = getchar();
while (c < '0' || c > '9') {if (c == '-') y = -1; c = getchar();}
while (c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
return x * y;
}

void update(int x)
{
b[x].clear();
for (int i = (x - 1) * B + 1; i <= min(n, x * B); i++)
b[x].push_back(a[i]);
sort(b[x].begin(), b[x].end());
}

void modify(int l, int r, int c)
{
if (id[l] == id[r])
{
for (int i = l; i <= r; i++)
a[i] += c;
update(id[l]);
return;
}
for (int i = l; id[i] == id[l]; i++) a[i] += c;
for (int i = r; id[i] == id[r]; i--) a[i] += c;
update(id[l]);
update(id[r]);
for (int i = id[l] + 1; i <= id[r] - 1; i++) tag[i] += c;
}

int query(int l, int r, int c)
{
if (id[l] == id[r])
{
int res = -1;
for (int i = l; i <= r; i++)
if (a[i] < c - tag[id[i]] && (res == -1 || res < a[i] + tag[id[i]]))
res = a[i] + tag[id[i]];
return res;
}
int res = -1;
for (int i = l; id[i] == id[l]; i++)
if (a[i] < c - tag[id[i]] && (res == -1 || res < a[i] + tag[id[i]]))
res = a[i] + tag[id[i]];
for (int i = r; id[i] == id[r]; i--)
if (a[i] < c - tag[id[i]] && (res == -1 || res < a[i] + tag[id[i]]))
res = a[i] + tag[id[i]];
for (int i = id[l] + 1; i <= id[r] - 1; i++)
{
int x = lower_bound(b[i].begin(), b[i].end(), c - tag[i]) - b[i].begin() - 1;
if (x < 0) continue;
if (b[i][x] + tag[i] < c && (res == -1 || res < b[i][x] + tag[i]))
res = b[i][x] + tag[i];
}
return res;
}

int main()
{
n = read();
for (int i = 1; i <= n; i++)
{
a[i] = read();
id[i] = (i - 1) / B + 1;
b[id[i]].push_back(a[i]);
}
for (int i = 1; i <= id[n]; i++)
sort(b[i].begin(), b[i].end());
for (int i = 1; i <= n; i++)
{
int op = read(), l = read(), r = read(), c = read();
if (op == 0) modify(l, r, c);
else printf("%d\n", query(l, r, c));
}
return 0;
}

LOJ6280

题目传送门

固然可以用线段树水过,如果用分块做,我们对每个块维护以下信息:

\(tag[i]\) 第 \(i\) 块一共被加了多少。

\(sum[i]\) 第 \(i\) 块的总和是多少。

对于每个修改操作,方法大同小异,不过注意在修改过程中要更新 \(sum\) 的值。

对于每个查询操作,对不完整块暴力计算,对完整块直接叠加 \(sum\),即可通过本题

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
#include <bits/stdc++.h>
#define int long long

using namespace std;
const int N = 5e4 + 10, B = 225;
int a[N], id[N], tag[B], sum[B];
int n;

int read()
{
int x = 0, y = 1; char c = getchar();
while (c < '0' || c > '9') {if (c == '-') y = -1; c = getchar();}
while (c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
return x * y;
}

int get(int x)
{
return min(n, x * B) - (x - 1) * B;
}

void modify(int l, int r, int c)
{
if (id[l] == id[r])
{
for (int i = l; i <= r; i++) a[i] += c, sum[id[i]] += c;;
return;
}
for (int i = l; id[i] == id[l]; i++) a[i] += c, sum[id[i]] += c;
for (int i = r; id[i] == id[r]; i--) a[i] += c, sum[id[i]] += c;
for (int i = id[l] + 1; i <= id[r] - 1; i++) tag[i] += c, sum[i] += c * get(i);
}

int query(int l, int r, int c)
{
if (id[l] == id[r])
{
int res = 0;
for (int i = l; i <= r; i++)
res = (res + a[i] + tag[id[i]]) % c;
return res;
}
int res = 0;
for (int i = l; id[i] == id[l]; i++) res = (res + a[i] + tag[id[i]]) % c;
for (int i = r; id[i] == id[r]; i--) res = (res + a[i] + tag[id[i]]) % c;
for (int i = id[l] + 1; i <= id[r] - 1; i++) res = (res + sum[i]) % c;
return res;
}

signed main()
{
n = read();
for (int i = 1; i <= n; i++)
{
a[i] = read();
id[i] = (i - 1) / B + 1;
sum[id[i]] += a[i];
}
for (int i = 1; i <= n; i++)
{
int op = read(), l = read(), r = read(), c = read();
if (op == 0) modify(l, r, c);
else printf("%lld\n", query(l, r, c + 1));
}
return 0;
}

LOJ6281

题目传送门

注意到,因为值域为 \([0, 2^{31}-1]\),所以每个数至多被开方四次就变为 \(1\),所以我们可以用一个 \(flag\) 数组维护当前块内的数是否全部为 \(1\),如果不是就对当前块进行暴力修改,在修改过程中顺便维护 \(sum\) 和 \(flag\),如果当前块的 \(flag\) 为 \(1\),则可以跳过修改,查询与上题一致。

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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include <bits/stdc++.h>

using namespace std;
const int N = 5e4 + 10, B = 225;
int a[N], id[N], flag[B], sum[B];
int n;

int read()
{
int x = 0, y = 1; char c = getchar();
while (c < '0' || c > '9') {if (c == '-') y = -1; c = getchar();}
while (c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
return x * y;
}

void update(int x)
{
flag[x] = 1;
for (int i = (x - 1) * B + 1; i <= min(n, x * B); i++)
flag[x] &= a[i] == 1;
}

void modify(int l, int r)
{
if (id[l] == id[r])
{
if (flag[id[l]]) return;
for (int i = l; i <= r; i++)
{
sum[id[i]] = sum[id[i]] - a[i] + sqrt(a[i]);
a[i] = sqrt(a[i]);
}
update(id[l]);
return;
}
if (!flag[id[l]])
{
for (int i = l; id[i] == id[l]; i++)
sum[id[i]] = sum[id[i]] - a[i] + sqrt(a[i]), a[i] = sqrt(a[i]);
update(id[l]);
}
if (!flag[id[r]])
{
for (int i = r; id[i] == id[r]; i--)
sum[id[i]] = sum[id[i]] - a[i] + sqrt(a[i]), a[i] = sqrt(a[i]);
update(id[r]);
}
for (int i = id[l] + 1; i <= id[r] - 1; i++)
{
if (flag[i]) continue;
for (int j = (i - 1) * B + 1; j <= min(n, i * B); j++)
sum[i] = sum[i] - a[j] + sqrt(a[j]), a[j] = sqrt(a[j]);
update(i);
}
}

int query(int l, int r)
{
if (id[l] == id[r])
{
int res = 0;
for (int i = l; i <= r; i++)
res += a[i];
return res;
}
int res = 0;
for (int i = l; id[i] == id[l]; i++) res += a[i];
for (int i = r; id[i] == id[r]; i--) res += a[i];
for (int i = id[l] + 1; i <= id[r] - 1; i++) res += sum[i];
return res;
}

int main()
{
n = read();
for (int i = 1; i <= n; i++)
{
a[i] = read();
id[i] = (i - 1) / B + 1;
sum[id[i]] += a[i];
}
for (int i = 1; i <= id[n]; i++)
{
flag[i] = 1;
for (int j = (i - 1) * B + 1; j <= min(n, i * B); j++)
flag[i] &= a[j] == 1;
}
for (int i = 1; i <= n; i++)
{
int op = read(), l = read(), r = read(), c = read();
if (op == 0) modify(l, r);
else printf("%d\n", query(l, r));
}
return 0;
}

LOJ6282

题目传送门

一个显然的做法是对序列进行分块,然后对于每个插入操作,我们直接在对应块中插入,对于查询操作,就把块中对应位置的值输出,但是这种做法有一个问题,就是如果有多个数被连续插入一个块中,时间复杂度就无法保证,所以当一个块长度超过一个给定值,我们就块重构,这样可以通过本题。

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
#include <bits/stdc++.h>
#define pii pair<int, int>

using namespace std;
const int N = 1e5 + 10, B = 1010;
int a[N << 1], id[N << 1];
vector<int> b[(N / B) << 1];
int n;

int read()
{
int x = 0, y = 1; char c = getchar();
while (c < '0' || c > '9') {if (c == '-') y = -1; c = getchar();}
while (c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
return x * y;
}

pii query(int x)
{
int k = 1;
while (x > b[k].size())
x -= b[k].size(), k++;
return {k, x - 1};
}

void rebuild()
{
int len = 0;
for (int i = 1; i <= id[n]; i++)
{
int l = b[i].size();
for (int j = 0; j < l; j++)
a[++len] = b[i][j];
b[i].clear();
}
n = len;
for (int i = 1; i <= n; i++)
{
id[i] = (i - 1) / B + 1;
b[id[i]].push_back(a[i]);
}
}

void insert(int l, int r)
{
pii t = query(l);
b[t.first].insert(b[t.first].begin() + t.second, r);
if (b[t.first].size() > (B << 2)) rebuild();
}

int ask(int x)
{
pii t = query(x);
return b[t.first][t.second];
}

int main()
{
n = read();
for (int i = 1; i <= n; i++)
{
a[i] = read();
id[i] = (i - 1) / B + 1;
b[id[i]].push_back(a[i]);
}
for (int i = 1; i <= n; i++)
{
int op = read(), l = read(), r = read(), c = read();
if (op == 0) insert(l, r);
else printf("%d\n", ask(r));
}
return 0;
}

LOJ6283

题目传送门

这题多了一个乘法操作,所以我们多维护一个乘法的标记 \(mul\),表示当前块被乘了多少,因为加法和乘法的优先级不同,所以在每一次修改前对不完整块下传一下标记即可通过。

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
79
80
81
#include <bits/stdc++.h>
#define int long long

using namespace std;
const int N = 1e5 + 10, mod = 1e4 + 7, B = 320;
int a[N], id[N], add[B], mul[B];
int n;

int read()
{
int x = 0, y = 1; char c = getchar();
while (c < '0' || c > '9') {
if (c == '-') y = -1;
c = getchar();
}
while (c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
return x * y;
}

void update(int x)
{
for (int i = (x - 1) * B + 1; i <= min(n, x * B); i++)
a[i] = (a[i] * mul[x] + add[x]) % mod;
add[x] = 0, mul[x] = 1;
}

void plu(int l, int r, int c)
{
if (id[l] == id[r])
{
update(id[l]);
for (int i = l; i <= r; i++)
a[i] = (a[i] + c) % mod;
return;
}
update(id[l]);
update(id[r]);
for (int i = l; id[i] == id[l]; i++) a[i] = (a[i] + c) % mod;
for (int i = r; id[i] == id[r]; i--) a[i] = (a[i] + c) % mod;
for (int i = id[l] + 1; i <= id[r] - 1; i++) add[i] = (add[i] + c) % mod;
}

void muli(int l, int r, int c)
{
if (id[l] == id[r])
{
update(id[l]);
for (int i = l; i <= r; i++)
a[i] = a[i] * c % mod;
return;
}
update(id[l]);
update(id[r]);
for (int i = l; id[i] == id[l]; i++) a[i] = a[i] * c % mod;
for (int i = r; id[i] == id[r]; i--) a[i] = a[i] * c % mod;
for (int i = id[l] + 1; i <= id[r] - 1; i++) add[i] = add[i] * c % mod, mul[i] = mul[i] * c % mod;
}

int query(int x)
{
return (a[x] * mul[id[x]] + add[id[x]]) % mod;
}

signed main()
{
n = read();
for (int i = 1; i <= n; i++)
{
a[i] = read() % mod;
id[i] = (i - 1) / B + 1;
}
for (int i = 1; i <= id[n]; i++) mul[i] = 1;
for (int i = 1; i <= n; i++)
{
int op = read(), l = read(), r = read(), c = read();
if (op == 0) plu(l, r, c);
else if (op == 1) muli(l, r, c);
else printf("%d\n", query(r));
}
return 0;
}

LOJ6284

题目传送门

首先值域太大了,离散化一下,然后用 \(tag[i]\) 表示第 \(i\) 块被覆盖成了什么,然后用 \(cnt[i][j]\) 表示 \(tag[i]\) 不存在 的情况下 \(j\) 的出现次数。

对于每个修改操作,还是套路对每个不完整块下传懒标记,然后修改完整块的 \(tag\) 即可。

对于每个查询操作,我们也对不完整块下传懒标记,然后对于每个完整块,如果 \(tag[i]\) 存在,则暴力判断 \(tag[i]\) 是否与要查找的值相等,不存在则直接加上 \(cnt\) 值即可。

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
79
80
81
82
83
84
85
86
87
88
89
90
#include <bits/stdc++.h>

using namespace std;
const int N = 2e5 + 10, B = 320, INF = -114514;
int a[N], id[N], tag[B];
int cnt[N][B];
int n;
unordered_map<int, int> h;
int tot;

int read()
{
int x = 0, y = 1; char c = getchar();
while (c < '0' || c > '9') {if (c == '-') y = -1; c = getchar();}
while (c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
return x * y;
}

void update(int x)
{
if (tag[x] == INF) return;
for (int i = (x - 1) * B + 1; i <= min(n, x * B); i++)
cnt[x][a[i]]--, cnt[x][tag[x]]++, a[i] = tag[x];
tag[x] = INF;
}

int get(int x)
{
return min(n, x * B) - (x - 1) * B;
}

int query(int l, int r, int c)
{
if (id[l] == id[r])
{
update(id[l]);
int res = 0;
for (int i = l; i <= r; i++)
if (a[i] == c) res++;
return res;
}
update(id[l]);
update(id[r]);
int res = 0;
for (int i = l; id[i] == id[l]; i++)
if (a[i] == c) res++;
for (int i = r; id[i] == id[r]; i--)
if (a[i] == c) res++;
for (int i = id[l] + 1; i <= id[r] - 1; i++)
if (tag[i] != INF) res += (tag[i] == c) * get(i);
else res += cnt[i][c];
return res;
}

void modify(int l, int r, int c)
{
if (id[l] == id[r])
{
update(id[l]);
for (int i = l; i <= r; i++) cnt[id[i]][a[i]]--, cnt[id[i]][c]++, a[i] = c;
return;
}
update(id[l]);
update(id[r]);
for (int i = l; id[i] == id[l]; i++) cnt[id[i]][a[i]]--, cnt[id[i]][c]++, a[i] = c;
for (int i = r; id[i] == id[r]; i--) cnt[id[i]][a[i]]--, cnt[id[i]][c]++, a[i] = c;
for (int i = id[l] + 1; i <= id[r] - 1; i++) tag[i] = c;
}

int main()
{
n = read();
for (int i = 1; i <= n; i++)
{
a[i] = read();
if (!h[a[i]]) h[a[i]] = ++tot;
a[i] = h[a[i]];
id[i] = (i - 1) / B + 1;
cnt[id[i]][a[i]]++;
}
for (int i = 1; i <= id[n]; i++) tag[i] = INF;
for (int i = 1; i <= n; i++)
{
int l = read(), r = read(), c = read();
if (!h[c]) h[c] = ++tot;
c = h[c];
printf("%d\n", query(l, r, c));
modify(l, r, c);
}
}

LOJ6285

题目传送门

这题回滚莫队可以水过去,但如果强制在线呢?

我们先对原序列分块,然后考虑 \(l\) 和 \(r\) 之间的众数,显然这个众数只可能是中间块的众数或是出现在 \(l\) 和 \(r\) 所在不完整块的数,所以我们可以这么搞:

\(f[l][r]\) 表示第 \(l\) 块到第 \(r\) 块的最小众数。

\(s[i][j]\) 表示在第 \(1\) 块到第 \(i\) 块中 \(j\) 的出现次数。

不难发现这两个东西都可以 \(O(n\sqrt{n})\) 求出,然后查找的时候直接把中间块的众数与左右块出现的数逐一比对即可得出答案,具体实现见代码。

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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include <bits/stdc++.h>

using namespace std;
const int N = 1e5 + 10, B = 320;
bool vis[N];
int a[N], b[N], id[N], len;
int f[B][B], s[B][N];
int cnt[N];

int read()
{
int x = 0, y = 1; char c = getchar();
while (c < '0' || c > '9') {if (c == '-') y = -1; c = getchar();}
while (c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
return x * y;
}

int find(int x)
{
return lower_bound(b + 1, b + len + 1, x) - b;
}

int get(int i, int j, int k)
{
return s[j][k] - s[i - 1][k];
}

int query(int l, int r)
{
memset(cnt, 0, sizeof cnt);
if (id[l] == id[r])
{
int res = 0;
for (int i = l; i <= r; i++)
{
cnt[a[i]]++;
if (cnt[a[i]] > cnt[res] || (cnt[a[i]] == cnt[res] && a[i] < res))
res = a[i];
}
return res;
}
int res = f[id[l] + 1][id[r] - 1];
for (int i = l; id[i] == id[l]; i++)
{
cnt[a[i]]++;
if (cnt[a[i]] + get(id[l] + 1, id[r] - 1, a[i]) > cnt[res] + get(id[l] + 1, id[r] - 1, res))
res = a[i];
else if (cnt[a[i]] + get(id[l] + 1, id[r] - 1, a[i]) == cnt[res] + get(id[l] + 1, id[r] - 1, res) && a[i] < res)
res = a[i];
}
for (int i = r; id[i] == id[r]; i--)
{
cnt[a[i]]++;
if (cnt[a[i]] + get(id[l] + 1, id[r] - 1, a[i]) > cnt[res] + get(id[l] + 1, id[r] - 1, res))
res = a[i];
else if (cnt[a[i]] + get(id[l] + 1, id[r] - 1, a[i]) == cnt[res] + get(id[l] + 1, id[r] - 1, res) && a[i] < res)
res = a[i];
}
return res;
}

int main()
{
int n = read();
for (int i = 1; i <= n; i++)
{
a[i] = read(), b[i] = a[i];
id[i] = (i - 1) / B + 1;
}
sort(b + 1, b + n + 1);
len = unique(b + 1, b + n + 1) - b - 1;
for (int i = 1; i <= n; i++) a[i] = find(a[i]);
for (int i = 1; i <= id[n]; i++)
{
for (int j = (i - 1) * B + 1; j <= min(n, i * B); j++)
s[i][a[j]]++;
for (int j = 1; j <= len; j++)
s[i][j] += s[i - 1][j];
}
for (int i = 1; i <= id[n]; i++)
for (int j = i; j <= id[n]; j++)
{
int &t = f[i][j];
t = f[i][j - 1];
for (int k = (j - 1) * B + 1; k <= min(n, j * B); k++)
if (get(i, j, a[k]) > get(i, j, t)) t = a[k];
else if (get(i, j, a[k]) == get(i, j, t) && a[k] < t) t = a[k];
}
for (int i = 1; i <= n; i++)
{
int l = read(), r = read();
printf("%d\n", b[query(l, r)]);
}
return 0;
}