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

#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;
int a[500010], b[500010], c[500010], n, q0, now, last[500010], ans[500010];
struct query {
  int l, r, id, ans;
} q[500010];
bool cmp(const query &x, const query &y) { return x.r < y.r; }
struct node {
  int s[2];
  int val, rd;
  int sz, cnt;
} t[500010];
int root, num;
int op, x;
void maintain(int rt) {
  t[rt].sz = t[t[rt].s[0]].sz + t[t[rt].s[1]].sz + t[rt].cnt;
}
void rotate(int &rt, int d) {
  int k = t[rt].s[d ^ 1];
  t[rt].s[d ^ 1] = t[k].s[d];
  t[k].s[d] = rt;
  maintain(rt);
  maintain(k);
  rt = k;
}
void insert(int &rt, int v) {
  if (rt == 0) {
    rt = ++num;
    t[rt].val = v;
    t[rt].s[0] = t[rt].s[1] = 0;
    t[rt].rd = rand();
    t[rt].sz = t[rt].cnt = 1;
    return;
  }
  if (t[rt].val == v) {
    t[rt].sz++;
    t[rt].cnt++;
    return;
  }
  if (v < t[rt].val)
    insert(t[rt].s[0], v);
  else
    insert(t[rt].s[1], v);
  if (t[t[rt].s[0]].rd > t[rt].rd)
    rotate(rt, 1);
  if (t[t[rt].s[1]].rd > t[rt].rd)
    rotate(rt, 0);
  maintain(rt);
}
void remove(int &rt, int v) {
  if (t[rt].val == v) {
    if (t[rt].cnt > 1) {
      t[rt].sz--;
      t[rt].cnt--;
      return;
    }
    if (t[rt].s[0] * t[rt].s[1] == 0) {
      rt = t[rt].s[0] + t[rt].s[1];
      return;
    }
    if (t[t[rt].s[0]].rd > t[t[rt].s[1]].rd) {
      rotate(rt, 1);
      remove(t[rt].s[1], v);
      maintain(rt);
      return;
    } else {
      rotate(rt, 0);
      remove(t[rt].s[0], v);
      maintain(rt);
      return;
    }
  }
  if (v < t[rt].val)
    remove(t[rt].s[0], v);
  else
    remove(t[rt].s[1], v);
  maintain(rt);
}
int rankl(int rt, int v) {
  if (rt == 0)
    return 0;
  if (t[rt].val == v)
    return t[t[rt].s[0]].sz + t[rt].cnt;
  if (v < t[rt].val)
    return rankl(t[rt].s[0], v);
  else
    return rankl(t[rt].s[1], v) + t[t[rt].s[0]].sz + t[rt].cnt;
}
int main() {
  int i;
  srand(0);
  cin >> n >> q0;
  for (i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    c[i] = c[i - 1];
    if (b[a[i]] == 0)
      c[i]++;
    b[a[i]]++;
  }
  for (i = 1; i <= q0; i++) {
    scanf("%d%d", &q[i].l, &q[i].r);
    q[i].id = i;
  }
  sort(q + 1, q + q0 + 1, cmp);
  // for(i=1;i<=n;i++) cout<<c[i]<<endl;
  for (i = 1; i <= q0; i++) {
    while (now < q[i].r) {
      now++;
      if (last[a[now]] != 0)
        remove(root, last[a[now]]);
      last[a[now]] = now;
      insert(root, now);
    }
    ans[q[i].id] = c[q[i].r] - rankl(root, q[i].l - 1);
  }
  for (i = 1; i <= q0; i++)
    printf("%d\\n", ans[i]);
  system("pause");
}


Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;
int a[500010], b[500010], c[500010], n, q0, now, last[500010], ans[500010];
struct query {
  int l, r, id, ans;
} q[500010];
bool cmp(const query &x, const query &y) { return x.r < y.r; }
struct node {
  int s[2];
  int val, rd;
  int sz, cnt;
} t[100010];
int root, num;
int op, x;
void maintain(int rt) {
  t[rt].sz = t[t[rt].s[0]].sz + t[t[rt].s[1]].sz + t[rt].cnt;
}
void rotate(int &rt, int d) {
  int k = t[rt].s[d ^ 1];
  t[rt].s[d ^ 1] = t[k].s[d];
  t[k].s[d] = rt;
  maintain(rt);
  maintain(k);
  rt = k;
}
void insert(int &rt, int v) {
  if (rt == 0) {
    rt = ++num;
    t[rt].val = v;
    t[rt].s[0] = t[rt].s[1] = 0;
    t[rt].rd = rand();
    t[rt].sz = t[rt].cnt = 1;
    return;
  }
  if (t[rt].val == v) {
    t[rt].sz++;
    t[rt].cnt++;
    return;
  }
  if (v < t[rt].val)
    insert(t[rt].s[0], v);
  else
    insert(t[rt].s[1], v);
  if (t[t[rt].s[0]].rd > t[rt].rd)
    rotate(rt, 1);
  if (t[t[rt].s[1]].rd > t[rt].rd)
    rotate(rt, 0);
  maintain(rt);
}
void remove(int &rt, int v) {
  if (t[rt].val == v) {
    if (t[rt].cnt > 1) {
      t[rt].sz--;
      t[rt].cnt--;
      return;
    }
    if (t[rt].s[0] * t[rt].s[1] == 0) {
      rt = t[rt].s[0] + t[rt].s[1];
      return;
    }
    if (t[t[rt].s[0]].rd > t[t[rt].s[1]].rd) {
      rotate(rt, 1);
      remove(t[rt].s[1], v);
      maintain(rt);
      return;
    } else {
      rotate(rt, 0);
      remove(t[rt].s[0], v);
      maintain(rt);
      return;
    }
  }
  if (v < t[rt].val)
    remove(t[rt].s[0], v);
  else
    remove(t[rt].s[1], v);
  maintain(rt);
}
int rankl(int rt, int v) {
  if (rt == 0)
    return 0;
  if (t[rt].val == v)
    return t[t[rt].s[0]].sz + t[rt].cnt;
  if (v < t[rt].val)
    return rankl(t[rt].s[0], v);
  else
    return rankl(t[rt].s[1], v) + t[t[rt].s[0]].sz + t[rt].cnt;
}
int main() {
  int i;
  srand(0);
  cin >> n >> q0;
  for (i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    c[i] = c[i - 1];
    if (b[a[i]] == 0)
      c[i]++;
    b[a[i]]++;
  }
  for (i = 1; i <= q0; i++) {
    scanf("%d%d", &q[i].l, &q[i].r);
    q[i].id = i;
  }
  sort(q + 1, q + q0 + 1, cmp);
  // for(i=1;i<=n;i++) cout<<c[i]<<endl;
  for (i = 1; i <= q0; i++) {
    while (now < q[i].r) {
      now++;
      if (last[a[now]] != 0)
        remove(root, last[a[now]]);
      last[a[now]] = now;
      insert(root, now);
    }
    ans[q[i].id] = c[q[i].r] - rankl(root, q[i].l - 1);
  }
  for (i = 1; i <= q0; i++)
    printf("%d\\n", ans[i]);
  // system("pause");
}