Problem p02599 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)n; ++i)
#define rrep(i, n) for (int i = (int)n - 1; i >= 0; --i)
using namespace std;
using ll = long long;
template <typename T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T> inline bool chmin(T &a, T b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T> vector<T> make_vec(size_t a) { return vector<T>(a); }
template <typename T, typename... Ts> auto make_vec(size_t a, Ts... ts) {
  return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...));
}
template <typename T, typename U, typename... V>
typename enable_if<is_same<T, U>::value>::type fill_v(U &u, const V... v) {
  u = U(v...);
}
template <typename T, typename U, typename... V>
typename enable_if<!is_same<T, U>::value>::type fill_v(U &u, const V... v) {
  for (auto &e : u)
    fill_v<T>(e, v...);
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  int n, q;
  cin >> n >> q;
  vector<int> a(n);
  rep(i, n) cin >> a[i], a[i]--;

  vector<int> ql(q), qr(q), qi(q);
  rep(i, q) cin >> ql[i] >> qr[i], ql[i]--;
  iota(qi.begin(), qi.end(), 0);

  int width = sqrt(q / 2 + 1);
  sort(qi.begin(), qi.end(), [&](int i, int j) {
    if (ql[i] / width != ql[j] / width)
      return ql[i] < ql[j];
    return qr[i] < qr[j];
  });

  vector<int> cnt(n), ans(q);
  int sum = 0;
  int l = 0, r = 0;

  auto add = [&](int i) {
    if (cnt[a[i]]++ == 0)
      sum++;
  };
  auto del = [&](int i) {
    if (--cnt[a[i]] == 0)
      --sum;
  };

  for (int i : qi) {
    while (ql[i] < l)
      add(--l);
    while (r < qr[i])
      add(r++);
    while (l < ql[i])
      del(l++);
    while (qr[i] < r)
      del(--r);
    ans[i] = sum;
  }

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


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)n; ++i)
#define rrep(i, n) for (int i = (int)n - 1; i >= 0; --i)
using namespace std;
using ll = long long;
template <typename T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T> inline bool chmin(T &a, T b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T> vector<T> make_vec(size_t a) { return vector<T>(a); }
template <typename T, typename... Ts> auto make_vec(size_t a, Ts... ts) {
  return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...));
}
template <typename T, typename U, typename... V>
typename enable_if<is_same<T, U>::value>::type fill_v(U &u, const V... v) {
  u = U(v...);
}
template <typename T, typename U, typename... V>
typename enable_if<!is_same<T, U>::value>::type fill_v(U &u, const V... v) {
  for (auto &e : u)
    fill_v<T>(e, v...);
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  int n, q;
  cin >> n >> q;
  vector<int> a(n);
  rep(i, n) cin >> a[i], a[i]--;

  vector<int> ql(q), qr(q), qi(q);
  rep(i, q) cin >> ql[i] >> qr[i], ql[i]--;
  iota(qi.begin(), qi.end(), 0);

  int width = sqrt(q);
  sort(qi.begin(), qi.end(), [&](int i, int j) {
    if (ql[i] / width != ql[j] / width)
      return ql[i] < ql[j];
    return qr[i] < qr[j];
  });

  vector<int> cnt(n), ans(q);
  int sum = 0;
  int l = 0, r = 0;

  auto add = [&](int i) {
    if (cnt[a[i]]++ == 0)
      sum++;
  };
  auto del = [&](int i) {
    if (--cnt[a[i]] == 0)
      --sum;
  };

  for (int i : qi) {
    while (ql[i] < l)
      add(--l);
    while (r < qr[i])
      add(r++);
    while (l < ql[i])
      del(l++);
    while (qr[i] < r)
      del(--r);
    ans[i] = sum;
  }

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