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

// Help from
// https://www.geeksforgeeks.org/queries-number-distinct-elements-subarray/

#include <bits/stdc++.h>

using namespace std;

struct Query {
  int l, r, index;
};

const int MAXN = 5e5 + 1;

vector<long long> input_vector(MAXN);
vector<long long> bit(MAXN, 0);
vector<Query> queries(MAXN);

bool compare(const Query &x, const Query &y) { return x.r < y.r; }

void update(int index, int value, int n) {
  for (; index <= n; index += index & -index)
    bit[index] += value;
}

long long query(int index, int n) {
  long long sum = 0;
  for (; index > 0; index -= index & -index) {
    sum += bit[index];
  }
  return sum;
}

void answer_queries(int n, int q) {
  vector<int> last_visit(MAXN, -1);
  vector<long long> answer(q);

  int query_counter = 0;

  for (int i = 0; i < n; i++) {
    if (last_visit[input_vector[i]] != -1)
      update(last_visit[input_vector[i]] + 1, -1, n);

    last_visit[input_vector[i]] = i;
    update(i + 1, 1, n);

    while (query_counter < q && queries[query_counter].r == i) {
      answer[queries[query_counter].index] =
          query(queries[query_counter].r + 1, n) -
          query(queries[query_counter].l, n);
      query_counter++;
    }
  }

  for (int i = 0; i < q; i++) {
    cout << answer[i] << "\\n";
  }
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  int number_of_elements;
  int number_of_queries;

  cin >> number_of_elements >> number_of_queries;

  input_vector.resize(number_of_elements);
  queries.resize(number_of_queries);

  for (int i = 0; i < number_of_elements; i++) {
    cin >> input_vector[i];
  }

  int l;
  int r;

  for (int i = 0; i < number_of_queries; i++) {
    cin >> l >> r;
    queries[i] = {l - 1, r - 1, i};
  }

  sort(queries.begin(), queries.end(), compare);
  answer_queries(number_of_elements, number_of_queries);

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

const int MAXN = 5e5;
vector<int> input_vector(MAXN);
set<int> segment_tree[4 * MAXN];

void build(int i, int s, int e) {

  if (s == e) {
    segment_tree[i].insert(input_vector[s]);
    return;
  }

  build(2 * i, s, (s + e) / 2);
  build(1 + 2 * i, 1 + (s + e) / 2, e);

  segment_tree[i].insert(segment_tree[2 * i].begin(),
                         segment_tree[2 * i].end());

  segment_tree[i].insert(segment_tree[2 * i + 1].begin(),
                         segment_tree[2 * i + 1].end());
}

set<int> query(int node, int l, int r, int a, int b) {
  set<int> left, right, result;

  if (b < l || a > r)
    return result;

  if (a <= l && r <= b)
    return segment_tree[node];

  left = query(2 * node, l, (l + r) / 2, a, b);
  result.insert(left.begin(), left.end());

  right = query(1 + 2 * node, 1 + (l + r) / 2, r, a, b);
  result.insert(right.begin(), right.end());

  return result;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  int numberOfElements;
  int numberOfQueries;

  cin >> numberOfElements >> numberOfQueries;

  input_vector.resize(numberOfElements);

  for (int i = 0; i < numberOfElements; i++) {
    cin >> input_vector[i];
  }

  build(1, 0, numberOfElements - 1);

  int l;
  int r;

  while (numberOfQueries--) {
    cin >> l >> r;
    cout << query(1, 0, numberOfElements - 1, l - 1, r - 1).size() << "\\n";
  }

  return 0;
}