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

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

#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define rep3(i, m, n) for (int(i) = m; (i) <= (n); (i)++)
#define rep3rev(i, m, n) for (int(i) = m; (i) >= (n); (i)--)
#define all(a) (a.begin()), (a.end())
#define rall(a) (a.rbegin()), (a.rend())
#define fi first
#define se second
#define pb push_back
#define eb emplace_back

using ll = long long;
using vll = vector<ll>;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using P = pair<int, int>;
using TP = tuple<int, int, int>;

// Binary Indexed Tree (Fenwick Tree)
// https://youtu.be/lyHk98daDJo?t=7960
template <typename T> struct BIT {
  int n;
  vector<T> d;
  BIT(int n = 0) : n(n), d(n + 1) {}
  void add(int i, T x = 1) {
    for (i++; i <= n; i += i & -i) {
      d[i] += x;
    }
  }
  T sum(int i) {
    T x = 0;
    for (i++; i; i -= i & -i) {
      x += d[i];
    }
    return x;
  }
  T sum(int l, int r) { return sum(r - 1) - sum(l - 1); }
};

void Main() {
  int n, q;
  cin >> n >> q;

  vi c(n);
  BIT<int> bit(n);
  vector<bool> seen(n, false);
  vvi color(n);

  rep(i, n) {
    cin >> c[i];
    c[i]--;
    color[c[i]].pb(i);
  }

  /*
  cout << "color[i]_confirm: " << endl;;
  rep(i, 3) {
      cout << i << ": ";
      for(auto x : color[i]) cout << x << " ";
      cout << endl;
  }
  //*/

  rep3rev(i, n - 1, 0) {
    if (!seen[c[i]])
      bit.add(i, 1);
    seen[c[i]] = true;
  }

  vector<TP> Q(q);
  rep(i, q) {
    int l, r;
    cin >> l >> r;
    l--;
    r--;
    get<0>(Q[i]) = l;
    get<1>(Q[i]) = r;
    get<2>(Q[i]) = i;
  }

  // r の降順でソート
  sort(all(Q), [&](TP x, TP y) { return get<1>(x) > get<1>(y); });

  // for(auto [l, r, i] : Q) cout << l << " " << r << " " << i << endl;

  vi ans(q);
  int rnow = n - 1;
  int rnext;

  rep(i, q) {
    /*
    cout << "BIT_confirm: ";
    rep(j, 4) cout << bit.sum(j, j+1) << " ";
    cout << endl;
    //*/
    auto [l, r, id] = Q[i];
    while (rnow != r) {
      color[c[rnow]].pop_back();
      /*
      cout << "c[rnow]_confirm: ";
      for(auto x : color[c[rnow]]) cout << x << " ";
      cout << endl;
      //*/
      if (color[c[rnow]].size() > 0) {
        rnext = color[c[rnow]].back();
        // cout << "rnext = " << rnext << endl;
        bit.add(rnext, 1);
      }
      rnow--;
    }
    ans[id] = bit.sum(l, r + 1);
  }

  rep(i, q) cout << ans[i] << endl;
  return;
}

int main() {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(15);
  Main();
  return 0;
}

Error: TLE

Fixed submission:

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

#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define rep3(i, m, n) for (int(i) = m; (i) <= (n); (i)++)
#define rep3rev(i, m, n) for (int(i) = m; (i) >= (n); (i)--)
#define all(a) (a.begin()), (a.end())
#define rall(a) (a.rbegin()), (a.rend())
#define fi first
#define se second
#define pb push_back
#define eb emplace_back

using ll = long long;
using vll = vector<ll>;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using P = pair<int, int>;

vector<set<int>> st;

set<int> unite(set<int> x, set<int> y) {
  set<int> res;
  for (auto i : x)
    res.insert(i);
  for (auto i : y)
    res.insert(i);
  return res;
}

set<int> query(int a, int b, int k, int l, int r) {
  // [a, b) の区間に対するクエリについて
  // ノード k （区間 [l, r) 担当）が答える
  if (r <= a || b <= l)
    return {}; // 区間が被らない場合は INF を返す
  if (a <= l && r <= b)
    return st[k]; // ノード k の担当範囲がクエリ区間 [a, b) に完全に含まれる
  else {
    set<int> c1 = query(a, b, 2 * k + 1, l, (l + r) / 2); // 左の子に値を聞く
    set<int> c2 = query(a, b, 2 * k + 2, (l + r) / 2, r); // 右の子に値を聞く
    return unite(c1, c2); // 左右の子の値の min を取る
  }
}

void Main() {
  int n, q, nn;
  scanf("%d%d", &n, &q);
  nn = 1;
  while (nn < n)
    nn *= 2; // 葉の数を計算（n以上の最小の2冪数）
  st.resize(2 * nn - 1);

  rep(i, n) {
    int a;
    scanf("%d", &a);
    st[nn + i - 1].insert(a);
  }

  rep3rev(i, nn - 2, 1) st[i] = unite(st[2 * i + 1], st[2 * i + 2]);

  /*
  rep(i, 2*nn-1){
      cout << i << ": ";
      for(auto x : st[i]) cout << x << " ";
      cout << endl;
  }
  */

  rep(i, q) {
    int l, r;
    scanf("%d%d", &l, &r);
    l--;
    r--;
    set<int> ans = query(l, r + 1, 0, 0, nn);
    printf("%d\\n", ans.size());
  }
  return;
}

int main() {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(15);
  Main();
  return 0;
}