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

#include <bits/stdc++.h>
using namespace std;

const int MAX = 5e5 + 1;

struct Query {
  int l, r, idx;
  Query(int left, int right, int index) : l(left), r(right), idx(index) {}
};

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(vector<int> &arr, int n, vector<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;
}

void solve() {
  int N, Q;
  cin >> N >> Q;
  vector<int> C(N);
  for (int &c : C)
    cin >> c;
  vector<Query> q;
  for (int i = 0; i < Q; ++i) {
    int l, r;
    cin >> l >> r;
    l--;
    r--;
    q.emplace_back(l, r, i);
  }
  sort(q.begin(), q.end(), cmp);
  answeringQueries(C, N, q, Q);
}

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

  int t = 1;
  while (t--) {
    solve();
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

vector<set<int>> tree;

void buildSegmentTree(int i, int start, int end, vector<int> &arr) {
  if (start == end) {
    tree[i].insert(arr[start]);
    return;
  }
  buildSegmentTree(2 * i, start, (start + end) / 2, arr);
  buildSegmentTree(1 + 2 * i, 1 + (start + end) / 2, end, arr);
  tree[i].insert(tree[2 * i].begin(), tree[2 * i].end());
  tree[i].insert(tree[2 * i + 1].begin(), tree[2 * i + 1].end());
}

set<int> querySegmentTree(int node, int l, int r, int a, int b) {
  set<int> st_left, st_right, ans;
  if (b < l || a > r)
    return ans;
  if (a <= l && r <= b)
    return tree[node];
  st_left = querySegmentTree(2 * node, l, (l + r) / 2, a, b);
  ans.insert(st_left.begin(), st_left.end());
  st_right = querySegmentTree(1 + 2 * node, 1 + (l + r) / 2, r, a, b);
  ans.insert(st_right.begin(), st_right.end());
  return ans;
}

void solve() {
  int N, Q;
  cin >> N >> Q;

  int h = (int)ceil(log2(N));
  h = (2 * (pow(2, h))) - 1;
  tree = vector<set<int>>(h + 1);

  vector<int> C(N);
  for (int &c : C)
    cin >> c;
  buildSegmentTree(1, 0, N - 1, C);
  vector<int> v(Q);
  for (int i = 0; i < Q; ++i) {
    int l, r;
    cin >> l >> r;
    l--;
    r--;
    const set<int> &ans = querySegmentTree(1, 0, N - 1, l, r);
    v[i] = ans.size();
  }
  for (int a : v) {
    cout << a << "\\n";
  }
}

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

  int t = 1;
  while (t--) {
    solve();
  }
}