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

#include <algorithm>
#include <cstdio>
#include <iostream>
using namespace std;

const int maxn = 500005;

struct Node {
  int sum;
  int lson, rson;
} a[maxn * 60];
int tot = 1;

void buildtree(int id, int l, int r) {
  if (l == r)
    return;
  a[id].lson = ++tot;
  a[id].rson = ++tot;
  int mid = (l + r) / 2;
  buildtree(a[id].lson, l, mid);
  buildtree(a[id].rson, mid + 1, r);
}

int change(int id, int l, int r, int x, int dl) {
  //	cout<<"change "<<endl;
  int pos = ++tot;
  a[pos] = a[id];
  //	cout<<"sum "<<pos<<' '<<a[pos].sum<<endl;
  if (l == r) {
    a[pos].sum += x;
    return pos;
  }
  int mid = (l + r) / 2;
  if (dl <= mid)
    a[pos].lson = change(a[id].lson, l, mid, x, dl);
  else
    a[pos].rson = change(a[id].rson, mid + 1, r, x, dl);
  a[pos].sum = a[a[pos].lson].sum + a[a[pos].rson].sum;
  return pos;
}

int query(int id, int l, int r, int dl) {
  //	cout<<"query "<<min_<<' '<<max_<<' '<<l<<' '<<r<<' '<<x<<endl;
  if (l == r)
    return a[id].sum;
  int mid = (l + r) / 2;
  if (dl <= mid)
    return a[a[id].rson].sum + query(a[id].lson, l, mid, dl);
  return query(a[id].rson, mid + 1, r, dl);
}

int n, m;

int d[maxn];

int book[maxn], rk[maxn];

int belong[maxn];

int main() {
  int maxd;
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    cin >> d[i];
  }
  buildtree(rk[0], 1, n);
  for (int i = 1; i <= n; ++i) {
    if (belong[d[i]]) {
      int t = change(rk[i - 1], 1, n, -1, belong[d[i]]);
      rk[i] = change(t, 1, n, 1, i);
    } else {
      rk[i] = change(rk[i - 1], 1, n, 1, i);
    }
    belong[d[i]] = i;
  }
  for (int i = 1; i <= m; i++) {
    int l, r;
    cin >> l >> r;
    cout << query(rk[r], 1, n, l) << endl;
  }
  return 0;
}

Error: -11

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
using namespace std;

const int maxn = 500005;

struct Node {
  int sum;
  int lson, rson;
} a[maxn * 30];
int tot = 1;

void buildtree(int id, int l, int r) {
  if (l == r)
    return;
  a[id].lson = ++tot;
  a[id].rson = ++tot;
  int mid = (l + r) / 2;
  buildtree(a[id].lson, l, mid);
  buildtree(a[id].rson, mid + 1, r);
}

int change(int id, int l, int r, int x, int dl) {
  //	cout<<"change "<<endl;
  int pos = ++tot;
  a[pos] = a[id];
  //	cout<<"sum "<<pos<<' '<<a[pos].sum<<endl;
  if (l == r) {
    a[pos].sum += x;
    return pos;
  }
  int mid = (l + r) / 2;
  if (dl <= mid)
    a[pos].lson = change(a[id].lson, l, mid, x, dl);
  else
    a[pos].rson = change(a[id].rson, mid + 1, r, x, dl);
  a[pos].sum = a[a[pos].lson].sum + a[a[pos].rson].sum;
  return pos;
}

int query(int id, int l, int r, int dl) {
  //	cout<<"query "<<min_<<' '<<max_<<' '<<l<<' '<<r<<' '<<x<<endl;
  if (l == r)
    return a[id].sum;
  int mid = (l + r) / 2;
  if (dl <= mid)
    return a[a[id].rson].sum + query(a[id].lson, l, mid, dl);
  return query(a[id].rson, mid + 1, r, dl);
}

int n, m;

int d[maxn];

int book[maxn], rk[maxn];

int belong[maxn];

int main() {
  int maxd;
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    cin >> d[i];
  }
  buildtree(rk[0], 1, n);
  for (int i = 1; i <= n; ++i) {
    if (belong[d[i]]) {
      int t = change(rk[i - 1], 1, n, -1, belong[d[i]]);
      rk[i] = change(t, 1, n, 1, i);
    } else {
      rk[i] = change(rk[i - 1], 1, n, 1, i);
    }
    belong[d[i]] = i;
  }
  for (int i = 1; i <= m; i++) {
    int l, r;
    cin >> l >> r;
    cout << query(rk[r], 1, n, l) << endl;
  }
  return 0;
}