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

#include <algorithm>
#include <iostream>
#include <numeric>
#include <vector>
using namespace std;

// Bit Indexed Tree
template <typename T> struct BIT {
  std::vector<T> dat;

  BIT(int n) : dat(n + 1, 0) {}
  void init(int n) { dat.assign(n + 1, 0); }

  inline void add(int a, T x) {
    for (auto i = a; i < dat.size(); i += i & -i) {
      dat[i] += x;
    }
  }

  // partial sum of [1,to) (1-origin).
  inline T sum(int to) {
    T ret{0};
    for (auto i = to; i > 0; i -= i & -i) {
      ret += dat[i];
    }
    return ret;
  }

  // partial sum of [from,to) (1-origin).
  inline T sum(int from, int to) { return sum(to - 1) - sum(from - 1); }

  // find index at which a_1 + ... + a_i >= w
  int lower_bound(T w) {
    if (w <= 0) {
      return 0;
    } else {
      int x = 0, r = 1;
      while (r < dat.size())
        r <<= 1;
      for (auto len = r; len; len >>= 1) {
        if (x + len < dat.size() && dat[x + len] < w) {
          w -= dat[x + len];
          x += len;
        }
      }
      return x + 1;
    }
  }

  // print all
  void print() {
    for (auto i = 1; i < dat.size(); ++i) {
      std::cout << (i == 1 ? "" : ", ") << sum(i, i + 1);
    }
    std::cout << std::endl;
  }
};

int main() {
  int N, Q;
  cin >> N >> Q;
  vector<int> c(N);
  for (int &x : c)
    cin >> x;
  vector<pair<int, int>> q(Q);
  vector<int> ans(Q), ind(Q);
  iota(begin(ind), end(ind), 0);
  for (auto &[l, r] : q)
    cin >> l >> r;
  sort(begin(ind), end(ind),
       [&q](int x, int y) { return q[x].second < q[y].second; });
  vector<int> last(N);
  int lastR{};
  BIT<int> b(N + 1);
  for (int i = 0; i != Q; ++i) {
    int R = q[ind[i]].second;
    if (R > lastR) {
      for (int j = lastR; j < R; ++j) {
        if (last[c[j]])
          b.add(last[c[j]], -1);
        last[c[j]] = j + 1;
        b.add(last[c[j]], 1);
      }
      lastR = R;
    }
    ans[ind[i]] = b.sum(q[ind[i]].first, q[ind[i]].second);
  }
  for (int x : ans)
    cout << x + 1 << endl;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <numeric>
#include <vector>
using namespace std;

// Bit Indexed Tree
template <typename T> struct BIT {
  std::vector<T> dat;

  BIT(int n) : dat(n + 1, 0) {}
  void init(int n) { dat.assign(n + 1, 0); }

  inline void add(int a, T x) {
    for (auto i = a; i < dat.size(); i += i & -i) {
      dat[i] += x;
    }
  }

  // partial sum of [1,to) (1-origin).
  inline T sum(int to) {
    T ret{0};
    for (auto i = to; i > 0; i -= i & -i) {
      ret += dat[i];
    }
    return ret;
  }

  // partial sum of [from,to) (1-origin).
  inline T sum(int from, int to) { return sum(to - 1) - sum(from - 1); }

  // find index at which a_1 + ... + a_i >= w
  int lower_bound(T w) {
    if (w <= 0) {
      return 0;
    } else {
      int x = 0, r = 1;
      while (r < dat.size())
        r <<= 1;
      for (auto len = r; len; len >>= 1) {
        if (x + len < dat.size() && dat[x + len] < w) {
          w -= dat[x + len];
          x += len;
        }
      }
      return x + 1;
    }
  }

  // print all
  void print() {
    for (auto i = 1; i < dat.size(); ++i) {
      std::cout << (i == 1 ? "" : ", ") << sum(i, i + 1);
    }
    std::cout << std::endl;
  }
};

int main() {
  int N, Q, l, r;
  cin >> N >> Q;
  vector<int> c(N);
  for (int i = 0; i < N; i++) {
    cin >> c[i];
  }

  vector<pair<int, int>> q;
  vector<int> ans(Q), ind(Q);
  iota(begin(ind), end(ind), 0);
  while (cin >> l >> r)
    q.emplace_back(l, r);
  sort(begin(ind), end(ind),
       [&q](int x, int y) { return q[x].second < q[y].second; });

  vector<int> last(N, 0);
  int lastR{};
  BIT<int> b(N + 1);
  for (int i = 0; i != Q; ++i) {
    int R = q[ind[i]].second;
    if (R > lastR) {
      for (int j = lastR; j < R; ++j) {
        if (last[c[j]])
          b.add(last[c[j]], -1);
        last[c[j]] = j + 1;
        b.add(last[c[j]], 1);
      }
    }
    ans[ind[i]] = b.sum(q[ind[i]].first, q[ind[i]].second);
  }
  for (int x : ans)
    cout << x + 1 << endl;
}