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;

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++;
    }
  }

  // print answer for each query
  for (int i = 0; i < q; i++)
    cout << ans[i] << endl;
}

// driver code
int main() {
  int n, q;
  cin >> n >> q;
  int a[n];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    a[i]--;
  }
  Query queries[q];
  int l, r;
  for (int i = 0; i < q; i++) {
    cin >> l >> r;
    l--;
    r--;
    queries[i].l = l;
    queries[i].r = r;
    queries[i].idx = i;
  }
  sort(queries, queries + q, cmp);
  answeringQueries(a, n, queries, q);
  return 0;
}


Error: TLE

Fixed submission:

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

#define ll long long int
#define pb push_back
#define fi first
#define se second
#define mp make_pair
#define mt make_tuple
#define all(A) A.begin(), A.end()
#define sz(A) (int)A.size()
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int, int> pii;
typedef tuple<int, int, int> tii;
const ll mod = 1e9 + 7;
// const ll mod = 998244353;
const int inf = 1e9;
// const ll inf = 1e18;
const int N = 5e5;
int a[N + 1];
set<int> seg[4 * N + 5];

void build(int l, int r, int p = 1) {
  if (l == r) {
    seg[p].insert(a[l]);
    return;
  }
  int md = (l + r) / 2;
  build(l, md, 2 * p);
  build(md + 1, r, 2 * p + 1);
  seg[p].insert(all(seg[2 * p]));
  seg[p].insert(all(seg[2 * p + 1]));
}

set<int> query(int l, int r, int lo, int hi, int p = 1) {
  set<int> x, y;
  if (lo > r || hi < l)
    return x;
  if (lo >= l && hi <= r)
    return seg[p];
  int md = (lo + hi) / 2;
  x = query(l, r, lo, md, 2 * p);
  y = query(l, r, md + 1, hi, 2 * p + 1);
  set<int> res;
  res.insert(all(x));
  res.insert(all(y));
  return res;
}

void solve() {
  int n, q;
  cin >> n >> q;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  build(1, n);
  while (q--) {
    int l, r;
    cin >> l >> r;
    set<int> x = query(l, r, 1, n);
    cout << sz(x) << "\\n";
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
#ifndef ONLINE_JUDGE
  freopen("in.txt", "r", stdin);
#endif
  int t = 1;
  while (t--) {
    solve();
  }
  return 0;
}