Blog of RuSun

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

CF940F Machine Learning

LuoGu: CF940F Machine Learning

CF: F. Machine Learning

带修莫队模板题。

查看代码
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#include <cstdio>
#include <cmath>
#include <vector>
#include <functional>
#include <algorithm>
using namespace std;
template <class Type>
void read (Type &x)
{
char c;
bool flag = false;
while ((c = getchar()) < '0' || c > '9')
c == '-' && (flag = true);
x = c - '0';
while ((c = getchar()) >= '0' && c <= '9')
x = (x << 1) + (x << 3) + c - '0';
flag && (x = ~x + 1);
}
template <class Type>
void write (Type x)
{
x < 0 && (putchar('-'), x = ~x + 1);
x > 9 && (write(x / 10), 0);
putchar('0' + x % 10);
}
const int N = 1e5 + 10, M = 2e5 + 10;
int n, m, len, w[N], v[N];
struct Query
{
int l, r, qi, ci;
bool operator < (const Query &_) const
{
if (l / len == _.l / len)
{
if (r / len == _.r / len)
return qi < _.qi;
return r < _.r;
}
return l < _.l;
}
};
vector <Query> q;
struct Modify
{
int t, pre, cur;
};
vector <Modify> c;
int cnt[M], mex[N], res = 1, ans[N];
void add (int x)
{
mex[cnt[x]]--;
if (!mex[cnt[x]] && cnt[x] < res)
res = cnt[x];
mex[++cnt[x]]++;
while (mex[res])
res++;
}
void del (int x)
{
mex[cnt[x]]--;
if (!mex[cnt[x]] && cnt[x] < res)
res = cnt[x];
mex[--cnt[x]]++;
while (mex[res])
res++;
}
int main ()
{
read(n), read(m);
len = pow(n, 0.67);
vector <int> ws;
for (int i = 1; i <= n; i++)
{
read(w[i]), v[i] = w[i];
ws.push_back(w[i]);
}
for (int op; m; m--)
{
read(op);
if (op == 1)
{
int l, r;
read(l), read(r);
q.push_back((Query){l, r, q.size(), c.size()});
}
else if (op == 2)
{
int t, k;
read(t), read(k);
ws.push_back(k);
c.push_back((Modify){t, v[t], k});
v[t] = k;
}
}
sort(ws.begin(), ws.end());
ws.erase(unique(ws.begin(), ws.end()), ws.end());
function <int(int)> find = [&](int x)
{
return lower_bound(ws.begin(), ws.end(), x) - ws.begin();
};
for (int i = 1; i <= n; i++)
w[i] = find(w[i]);
for (Modify &i : c)
i.pre = find(i.pre), i.cur = find(i.cur);
sort(q.begin(), q.end());
int i = 1, j = 0, t = 0;
for (Query k : q)
{
for (; t < k.ci; t++)
{
if (i <= c[t].t && c[t].t <= j)
del(c[t].pre), add(c[t].cur);
w[c[t].t] = c[t].cur;
}
for (; t > k.ci; t--)
{
if (i <= c[t - 1].t && c[t - 1].t <= j)
del(c[t - 1].cur), add(c[t - 1].pre);
w[c[t - 1].t] = c[t - 1].pre;
}
while (i > k.l)
add(w[--i]);
while (j < k.r)
add(w[++j]);
while (i < k.l)
del(w[i++]);
while (j > k.r)
del(w[j--]);
ans[k.qi] = res;
}
for (int i = 0; i < q.size(); i++)
write(ans[i]), puts("");
return 0;
}