CSES - Shared codeLink to this code:
https://cses.fi/paste/46a3c1fc1a0c1bbb79e67e/
#include <algorithm>
#include <bits/stdc++.h>
#include <climits>
#include <deque>
#include <ext/pb_ds/assoc_container.hpp>
#include <iterator>
#include <numeric>
#include <vector>
using namespace std;
using namespace __gnu_pbds;
#ifndef LOCAL
#endif
#define all(x) (x).begin(), (x).end()
#define allr(x) (x).rbegin(), (x).rend()
typedef long long ll;
template <typename T, typename comp = less<T>>
using ordered_set =
tree<T, null_type, comp, rb_tree_tag, tree_order_statistics_node_update>;
template <typename T> using min_heap = priority_queue<T, vector<T>, greater<T>>;
template <typename T> class vect1 : public vector<T> {
public:
using vector<T>::vector;
T& operator[](size_t idx) {
return this->at(idx - 1);
}
};
#define int long long
int block_size;
struct query {
int l, r, idx;
friend bool operator<(query q1, query q2) {
return make_pair(q1.l / block_size,
(q1.l / block_size & 1) ? q1.r : -q1.r) <
make_pair(q2.l / block_size, (q2.l / block_size & 1) ? q2.r : -q2.r);
}
};
vect1<int> ht;
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n, q;
cin >> n >> q;
block_size = (int)sqrt((long double)n);
vect1<int> arr(n);
for (auto& a : arr)
cin >> a;
{
int sz = 0;
map<int, int> mp;
for (auto& a : arr)
mp[a];
for (auto &[k, v] : mp)
v = ++sz;
for (auto& a : arr)
a = mp[a];
ht.resize(sz, 0);
}
ll cnt = 0;
auto add = [&](int val) {
if (ht[val] == 0)
cnt++;
ht[val]++;
};
auto remove = [&](int val) {
ht[val]--;
if (ht[val] == 0)
cnt--;
};
vect1<int> ans(q);
vect1<query> queries(q);
for (int i = 1; i <= q; i++)
cin >> queries[i].l >> queries[i].r, queries[i].idx = i;
sort(all(queries));
int cur_l = 1, cur_r = 0;
for (auto &[ql, qr, idx] : queries) {
while (cur_l > ql)
add(arr[--cur_l]);
while (cur_l < ql)
remove(arr[cur_l++]);
while (cur_r > qr)
remove(arr[cur_r--]);
while (cur_r < qr)
add(arr[++cur_r]);
ans[idx] = cnt;
}
for (auto& a : ans)
cout << a << '\n';
}