Problem p02599 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <stdexcept>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
using ll = long long;

#include <vector>
namespace ProconLib {

// Summation only
template <typename Int> struct BIT {
private:
  int N;
  std::vector<Int> dat;

public:
  BIT(int N) : N(N), dat(N, 0) {}
  void add(int pos, Int x);
  Int sum(int r);
  Int sum(int l, int r);
};

template <typename Int> void BIT<Int>::add(int pos, Int x) {
  pos++;
  while (pos <= N) {
    dat[pos - 1] += x;
    pos += pos & -pos;
  }
}

template <typename Int> Int BIT<Int>::sum(int r) {
  Int res = 0;
  while (r > 0) {
    res += dat[r - 1];
    r -= r & -r;
  }
  return res;
}

template <typename Int> Int BIT<Int>::sum(int l, int r) {
  return sum(r) - sum(l);
}

}; // namespace ProconLib
using namespace ProconLib;

struct Query {
  int l, r;
  int id;
};

int main() {
  int n, q;
  cin >> n >> q;

  vector<int> c(n);
  for (int i = 0; i < n; i++)
    cin >> c[i], c[i]--;

  vector<vector<Query>> query(n);
  for (int i = 0; i < q; i++) {
    int l, r;
    cin >> l >> r;
    l--;
    query[l].push_back(Query{l, r, i});
  }

  BIT<int> bit(n + 1);
  bit.add(n, n);
  vector<int> lst(n, n);

  vector<int> ans(q);

  for (int i = n - 1; i >= 0; i--) {
    bit.add(lst[c[i]], -1);
    bit.add(i, 1);
    lst[c[i]] = i;
    for (auto que : query[i]) {
      ans[que.id] = bit.sum(que.l, que.r);
    }
  }

  for (int i = 0; i < q; i++)
    cout << ans[i] << endl;
  return 0;
}


Error: WA

Fixed submission:

#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <stdexcept>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
using ll = long long;

#include <vector>
namespace ProconLib {

// Summation only
template <typename Int> struct BIT {
private:
  int N;
  std::vector<Int> dat;

public:
  BIT(int N) : N(N), dat(N, 0) {}
  void add(int pos, Int x);
  Int sum(int r);
  Int sum(int l, int r);
};

template <typename Int> void BIT<Int>::add(int pos, Int x) {
  pos++;
  while (pos <= N) {
    dat[pos - 1] += x;
    pos += pos & -pos;
  }
}

template <typename Int> Int BIT<Int>::sum(int r) {
  Int res = 0;
  while (r > 0) {
    res += dat[r - 1];
    r -= r & -r;
  }
  return res;
}

template <typename Int> Int BIT<Int>::sum(int l, int r) {
  return sum(r) - sum(l);
}

}; // namespace ProconLib
using namespace ProconLib;

struct Query {
  int l, r;
  int id;
};

int main() {
  int n, q;
  cin >> n >> q;

  vector<int> c(n);
  for (int i = 0; i < n; i++)
    cin >> c[i], c[i]--;

  vector<vector<Query>> query(n);
  for (int i = 0; i < q; i++) {
    int l, r;
    cin >> l >> r;
    l--;
    query[l].push_back(Query{l, r, i});
  }

  BIT<int> bit(n + 1);
  bit.add(n, n);
  vector<int> lst(n, n);

  vector<int> ans(n);

  for (int i = n - 1; i >= 0; i--) {
    bit.add(lst[c[i]], -1);
    bit.add(i, 1);
    lst[c[i]] = i;
    for (auto que : query[i]) {
      ans[que.id] = bit.sum(que.l, que.r);
    }
  }

  for (int i = 0; i < n; i++)
    cout << ans[i] << endl;
  return 0;
}