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

#include <bits/stdc++.h>
#define ll long long int
#define ld long double
#define pi pair<int, int>
#define all(x) x.begin(), x.end()
#define allr(x) x.rbegin(), x.rend()
#define sz(x) ((int)x.size())
#define ln(x) ((int)x.length())
#define mp make_pair
#define pb push_back
#define ff first
#define ss second
#define endl '\\n'
#define dbg(x) cout << #x << ": " << x << endl
#define clr(x, v) memset(x, v, sizeof(x))
#define FASTIO                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
using namespace std;
const double eps = 1e-9;
const double PI = acos(-1.0);
const ll mod = 1e9 + 7;
const int MAXN = 5e5 + 5;

int arr[MAXN];
int bit[MAXN];
int last_visit[MAXN];

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

Query queries[MAXN];

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 cp() {
  int n, q;
  cin >> n >> q;
  for (int i = 0; i < n; i++)
    cin >> arr[i];
  for (int i = 0; i < q; i++) {
    int l, r;
    cin >> l >> r;
    l--, r--;
    queries[i] = {l, r, i};
  }
  sort(queries, queries + q, cmp);
  clr(last_visit, -1);

  int ans[q];
  int cnt = 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 (cnt < q && queries[cnt].r == i) {
      ans[queries[cnt].idx] =
          query(queries[cnt].r + 1, bit, n) - query(queries[cnt].l, bit, n);
      cnt++;
    }
  }

  for (int i = 0; i < q; i++)
    cout << ans[i] << endl;
}

int main() {
  FASTIO;
  int t;
  t = 1;
  // cin >> t;
  while (t--) {
    cp();
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define ll long long int
#define ld long double
#define pi pair<int, int>
#define all(x) x.begin(), x.end()
#define allr(x) x.rbegin(), x.rend()
#define sz(x) ((int)x.size())
#define ln(x) ((int)x.length())
#define mp make_pair
#define pb push_back
#define ff first
#define ss second
#define endl '\\n'
#define dbg(x) cout << #x << ": " << x << endl
#define clr(x, v) memset(x, v, sizeof(x))
#define FASTIO                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
using namespace std;
const double eps = 1e-9;
const double PI = acos(-1.0);
const ll mod = 1e9 + 7;
const int MAXN = 5e5 + 5;

int arr[MAXN];

set<int> t[4 * MAXN];

void build(int v, int tl, int tr) {
  if (tl == tr)
    t[v].insert(arr[tl]);
  else {
    int tm = (tl + tr) / 2;
    build(2 * v, tl, tm);
    build(2 * v + 1, tm + 1, tr);
    t[v].insert(all(t[2 * v]));
    t[v].insert(all(t[2 * v + 1]));
  }
}

set<int> query(int v, int tl, int tr, int l, int r) {
  if (l > r)
    return set<int>();
  if (tl == l && tr == r)
    return t[v];
  else {
    int tm = (tl + tr) / 2;
    set<int> left = query(2 * v, tl, tm, l, min(r, tm));
    set<int> right = query(2 * v + 1, tm + 1, tr, max(l, tm + 1), r);
    set<int> res(all(left));
    res.insert(all(right));
    return res;
  }
}

void cp() {
  int n, q;
  cin >> n >> q;
  for (int i = 0; i < n; i++)
    cin >> arr[i];
  build(1, 0, n - 1);
  while (q--) {
    int l, r;
    cin >> l >> r;
    l--, r--;
    cout << sz(query(1, 0, n - 1, l, r)) << endl;
  }
}

int main() {
  FASTIO;
  int t;
  t = 1;
  // cin >> t;
  while (t--) {
    cp();
  }
  return 0;
}