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

#include <algorithm>
#include <cstdio>
#include <map>
#include <vector>
#define LB(v, x) std::lower_bound(v.begin(), v.end(), x) - v.begin() + 1
#define vi std::vector<int>
const int maxN = 6e5 + 10;
int arr[maxN], next[maxN];
std::map<int, int> mp;
vi BIT[maxN], required[maxN];
void update(int index, int val, int n) {
  while (index <= n) {
    int indexj = LB(required[index], val);
    int sz = BIT[index].size();
    while (indexj <= sz) {
      BIT[index][indexj] += 1;
      indexj += (indexj & -indexj);
    }
    index += (index & -index);
  }
}
int query(int index, int val) {
  int ret = 0;
  while (index > 0) {
    int sz = required[index].size(), indexj = sz;
    while (indexj > 0) {
      ret += BIT[index][indexj];
      indexj -= (indexj & -indexj);
    }
    indexj = LB(required[index], val) - 1;
    while (indexj > 0) {
      ret -= BIT[index][indexj];
      indexj -= (indexj & -indexj);
    }
    index -= (index & -index);
  }
  return ret;
}
int main() {
  int n, q;
  scanf("%d%d", &n, &q);
  for (int i = 1; i <= n; i++)
    scanf("%d", &arr[i]);
  for (int i = n; i; i--) {
    next[i] = mp.count(arr[i]) ? mp[arr[i]] : n + 1;
    mp[arr[i]] = i;
  }

  for (int i = 1; i <= n; i++) {
    int index = i;
    while (index <= n) {
      required[index].push_back(next[i]);
      index += (index & -index);
    }
    std::sort(required[i].begin(), required[i].end());
    required[i].erase(unique(required[i].begin(), required[i].end()),
                      required[i].end());
    BIT[i] = vi(required[i].size() + 2, 0);
  }

  for (int i = 1; i <= n; i++)
    update(i, next[i], n);

  for (int i = 1; i <= q; i++) {
    int l, r;
    scanf("%d%d", &l, &r);
    int ret = query(r, r + 1) - query(l - 1, r + 1);
    printf("%d\\n", ret);
  }
}


Error: 0

Fixed submission:

#include <cstdio>
#include <map>
#include <vector>
#include <algorithm>
#define LB(v, x) std::lower_bound(v.begin(), v.end(), x) - v.begin() + 1
#define vi std::vector<int>
const int maxN = 6e5 + 10;
int arr[maxN], next[maxN];
std::map<int, int> mp;
vi BIT[maxN], required[maxN];
void update(int index, int val, int n) {
  while (index <= n) {
    int indexj = LB(required[index], val);
    int sz = BIT[index].size();
    while (indexj <= sz) {
      BIT[index][indexj] += 1;
      indexj += (indexj & -indexj);
    }
    index += (index & -index);
  }
}
int query(int index, int val) {
  int ret = 0;
  while (index > 0) {
    int sz = required[index].size(), indexj = sz;
    while (indexj > 0) {
      ret += BIT[index][indexj];
      indexj -= (indexj & -indexj);
    }
    indexj = LB(required[index], val) - 1;
    while (indexj > 0) {
      ret -= BIT[index][indexj];
      indexj -= (indexj & -indexj);
    }
    index -= (index & -index);
  }
  return ret;
}
int main() {
  int n, q;
  scanf("%d%d", &n, &q);
  for (int i = 1; i <= n; i++)
    scanf("%d", &arr[i]);
  for (int i = n; i; i--) {
    next[i] = mp.count(arr[i]) ? mp[arr[i]] : n + 1;
    mp[arr[i]] = i;
  }

  for (int i = 1; i <= n; i++) {
    int index = i;
    while (index <= n) {
      required[index].push_back(next[i]);
      index += (index & -index);
    }
    std::sort(required[i].begin(), required[i].end());
    required[i].erase(unique(required[i].begin(), required[i].end()),
                      required[i].end());
    BIT[i] = vi(required[i].size() + 1, 0);
  }

  for (int i = 1; i <= n; i++) {
    //	printf("%d ",next[i]);
    update(i, next[i], n);
  }

  for (int i = 1; i <= q; i++) {
    int l, r;
    scanf("%d%d", &l, &r);
    int ret = query(r, r + 1) - query(l - 1, r + 1);
    printf("%d\\n", ret);
  }
}