TheAlgorithms/C++
1.0.0
All the algorithms implemented in C++
Toggle main menu visibility
Loading...
Searching...
No Matches
mo.cpp
1
#include <algorithm>
2
#include <cmath>
3
#include <iostream>
4
5
using namespace
std;
6
const
int
N = 1e6 + 5;
7
int
a[N], bucket[N], cnt[N];
8
int
bucket_size;
9
struct
query
{
10
int
l, r, i;
11
} q[N];
12
int
ans = 0;
13
14
void
add
(
int
index) {
15
cnt[a[index]]++;
16
if
(cnt[a[index]] == 1)
17
ans++;
18
}
19
void
remove(
int
index) {
20
cnt[a[index]]--;
21
if
(cnt[a[index]] == 0)
22
ans--;
23
}
24
25
bool
mycmp(
query
x,
query
y) {
26
if
(x.l / bucket_size != y.l / bucket_size)
27
return
x.l / bucket_size < y.l / bucket_size;
28
return
x.r < y.r;
29
}
30
31
int
main
() {
32
int
n, t, i;
33
scanf(
"%d"
, &n);
34
for
(i = 0; i < n; i++) scanf(
"%d"
, &a[i]);
35
bucket_size = ceil(sqrt(n));
36
scanf(
"%d"
, &t);
37
for
(i = 0; i < t; i++) {
38
scanf(
"%d %d"
, &q[i].l, &q[i].r);
39
q[i].l--;
40
q[i].r--;
41
q[i].i = i;
42
}
43
sort(q, q + t, mycmp);
44
int
left = 0, right = 0;
45
for
(i = 0; i < t; i++) {
46
int
L = q[i].l, R = q[i].r;
47
while
(left < L) {
48
remove(left);
49
left++;
50
}
51
while
(left > L) {
52
add
(left - 1);
53
left--;
54
}
55
while
(right <= R) {
56
add
(right);
57
right++;
58
}
59
while
(right > R + 1) {
60
remove(right - 1);
61
right--;
62
}
63
bucket[q[i].i] = ans;
64
}
65
for
(i = 0; i < t; i++) printf(
"%d\n"
, bucket[i]);
66
return
0;
67
}
main
int main()
Main function.
Definition
generate_parentheses.cpp:110
query
Definition
mo.cpp:9
add
std::string add(const std::string &first, const std::string &second)
Adding two string.
Definition
uint128_t.hpp:31
range_queries
mo.cpp
Generated by
1.18.0