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

#include <bits/stdc++.h>
#define WHOLE(v) (v).begin(), (v).end()
#define REV_WHOLE(v) (v).rbegin(), (v).rend()
using i64 = int64_t;
using namespace std;
template <class F> auto recursive(F f) {
  return [f](auto... a) { return f(f, a...); };
}
template <class T>
using rev_priority_queue = priority_queue<T, vector<T>, greater<T>>;
template <class T> using vector2d = vector<vector<T>>;
struct fixprec {
  int p;
  fixprec(int p) : p(p) {}
};
ostream &operator<<(ostream &o, fixprec f) {
  return o << fixed << setprecision(f.p);
}

/*!* [fenwick] *!*/
/*+* フェニック木
     長さNの配列の部分和と更新をO(log N)で行う *+*/
template <typename T, typename F = function<T(T, T)>> struct Fenwick {
  int N;
  vector<T> array;
  const F f;
  const T id;
  Fenwick() {}
  Fenwick(int len, const F f, const T &id) : N(len), f(f), id(id) {
    array.assign(N, id);
  }
  inline int LSB(int x) { return x & (-x); }
  void update(int i, T val) {
    for (; i < N; i += LSB(i + 1)) {
      array[i] = f(array[i], val);
    }
  }
  // sum of the elements [0:i]
  T sum(int i) {
    T s = id;
    for (i++; i; i -= LSB(i)) {
      s = f(s, array[i - 1]);
    }
    return s;
  }
};

int main() {
  using R = tuple<int, int, int>;
  int N, Q;
  cin >> N >> Q;

  vector<int> C(N);
  vector<R> range(Q); // (r: 右端, l: 左端, j: 区間番号)

  for (int &c : C)
    cin >> c;
  for (int j = 0; j < Q; j++) {
    int l, r;
    cin >> l >> r;
    l--, r--;
    range[j] = make_tuple(r, l, j);
  }

  sort(WHOLE(range));

  auto add = [](int x, int y) -> int { return x + y; };
  Fenwick<int, decltype(add)> tree(N, add, 0);
  map<int, int> pos; // 色 -> 最右の位置

  vector<int> ans(Q);
  int q = 0;
  for (int i = 0; i < N; i++) {
    int c = C[i];
    if (pos.count(c)) {
      tree.update(pos[c], -1);
    }
    tree.update(i, 1);
    pos[c] = i;
    while (q < Q && get<0>(range[q]) <= i) {
      int l, r, j;
      tie(r, l, j) = range[q];
      ans[j] = tree.sum(r) - tree.sum(l - 1);
      q++;
    }
  }
  for (int x : ans)
    cout << x << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define WHOLE(v) (v).begin(), (v).end()
#define REV_WHOLE(v) (v).rbegin(), (v).rend()
using i64 = int64_t;
using namespace std;
template <class F> auto recursive(F f) {
  return [f](auto... a) { return f(f, a...); };
}
template <class T>
using rev_priority_queue = priority_queue<T, vector<T>, greater<T>>;
template <class T> using vector2d = vector<vector<T>>;
struct fixprec {
  int p;
  fixprec(int p) : p(p) {}
};
ostream &operator<<(ostream &o, fixprec f) {
  return o << fixed << setprecision(f.p);
}

/*!* [fenwick] *!*/
/*+* フェニック木
     長さNの配列の部分和と更新をO(log N)で行う *+*/
template <typename T, typename F = function<T(T, T)>> struct Fenwick {
  int N;
  vector<T> array;
  const F f;
  const T id;
  Fenwick() {}
  Fenwick(int len, const F f, const T &id) : N(len), f(f), id(id) {
    array.assign(N, id);
  }
  inline int LSB(int x) { return x & (-x); }
  void update(int i, T val) {
    for (; i < N; i += LSB(i + 1)) {
      array[i] = f(array[i], val);
    }
  }
  // sum of the elements [0:i]
  T sum(int i) {
    T s = id;
    for (i++; i; i -= LSB(i)) {
      s = f(s, array[i - 1]);
    }
    return s;
  }
};

int main() {
  using Set = map<int, int>;
  int N, Q;
  cin >> N >> Q;
  auto concat = [&](Set x, Set y) -> Set {
    Set z;
    for (auto a : x)
      z[a.first] += a.second;
    for (auto a : y)
      z[a.first] += a.second;
    return z;
  };
  Fenwick<Set, decltype(concat)> tree(N, concat, Set());
  for (int i = 0; i < N; i++) {
    int c;
    cin >> c;
    Set s;
    s[c] = 1;
    tree.update(i, s);
  }
  for (int q = 0; q < Q; q++) {
    int l, r;
    cin >> l >> r;
    l--, r--;
    Set R = tree.sum(r);
    Set L = tree.sum(l - 1);
    int ans = 0;
    for (auto a : R) {
      if (L[a.first] < a.second) {
        ans++;
      }
    }
    cout << ans << endl;
  }
  return 0;
}