Blog of RuSun

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

带修莫队模板

用于带修改的莫队问题。

增加一维维护当前修改的位置。块长取 $n ^ \frac 2 3 $ 。

查看代码
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
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
using namespace std;
const int N = 1e6 + 10;
int n, m, len, res, w[N], v[N], ans[N], cnt[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 pos, pre, cur;
};
vector<Modify> c;
void add(int x)
{
if (!cnt[x])
res++;
cnt[x]++;
}
void del(int x)
{
cnt[x]--;
if (!cnt[x])
res--;
}
int main()
{
scanf("%d%d", &n, &m);
len = pow(n, 2.0 / 3);
for (int i = 1; i <= n; i++)
{
scanf("%d", &w[i]);
v[i] = w[i];
}
for (char op[3]; m; m--)
{
scanf("%s", op);
if (op[0] == 'Q')
{
int l, r;
scanf("%d%d", &l, &r);
q.push_back((Query){l, r, q.size(), c.size()});
}
else if (op[0] == 'R')
{
int a, b;
scanf("%d%d", &a, &b);
c.push_back((Modify){a, v[a], b});
v[a] = b;
}
}
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].pos && c[t].pos <= j)
{
del(c[t].pre);
add(c[t].cur);
}
w[c[t].pos] = c[t].cur;
}
for (; t > k.ci; t--)
{
if (i <= c[t - 1].pos && c[t - 1].pos <= j)
{
del(c[t - 1].cur);
add(c[t - 1].pre);
}
w[c[t - 1].pos] = c[t - 1].pre;
}
while (i > k.l)
add(w[--i]);
while (i < k.l)
del(w[i++]);
while (j < k.r)
add(w[++j]);
while (j > k.r)
del(w[j--]);
ans[k.qi] = res;
}
for (int i = 0; i < q.size(); i++)
printf("%d\n", ans[i]);
return 0;
}