Problem p02599 (C++) — buggy submission, verdict: Time Limit Exceeded

// C++ code to find number of distinct numbers
// in a subarray
#include <bits/stdc++.h>
using namespace std;
#define ld long double
#define ll long long
const int MAX = 1000001;
int a[5 * MAX + 7];
struct Query {
  int l, r, idx;
};

bool cmp(Query x, Query y) { return x.r < y.r; }

void update(int idx, int val, int bit[], int n) {
  for (; idx <= n; idx += idx & -idx)
    bit[idx] += val;
}
int query(int idx, int bit[], int n) {
  int sum = 0;
  for (; idx > 0; idx -= idx & -idx)
    sum += bit[idx];
  return sum;
}

void answeringQueries(int arr[], int n, Query queries[], int q) {
  int bit[n + 1];
  memset(bit, 0, sizeof(bit));
  int last_visit[MAX];
  memset(last_visit, -1, sizeof(last_visit));
  int ans[q];
  int query_counter = 0;
  for (int i = 0; i < n; i++) {

    if (last_visit[arr[i]] != -1)
      update(last_visit[arr[i]] + 1, -1, bit, n);
    last_visit[arr[i]] = i;
    update(i + 1, 1, bit, n);
    while (query_counter < q && queries[query_counter].r == i) {
      ans[queries[query_counter].idx] =
          query(queries[query_counter].r + 1, bit, n) -
          query(queries[query_counter].l, bit, n);
      query_counter++;
    }
  }
  for (int i = 0; i < q; i++)
    cout << ans[i] << endl;
}
int main() {
  int n, q;
  cin >> n >> q;
  for (int i = 0; i <= n - 1; i++)
    cin >> a[i];
  Query queries[q];
  int t = 0;
  int p = q;
  while (q--) {
    int s, e;
    cin >> s >> e;
    s--;
    e--;
    queries[t].l = s;
    queries[t].r = e;
    queries[t].idx = t;
    t++;
  }
  sort(queries, queries + p, cmp);
  answeringQueries(a, n, queries, p);
  return 0;
}

Error: TLE

Fixed submission:

#include "bits/stdc++.h"
constexpr int N = 5 * 100000 + 8;
using namespace std;
struct custom_hash {
  static uint64_t splitmix64(uint64_t x) {
    x += 0x9e3779b97f4a7c15;
    x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
    x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
    return x ^ (x >> 31);
  }
  size_t operator()(uint64_t x) const {
    static const uint64_t FIXED_RANDOM =
        chrono::steady_clock::now().time_since_epoch().count();
    return splitmix64(x + FIXED_RANDOM);
  }
};
struct Node {
  vector<int> v;
  unordered_map<int, int, custom_hash> g;
};
Node st[2 * N];
int a[N];
Node merge(Node l, Node r) {
  Node temp;
  temp.v = l.v;
  temp.g = l.g;
  for (auto i : r.v) {
    if (temp.g[i] == 0) {
      temp.g[i] = 1;
      temp.v.emplace_back(i);
    }
  }
  return temp;
}
void build(int n) {
  for (int i = 0; i <= n - 1; i++) {
    st[i + n].v.emplace_back(a[i]);
    st[i + n].g[a[i]] = 1;
  }
  for (int i = n - 1; i >= 1; i--) {
    st[i] = merge(st[i << 1], st[(i << 1) | 1]);
  }
}
int query(int l, int r, int n) {
  l += n;
  r += n;
  Node res;
  for (l, r; l < r; l >>= 1, r >>= 1) {
    if (l & 1)
      res = merge(res, st[l++]);
    if (r & 1)
      res = merge(res, st[--r]);
  }
  return (int)res.v.size();
}
int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  int n, q;
  cin >> n >> q;
  for (int i = 0; i <= n - 1; i++)
    cin >> a[i];
  build(n);
  int l, r;
  while (q--) {
    cin >> l >> r;
    l--;
    cout << query(l, r, n) << "\\n";
  }
}