Problem p02599 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

template <typename T> class FenwickTree {
private:
  int n, mx;
  vector<T> bit;

public:
  FenwickTree(int sz) : n(sz + 1), mx(1), bit(n, 0) {
    while (mx * 2 <= n)
      mx *= 2;
  }
  FenwickTree(const vector<T> &v) : n((int)v.size() + 1), mx(1), bit(n, 0) {
    for (int i = 0; i < n - 1; i++)
      add(i, v.at(i));
    while (mx * 2 <= n)
      mx *= 2;
  }
  void add(int i, T x) {
    i++;
    while (i < n)
      bit.at(i) += x, i += i & -i;
  }
  void add(int l, int r, T x) {
    add(l, x);
    add(r + 1, -x);
  }
  T sum(int i) {
    i++;
    T ret = 0;
    while (i > 0)
      ret += bit.at(i), i -= i & -i;
    return ret;
  }
  T sum(int l, int r) { return sum(r) - sum(l - 1); }
  int lower_bound(T w) {
    if (w <= 0)
      return 0;
    int x = 0;
    for (int k = mx; k > 0; k /= 2) {
      if (x + k <= n - 1 && bit.at(x + k) < w) {
        w -= bit.at(x + k);
        x += k;
      }
    }
    return x;
  }
  int upper_bound(T w) {
    if (w < 0)
      return 0;
    int x = 0;
    for (int k = mx; k > 0; k /= 2) {
      if (x + k <= n - 1 && bit.at(x + k) <= w) {
        w -= bit.at(x + k);
        x += k;
      }
    }
    return x;
  }
};

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

  int N, Q;
  cin >> N >> Q;
  vector<int> C(N);
  for (int i = 0; i < N; i++)
    cin >> C.at(i);
  vector<vector<pair<int, int>>> V(N);
  for (int i = 0, l, r; cin >> l >> r; i++) {
    V.at(--r).push_back({--l, i});
  }

  FenwickTree<int> FT(N);
  vector<int> ans(Q);
  vector<int> pre(N, -1);
  for (int r = 0; r < N; r++) {
    if (pre.at(C.at(r)) >= 0)
      FT.add(pre.at(C.at(r)), -1);
    pre.at(C.at(r)) = r;
    FT.add(r, 1);
    for (auto [l, i] : V.at(r))
      ans.at(i) = FT.sum(l, r);
  }
  for (auto a : ans)
    cout << a << "\n";
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

template <typename T> class FenwickTree {
private:
  int n, mx;
  vector<T> bit;

public:
  FenwickTree(int sz) : n(sz + 1), mx(1), bit(n, 0) {
    while (mx * 2 <= n)
      mx *= 2;
  }
  FenwickTree(const vector<T> &v) : n((int)v.size() + 1), mx(1), bit(n, 0) {
    for (int i = 0; i < n - 1; i++)
      add(i, v.at(i));
    while (mx * 2 <= n)
      mx *= 2;
  }
  void add(int i, T x) {
    i++;
    while (i < n)
      bit.at(i) += x, i += i & -i;
  }
  void add(int l, int r, T x) {
    add(l, x);
    add(r + 1, -x);
  }
  T sum(int i) {
    i++;
    T ret = 0;
    while (i > 0)
      ret += bit.at(i), i -= i & -i;
    return ret;
  }
  T sum(int l, int r) { return sum(r) - sum(l - 1); }
  int lower_bound(T w) {
    if (w <= 0)
      return 0;
    int x = 0;
    for (int k = mx; k > 0; k /= 2) {
      if (x + k <= n - 1 && bit.at(x + k) < w) {
        w -= bit.at(x + k);
        x += k;
      }
    }
    return x;
  }
  int upper_bound(T w) {
    if (w < 0)
      return 0;
    int x = 0;
    for (int k = mx; k > 0; k /= 2) {
      if (x + k <= n - 1 && bit.at(x + k) <= w) {
        w -= bit.at(x + k);
        x += k;
      }
    }
    return x;
  }
};

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

  int N, Q;
  cin >> N >> Q;
  vector<int> C(N);
  for (int i = 0; i < N; i++)
    cin >> C.at(i);
  vector<vector<pair<int, int>>> V(N);
  for (int i = 0, l, r; cin >> l >> r; i++) {
    V.at(--r).push_back({--l, i});
  }

  FenwickTree<int> FT(N);
  vector<int> ans(Q);
  vector<int> pre(N + 1, -1);
  for (int r = 0; r < N; r++) {
    if (pre.at(C.at(r)) >= 0)
      FT.add(pre.at(C.at(r)), -1);
    pre.at(C.at(r)) = r;
    FT.add(r, 1);
    for (auto [l, i] : V.at(r))
      ans.at(i) = FT.sum(l, r);
  }
  for (auto a : ans)
    cout << a << "\n";
}