This repository has been archived by the owner on Jul 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathP1903.cpp
executable file
·94 lines (93 loc) · 1.92 KB
/
P1903.cpp
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
// Fear cuts deeper than swords.
#include <algorithm>
#include <cmath>
#include <iostream>
using namespace std;
int n, m, c[150001], t[150001], v[1000001], a[150001], ans, bs, qnt, rnt;
char o[5];
struct query
{
int l, r, p, i;
bool operator<(const query b) const
{
if (l / bs == b.l / bs)
{
if (r / bs == b.r / bs)
return p < b.p;
return r < b.r;
}
return l < b.l;
}
} q[150001];
struct change
{
int p, c, l;
} r[150001];
void add(int x)
{
if (!v[x])
ans++;
v[x]++;
}
void del(int x)
{
v[x]--;
if (!v[x])
ans--;
}
int main()
{
cin >> n >> m;
bs = pow(n, 2.0 / 3.0);
for (int i = 1; i <= n; i++)
cin >> c[i], t[i] = c[i];
for (int i = 1; i <= m; i++)
{
cin >> o;
if (o[0] == 'Q')
{
qnt++;
cin >> q[qnt].l >> q[qnt].r;
q[qnt].p = rnt;
q[qnt].i = qnt;
}
else
{
rnt++;
cin >> r[rnt].p >> r[rnt].c;
r[rnt].l = t[r[rnt].p];
t[r[rnt].p] = r[rnt].c;
}
}
sort(q + 1, q + qnt + 1);
for (int i = 1, pl = 1, pr = 0, p = 0; i <= qnt; i++)
{
while (p < q[i].p)
{
if (pl <= r[p + 1].p && r[p + 1].p <= pr)
del(r[p + 1].l), add(r[p + 1].c);
c[r[p + 1].p] = r[p + 1].c;
p++;
}
while (p > q[i].p)
{
if (pl <= r[p].p && r[p].p <= pr)
del(r[p].c), add(r[p].l);
c[r[p].p] = r[p].l;
p--;
}
while (pl > q[i].l)
add(c[--pl]);
while (pr < q[i].r)
add(c[++pr]);
while (pl < q[i].l)
del(c[pl++]);
while (pr > q[i].r)
del(c[pr--]);
a[q[i].i] = ans;
}
for (int i = 1; i <= qnt; i++)
{
cout << a[i] << endl;
}
}