-
Notifications
You must be signed in to change notification settings - Fork 185
/
1734.cc
51 lines (49 loc) · 1.17 KB
/
1734.cc
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
// https://cses.fi/problemset/task/1734/
#include <bits/stdc++.h>
using namespace std;
using iii = tuple<int, int, int>;
using mii = map<int, int>;
using vi = vector<int>;
using viii = vector<iii>;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int n, q, a, b;
cin >> n >> q;
vi x(n);
mii m;
for (int i = 0; i < n; i++) {
int y;
cin >> y;
if (m.count(y)) x[i] = m[y];
else x[i] = m.size(), m[y] = x[i];
}
viii y(q);
for (int i = 0; i < q; i++) {
cin >> a >> b;
y[i] = {a-1, b, i};
}
int k = sqrt(n);
sort(y.begin(), y.end(), [&](iii x, iii y) -> bool {
int a1, b1, i1, a2, b2, i2;
tie(a1, b1, i1) = x;
tie(a2, b2, i2) = y;
int a1q = a1/k, a2q = a2/k;
if (a1q < a2q) return true;
if (a1q > a2q) return false;
return b1 < b2;
});
vi res(q);
vi count(m.size());
int l = 0, r = 0, ans = 0;
for (iii z : y) {
int i;
tie(a, b, i) = z;
while (r < b) if (++count[x[r++]] == 1) ans++;
while (r > b) if (--count[x[--r]] == 0) ans--;
while (l < a) if (--count[x[l++]] == 0) ans--;
while (l > a) if (++count[x[--l]] == 1) ans++;
res[i] = ans;
}
for (int z : res) cout << z << '\n';
}