-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAdauniq.cpp
94 lines (94 loc) · 2.51 KB
/
Adauniq.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
// Ivan Carvalho
// Solution to https://www.spoj.com/problems/ADAUNIQ/
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 2 * 1e5 + 10;
const int BUCKET = 3430;
struct que {
int id, l, r, t;
};
struct upd {
int pos, velho, novo;
};
int divisao[MAXN], mo_left, mo_right, mo_time, n, q, freq[MAXN], exibir[MAXN],
vetor[MAXN], resp;
vector<que> Q;
vector<upd> U;
bool compara(que A, que B) {
if (divisao[A.l] != divisao[B.l]) return divisao[A.l] < divisao[B.l];
if (divisao[A.r] != divisao[B.r]) return divisao[A.r] < divisao[B.r];
return A.t < B.t;
}
void add(int val) {
freq[val]++;
if (freq[val] == 1)
resp++;
else if (freq[val] == 2)
resp--;
}
void remove(int val) {
freq[val]--;
if (freq[val] == 1)
resp++;
else if (freq[val] == 0)
resp--;
}
void update(int idx, int novo) {
if (mo_left <= idx && idx <= mo_right) {
remove(vetor[idx]);
vetor[idx] = novo;
add(vetor[idx]);
} else {
vetor[idx] = novo;
}
}
void doQuery(int idx) {
for (int t = mo_time + 1; t <= Q[idx].t; t++)
update(U[t - 1].pos, U[t - 1].novo);
for (int t = mo_time; t > Q[idx].t; t--)
update(U[t - 1].pos, U[t - 1].velho);
for (int i = mo_right + 1; i <= Q[idx].r; i++) add(vetor[i]);
for (int i = mo_left - 1; i >= Q[idx].l; i--) add(vetor[i]);
for (int i = mo_right; i > Q[idx].r; i--) remove(vetor[i]);
for (int i = mo_left; i < Q[idx].l; i++) remove(vetor[i]);
mo_left = Q[idx].l;
mo_right = Q[idx].r;
mo_time = Q[idx].t;
exibir[Q[idx].id] = resp;
}
int main() {
scanf("%d %d", &n, &q);
for (int i = 0; i < n; i++) {
scanf("%d", &vetor[i]);
divisao[i] = i / BUCKET;
}
for (int i = 0; i < q; i++) {
int x, y, z;
scanf("%d %d %d", &x, &y, &z);
if (x == 1) {
mo_time++;
upd davez;
davez.pos = y;
davez.velho = vetor[y];
davez.novo = z;
vetor[y] = z;
U.push_back(davez);
exibir[i] = -1;
} else {
que davez;
davez.l = y;
davez.r = z;
davez.t = mo_time;
davez.id = i;
Q.push_back(davez);
}
}
sort(Q.begin(), Q.end(), compara);
for (int i = 0; i < n; i++) add(vetor[i]);
mo_left = 0;
mo_right = n - 1;
for (int i = 0; i < Q.size(); i++) doQuery(i);
for (int i = 0; i < q; i++)
if (exibir[i] != -1) printf("%d\n", exibir[i]);
return 0;
}