Blog of RuSun

\begin {array}{c} \mathfrak {One Problem Is Difficult} \\\\ \mathfrak {Because You Don't Know} \\\\ \mathfrak {Why It Is Diffucult} \end {array}

P3709 大爷的字符串题

P3709 大爷的字符串题

离散化。莫队维护 $cnt$ 表示某种数出现的个数,$tot$ 表示有多少种数出现若干次。当增加时,更新答案。删除时,如果当前是答案且只有一种数,那么少一个后答案依然是该数,此时次数减一。

查看代码
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
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
using namespace std;
const int N = 2e5 + 10;
int n, m, len, res, w[N], ans[N], tot[N], cnt[N];
struct Query
{
int l, r, id;
bool operator<(const Query &_) const
{
if (l / len != _.l / len)
return l < _.l;
return (l / len) & 1 ? r < _.r : r > _.r;
}
} q[N];
void add(int x)
{
tot[cnt[x]]--;
tot[++cnt[x]]++;
res = max(res, cnt[x]);
}
void del(int x)
{
tot[cnt[x]]--;
if (cnt[x] == res && !tot[cnt[x]])
res--;
tot[--cnt[x]]++;
}
int main()
{
scanf("%d%d", &n, &m);
len = sqrt(n);
vector<int> ws;
for (int i = 1; i <= n; i++)
{
scanf("%d", &w[i]);
ws.push_back(w[i]);
}
sort(ws.begin(), ws.end());
ws.erase(unique(ws.begin(), ws.end()), ws.end());
for (int i = 1; i <= n; i++)
w[i] = lower_bound(ws.begin(), ws.end(), w[i]) - ws.begin() + 1;
for (int i = 1; i <= m; i++)
{
scanf("%d%d", &q[i].l, &q[i].r);
q[i].id = i;
}
sort(q + 1, q + m + 1);
for (int k = 1, i = 0, j = 1; k <= m; k++)
{
int l = q[k].l, r = q[k].r;
while (i < r)
add(w[++i]);
while (i > r)
del(w[i--]);
while (j < l)
del(w[j++]);
while (j > l)
add(w[--j]);
ans[q[k].id] = res;
}
for (int i = 1; i <= m; i++)
printf("%d\n", -ans[i]);
return 0;
}