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

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep2(i, m, n) for (int i = m; i < (n); ++i)
#define all(x) (x).begin(), (x).end()
inline int popcount(const int x) { return __builtin_popcount(x); }
template <class T> void chmin(T &a, const T &b) noexcept {
  if (b < a)
    a = b;
}
template <class T> void chmax(T &a, const T &b) noexcept {
  if (a < b)
    a = b;
}
template <class T> void drop(const T &x) {
  std::cout << x << endl;
  exit(0);
}
void debug_out() { std::cout << "\\n"; }
template <class T, class... Args>
void debug_out(const T &x, const Args &...args) {
  std::cout << x << " ";
  debug_out(args...);
}
#ifdef _DEBUG
#define debug(...) debug_out(__VA_ARGS__)
#else
#define debug(...)
#endif

struct InitIO {
  InitIO() {
    std::cin.tie(nullptr);
    std::ios_base::sync_with_stdio(false);
    std::cout << std::fixed << std::setprecision(15);
  }
} init_io;

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) {
    assert(i < n);
    for (i++; i <= n; i += i & -i)
      d[i] += x; // LSB:i&(-i)
  }
  T sum(int i) {
    assert(i < n);
    T x = 0;
    for (i++; i; i -= i & -i)
      x += d[i];
    return x;
  }
  T sum(int l, int r) { //[l,r)
    assert(l < n and r <= n);
    return sum(r - 1) - sum(l - 1);
  }
  T operator[](const int &i) const {
    assert(i < n);
    return sum(i, i + 1);
  }
};

int main() {
  int n, q;
  cin >> n >> q;
  vector<int> c(n);
  rep(i, n) {
    cin >> c[i];
    c[i]--;
  }
  using P = pair<pair<int, int>, int>;
  vector<P> query(q);
  rep(i, q) {
    int l, r;
    cin >> l >> r;
    l--;
    r--;
    query[i].second = i;
    query[i].first.first = r;
    query[i].first.second = l;
  }
  sort(all(query));

  BIT<int> bit(n);
  vector<int> last(n, -1);

  auto update = [&](int i) {
    if (last[c[i]] != -1)
      bit.add(last[c[i]], -1);
    last[c[i]] = i;
    bit.add(i);
  };

  int r = query[0].first.first;
  vector<int> ans(q);
  rep(i, r + 1) update(i);
  ans[query[0].second] =
      bit.sum(query[0].first.second, query[0].first.first + 1);
  rep2(i, 1, q) {
    int nr = query[i].first.first;
    rep2(i, r + 1, nr + 1) update(i);
    ans[query[i].second] =
        bit.sum(query[i].first.second, query[i].first.first + 1);
    r = nr;
  }

  rep(i, q) cout << ans[i] << "\\n";
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep2(i, m, n) for (int i = m; i < (n); ++i)
#define all(x) (x).begin(), (x).end()
inline int popcount(const int x) { return __builtin_popcount(x); }
template <class T> void chmin(T &a, const T &b) noexcept {
  if (b < a)
    a = b;
}
template <class T> void chmax(T &a, const T &b) noexcept {
  if (a < b)
    a = b;
}
template <class T> void drop(const T &x) {
  std::cout << x << endl;
  exit(0);
}
void debug_out() { std::cout << "\\n"; }
template <class T, class... Args>
void debug_out(const T &x, const Args &...args) {
  std::cout << x << " ";
  debug_out(args...);
}
#ifdef _DEBUG
#define debug(...) debug_out(__VA_ARGS__)
#else
#define debug(...)
#endif

struct InitIO {
  InitIO() {
    std::cin.tie(nullptr);
    std::ios_base::sync_with_stdio(false);
    std::cout << std::fixed << std::setprecision(15);
  }
} init_io;

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) {
    assert(i < n);
    for (i++; i <= n; i += i & -i)
      d[i] += x; // LSB:i&(-i)
  }
  T sum(int i) {
    assert(i < n);
    T x = 0;
    for (i++; i; i -= i & -i)
      x += d[i];
    return x;
  }
  T sum(int l, int r) { //[l,r)
    assert(l < n and r <= n);
    return sum(r - 1) - sum(l - 1);
  }
  T operator[](const int &i) const {
    assert(i < n);
    return sum(i, i + 1);
  }
};

int main() {
  int n, q;
  cin >> n >> q;
  vector<int> c(n);
  rep(i, n) {
    cin >> c[i];
    c[i]--;
  }
  using P = pair<pair<int, int>, int>;
  vector<P> query(q);
  rep(i, q) {
    int l, r;
    cin >> l >> r;
    l--;
    r--;
    query[i].second = i;
    query[i].first.first = r;
    query[i].first.second = l;
  }
  sort(all(query));

  BIT<int> bit(n);
  vector<int> last(n, -1);

  auto update = [&](int i) {
    if (last[c[i]] != -1)
      bit.add(last[c[i]], -1);
    last[c[i]] = i;
    bit.add(i);
  };

  int r = query[0].first.first;
  vector<int> ans(q);
  rep(i, r + 1) update(i);
  ans[query[0].second] =
      bit.sum(query[0].first.second, query[0].first.first + 1);
  rep2(i, 1, q) {
    int nr = query[i].first.first;
    rep2(i, r + 1, nr + 1) update(i);
    ans[query[i].second] =
        bit.sum(query[i].first.second, query[i].first.first + 1);
  }

  rep(i, q) cout << ans[i] << "\\n";
  return 0;
}