TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
Loading...
Searching...
No Matches
mo.cpp
1#include <algorithm>
2#include <cmath>
3#include <iostream>
4
5using namespace std;
6const int N = 1e6 + 5;
7int a[N], bucket[N], cnt[N];
8int bucket_size;
9struct query {
10 int l, r, i;
11} q[N];
12int ans = 0;
13
14void add(int index) {
15 cnt[a[index]]++;
16 if (cnt[a[index]] == 1)
17 ans++;
18}
19void remove(int index) {
20 cnt[a[index]]--;
21 if (cnt[a[index]] == 0)
22 ans--;
23}
24
25bool 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
31int main() {
32 int n, t, i, j, k = 0;
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}
double k(double x)
Another test function.
int main()
Main function.
constexpr uint32_t N
A struct to represent sparse table for min() as their invariant function, for the given array A....
Definition mo.cpp:9
std::string add(const std::string &first, const std::string &second)
Adding two string.
Definition uint128_t.hpp:38