-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdistinct-values-queries.cpp
83 lines (71 loc) · 1.83 KB
/
distinct-values-queries.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
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
//solvable offline with regular fenwick/segment tree
//i wanted to practice persistent segment tree impl.
const int maxn = 2e5+5;
int n, m, a[maxn];
map<int,int> last;
struct psegtree {
int nodes = 0;
vector<int> st, L, R, root;
psegtree() {
root.push_back(nodes++);
addnode();
build(root.back());
}
void addnode(int x = 0) {
st.push_back(x);
L.push_back(-1);
R.push_back(-1);
}
void build(int pos, int l = 1, int r = n) {
if (l == r) return;
int mid = (l+r) >> 1;
L[pos] = nodes++, addnode();
R[pos] = nodes++, addnode();
build(L[pos], l, mid);
build(R[pos], mid+1, r);
}
void upd(int i, int v, int last, int pos, int l = 1, int r = n) {
if (l == r) { st[pos] = v; return; }
int mid = (l+r) >> 1;
if (i <= mid) {
if (L[pos] == -1) L[pos] = nodes++, addnode(st[L[last]]);
if (R[pos] == -1) R[pos] = R[last];
upd(i, v, L[last], L[pos], l, mid);
} else {
if (L[pos] == -1) L[pos] = L[last];
if (R[pos] == -1) R[pos] = nodes++, addnode(st[R[last]]);
upd(i, v, R[last], R[pos], mid+1, r);
}
st[pos] = st[L[pos]] + st[R[pos]];
}
int query(int ql, int qr, int pos = 1, int l = 1, int r = n) {
if (r < ql or l > qr) return 0;
if (ql <= l && r <= qr) return st[pos];
int mid = (l+r) >> 1;
return query(ql, qr, L[pos], l, mid) + query(ql, qr, R[pos], mid+1, r);
}
void upd(int i) {
root.push_back(nodes++);
addnode();
if (last.count(a[i])) upd(last[a[i]], 0, root[i-1], root[i]);
upd(last[a[i]] = i, 1, root[i-1], root[i]);
}
int getans(int ql, int qr) { return query(ql, qr, root[qr]); }
};
int main(){
scanf("%d %d", &n, &m);
psegtree st;
for(int i = 1; i<=n; i++) {
scanf("%d", &a[i]);
st.upd(i);
}
while(m--) {
int l, r;
scanf("%d %d", &l, &r);
printf("%d\n", st.getans(l, r));
}
return 0;
}