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

#include <algorithm>
#include <cassert>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstdlib>
#include <deque>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
const unsigned int BIT_ARRAY_SIZE = 500005;
ll BIT_ARRAY[BIT_ARRAY_SIZE];
int l = BIT_ARRAY_SIZE;

void BIT_update(int i, ll val) {
  while (i <= l) {
    BIT_ARRAY[i] += val;
    i = i + (i & (-i));
  }
}

ll BIT_query(int i) {
  ll s = 0;
  while (i > 0) {
    s += BIT_ARRAY[i];
    i = i - (i & (-i));
  }
  return s;
}
int lastpos[BIT_ARRAY_SIZE * 10];
int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  int n, x, y, q;
  cin >> n >> q;
  vector<int> v(n + 1);
  for (int i = 1; i <= n; i++)
    cin >> v[i];
  vector<pair<pair<int, int>, int>> Q;
  vector<int> ans(q + 1);
  for (int i = 1; i <= q; i++) {
    cin >> x >> y;
    Q.push_back(make_pair(make_pair(y, x), i));
  }
  sort(Q.begin(), Q.end());
  int pos = 1;
  for (int i = 0; i < q; i++) {
    while (pos <= Q[i].first.first) {
      if (lastpos[v[pos]] != 0)
        BIT_update(lastpos[v[pos]], -1);
      BIT_update(pos, 1);
      lastpos[v[pos]] = pos;
      pos++;
    }
    ans[Q[i].second] =
        BIT_query(Q[i].first.first) - BIT_query(Q[i].first.second - 1);
  }
  for (int i = 1; i <= q; i++)
    cout << ans[i] << "\\n";
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cassert>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstdlib>
#include <deque>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
const unsigned int BIT_ARRAY_SIZE = 100005;
ll BIT_ARRAY[BIT_ARRAY_SIZE];
int l = BIT_ARRAY_SIZE;

void BIT_update(int i, ll val) {
  while (i <= l) {
    BIT_ARRAY[i] += val;
    i = i + (i & (-i));
  }
}

ll BIT_query(int i) {
  ll s = 0;
  while (i > 0) {
    s += BIT_ARRAY[i];
    i = i - (i & (-i));
  }
  return s;
}
int lastpos[BIT_ARRAY_SIZE * 10];
int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  int n, x, y, q;
  cin >> n >> q;
  vector<int> v(n + 1);
  for (int i = 1; i <= n; i++)
    cin >> v[i];
  vector<pair<pair<int, int>, int>> Q;
  vector<int> ans(q + 1);
  for (int i = 1; i <= q; i++) {
    cin >> x >> y;
    Q.push_back(make_pair(make_pair(y, x), i));
  }
  sort(Q.begin(), Q.end());
  int pos = 1;
  for (int i = 0; i < q; i++) {
    while (pos <= Q[i].first.first) {
      if (lastpos[v[pos]] != 0)
        BIT_update(lastpos[v[pos]], -1);
      BIT_update(pos, 1);
      lastpos[v[pos]] = pos;
      pos++;
    }
    ans[Q[i].second] =
        BIT_query(Q[i].first.first) - BIT_query(Q[i].first.second - 1);
  }
  for (int i = 1; i <= q; i++)
    cout << ans[i] << "\\n";
}