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

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

const int MAX = 1000001;

// structure to store queries
struct Query {
  int l, r, idx;
};

// cmp function to sort queries according to r
bool cmp(Query x, Query y) { return x.r < y.r; }

// updating the bit array
void update(int idx, int val, int bit[], int n) {
  for (; idx <= n; idx += idx & -idx)
    bit[idx] += val;
}

// querying the bit array
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) {
  // initialising bit array
  int bit[n + 1];
  memset(bit, 0, sizeof(bit));

  // holds the rightmost index of any number
  // as numbers of a[i] are less than or equal to 10^6
  int last_visit[MAX];
  memset(last_visit, -1, sizeof(last_visit));

  // answer for each query
  int ans[q];
  int query_counter = 0;
  for (int i = 0; i < n; i++) {
    // If last visit is not -1 update -1 at the
    // idx equal to last_visit[arr[i]]
    if (last_visit[arr[i]] != -1)
      update(last_visit[arr[i]] + 1, -1, bit, n);

    // Setting last_visit[arr[i]] as i and updating
    // the bit array accordingly
    last_visit[arr[i]] = i;
    update(i + 1, 1, bit, n);

    // If i is equal to r of any query  store answer
    // for that query in ans[]
    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() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n, q;
  cin >> n >> q;
  int arr[n];
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
  }
  Query queries[q];
  for (int i = 0; i < q; i++) {
    int l, r;
    cin >> l >> r;
    l--;
    r--;
    queries[i].l = l;
    queries[i].r = r;
    queries[i].idx = i;
  }
  sort(queries, queries + q, cmp);
  answeringQueries(arr, n, queries, q);
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
set<ll> *segment;
void build(int i, ll s, ll e, ll arr[]) {
  if (s == e) {
    segment[i].insert(arr[s]);
    return;
  }
  ll mid = s + (e - s) / 2;
  build(2 * i, s, mid, arr);
  build(1 + 2 * i, 1 + mid, e, arr);
  segment[i].insert(segment[2 * i].begin(), segment[2 * i].end());

  segment[i].insert(segment[2 * i + 1].begin(), segment[2 * i + 1].end());
}
set<ll> query(ll node, ll l, ll r, ll a, ll b) {
  set<ll> left, right, result;
  if (b < l || a > r)
    return result;
  if (a <= l && r <= b)
    return segment[node];
  ll mid = l + (r - l) / 2;
  left = query(2 * node, l, mid, a, b);
  result.insert(left.begin(), left.end());

  right = query(1 + 2 * node, 1 + mid, r, a, b);
  result.insert(right.begin(), right.end());
  return result;
}
void init(ll n) {
  ll h = (int)ceil(log2(n));
  ll p = 1;
  for (int i = 0; i < h; i++) {
    p = 1LL * p * 2;
  }
  h = (1LL * 2 * p) - 1;
  segment = new set<ll>[4 * h];
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll n, q;
  cin >> n >> q;
  ll arr[n + 1000];
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
  }
  init(n);
  build(1, 0, n - 1, arr);
  for (int i = 0; i < q; i++) {
    int l, r;
    cin >> l >> r;
    l--;
    r--;
    set<ll> ans = query(1, 0, n - 1, l, r);
    cout << ans.size() << "\\n";
  }
  return 0;
}