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

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

#define int long long
#define pii pair<int, int>
#define pb push_back
#define vi vector<int>

const int N = 5e5 + 1;
int ans[N];
struct BIT {
  int N;
  vector<int> bit;

  void init(int n) {
    N = n;
    bit.assign(n + 1, 0);
  }

  void update(int idx, int val) {
    while (idx <= N) {
      bit[idx] += val;
      idx += idx & -idx;
    }
  }

  void updateMax(int idx, int val) {
    while (idx <= N) {
      bit[idx] = max(bit[idx], val);
      idx += idx & -idx;
    }
  }

  int pref(int idx) {
    int ans = 0;
    while (idx > 0) {
      ans += bit[idx];
      idx -= idx & -idx;
    }
    return ans;
  }

  int rsum(int l, int r) { return pref(r) - pref(l - 1); }

  int prefMax(int idx) {
    int ans = -2e9;
    while (idx > 0) {
      ans = max(ans, bit[idx]);
      idx -= idx & -idx;
    }
    return ans;
  }
};
/*
BIT b;
b.init(10);
*/

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int n, q;
  cin >> n >> q;
  int c[n + 1];
  for (int i = 1; i <= n; i++) {
    cin >> c[i];
  }
  int a[n + 1] = {};
  memset(a, -1, sizeof a);
  vector<pair<pii, int>> queries;
  int idx = 0;
  while (q--) {
    int l, r;
    cin >> l >> r;
    queries.pb({{r, l}, idx++});
  }

  sort(queries.begin(), queries.end());
  BIT b;
  b.init(n + 1);
  for (int i = 1; i <= queries.front().first.first; i++) {
    if (a[c[i]] == -1) {
      b.update(i, 1);
    } else {
      b.update(a[c[i]], -1);
      b.update(i, 1);
    }
    a[c[i]] = i;
  }
  int last_r = -1;
  for (auto x : queries) {

    int l = x.first.second, r = x.first.first;
    if (last_r != -1) {
      for (int j = last_r; j <= r; j++) {
        if (a[c[j]] == -1) {
          b.update(j, 1);
        } else {
          b.update(a[c[j]], -1);
          b.update(j, 1);
        }
        a[c[j]] = j;
      }
    }
    int res = b.rsum(l, r);
    ans[x.second] = res;
    last_r = r;
  }
  for (int i = 0; i < idx; i++) {
    cout << ans[i] << "\\n";
  }
  return 0;
}


Error: TLE

Fixed submission:

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

#define int long long
#define pii pair<int, int>
#define pb push_back
#define vi vector<int>

const int N = 5e5 + 1;
set<int> a[N], E;
// set<int> tree[N];
vector<set<int>> tree;

#define lc (i << 1)
#define rc (lc | 1)
#define mid ((l + r) >> 1)

set<int> merge(set<int> x, set<int> y) {
  if (x.size() < y.size())
    swap(x, y);
  x.insert(y.begin(), y.end());
  return x;
}

void build(int i, int l, int r) {
  if (l == r) {
    tree[i] = a[l];
    return;
  }
  build(lc, l, mid);
  build(rc, mid + 1, r);
  tree[i] = merge(tree[lc], tree[rc]);
}

set<int> query(int i, int l, int r, int ql, int qr) {
  if (r < ql || l > qr)
    return E;
  if (ql <= l && r <= qr)
    return tree[i];
  set<int> q1 = query(lc, l, mid, ql, qr);
  set<int> q2 = query(rc, mid + 1, r, ql, qr);
  return merge(q1, q2);
}

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int n, q;
  cin >> n >> q;
  tree.resize(4 * n);
  int c[n + 1];
  for (int i = 1; i <= n; i++) {
    cin >> c[i];
    a[i].insert(c[i]);
  }
  build(1, 1, n);
  while (q--) {
    int l, r;
    cin >> l >> r;
    set<int> b = query(1, 1, n, l, r);
    cout << b.size() << endl;
  }
  return 0;
}