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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 5e5 + 5;
const int MOD = 1e9 + 7;

int a[maxn], c[maxn];
int mp[maxn];
int n;
struct qq {
  int l, r;
  int index;
} q[maxn];
int cmp(qq x, qq y) { return x.r < y.r; }
int lowbit(int x) { return x & (-x); }
void update(int i, int k) {
  int x = i;
  while (i <= n) {
    c[i] += k;
    i += lowbit(i);
  }
}
ll getsum(int i) {
  ll ans = 0;
  int x = i;
  while (i > 0) {
    ans += c[i];
    i -= lowbit(i);
  }
  return ans;
}
int ans[maxn];
int main() {
  int m;
  cin >> n >> m;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  for (int i = 1; i <= m; i++) {
    cin >> q[i].l >> q[i].r;
    q[i].index = i;
  }
  sort(q + 1, q + 1 + m, cmp);
  int next = 1;
  for (int i = 1; i <= m; i++) {
    for (int j = next; j <= q[i].r; j++) {
      if (mp[a[j]]) {
        update(mp[a[j]], -1);
      }
      update(j, 1);
      mp[a[j]] = j;
    }
    next = q[i].r + 1;
    ans[q[i].index] = getsum(q[i].r) - getsum(q[i].l - 1);
  }
  for (int i = 1; i <= m; i++)
    cout << ans[i] << endl;
}


Error: -11

Fixed submission:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define PI acos(-1.0)
#define LL long long
#define ULL unsigned long long
#define INF 0x3f3f3f3f
#define mm(a, b) memset(a, b, sizeof(a))
#define PP puts("*********************");
template <class T> T f_abs(T a) { return a > 0 ? a : -a; }
template <class T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }
template <class T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
// 0x3f3f3f3f3f3f3f3f
// 0x3f3f3f3f

const int maxn = 5e5 + 50;
const int maxm = maxn * 30;
int arr[maxn]; // 原数组
int root[maxn], lson[maxm], rson[maxm],
    cnt[maxm]; // 根节点，左儿子，右儿子，区间内的数出现次数的前缀和
int tot, n;
void init() {
  tot = 1;
  root[0] = lson[0] = rson[0] = cnt[0] = 0; // 建一棵空树
}
int update(int rt, int pos, int val) {
  int newrt = tot++, temp = newrt;
  cnt[newrt] = cnt[rt] + val;
  int l = 1, r = n;
  while (l < r) {
    int mid = (l + r) / 2;
    if (pos <= mid) {
      r = mid;
      lson[newrt] = tot++;
      rson[newrt] = rson[rt];
      newrt = lson[newrt];
      rt = lson[rt];
    } else {
      l = mid + 1;
      lson[newrt] = lson[rt];
      rson[newrt] = tot++;
      newrt = rson[newrt];
      rt = rson[rt];
    }
    cnt[newrt] = cnt[rt] + val;
  }
  return temp;
}
int query(int rt, int pos) {
  int l = 1, r = n;
  int res = 0;
  while (l < r) {
    int mid = (l + r) / 2;
    if (pos <= mid) {
      r = mid;
      res += cnt[rson[rt]];
      rt = lson[rt];
    } else {
      l = mid + 1;
      rt = rson[rt];
    }
  }
  res += cnt[rt];
  return res;
}
map<int, int> mp;
int main() {

  int m, L, R;
  scanf("%d", &n);
  scanf("%d", &m);

  for (int i = 1; i <= n; i++)
    scanf("%d", &arr[i]);
  init();
  mp.clear();
  for (int i = 1; i <= n; i++) {
    if (mp.count(arr[i])) {
      root[i] = update(root[i - 1], mp[arr[i]], -1);
      root[i] = update(root[i], i, 1);
    } else
      root[i] = update(root[i - 1], i, 1);
    mp[arr[i]] = i;
  }
  // scanf("%d",&m);
  while (m--) {
    scanf("%d%d", &L, &R);
    int ans = query(root[R], L);
    printf("%d\\n", ans);
  }

  return 0;
}