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

#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdlib.h>
#include <string>
#include <vector>
using namespace std;
#define mem(a, b) memset(a, b, sizeof(a))
#define lson l, m, rt << 1
#define rson m + 1, r, rt << 1 | 1
typedef long long ll;
const int N = 5e5 + 10;
const int inf = 0x3f3f3f3f;
int a[N], n, m, ans[N], c[N];
map<int, int> mp;
int lowbit(int x) { return x & -x; }
void add(int i, int k) {
  while (i <= n) {
    c[i] += k;
    i += lowbit(i);
  }
}
int sum(int i) {
  int res = 0;
  while (i > 0) {
    res += c[i];
    i -= lowbit(i);
  }
  return res;
}
struct node {
  int l, r, id;
} query[N];
bool cmp(node x, node y) { return x.r < y.r; }
int main() {
  scanf("%d%d", &n, &m);
  mem(c, 0);
  mp.clear();
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  for (int i = 0; i < m; i++) {
    scanf("%d%d", &query[i].l, &query[i].r);
    query[i].id = i;
  }
  sort(query, query + m, cmp);

  int cur = 1; // 从下标为1的位置开始扫描
  for (int i = 0; i < m; i++) {
    for (int j = cur; j <= query[i].r; j++) {
      if (mp.find(a[j]) != mp.end()) {
        add(mp[a[j]], -1);
      }
      add(j, 1);
      mp[a[j]] = j;
    }
    cur = query[i].r + 1;
    ans[query[i].id] = sum(query[i].r) - sum(query[i].l - 1);
  }
  for (int i = 0; i < m; i++)
    printf("%d\\n", ans[i]);

  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <time.h>
#include <vector>
using namespace std;

const int MAXN = 500005;
const int M = 1000005;
int n, q, tot;
int a[MAXN];
int T[M], lson[M], rson[M], c[M];
int build(int l, int r) {
  int root = tot++;
  c[root] = 0;
  if (l != r) {
    int mid = (l + r) >> 1;
    lson[root] = build(l, mid);
    rson[root] = build(mid + 1, r);
  }
  return root;
}
int update(int root, int pos, int val) {
  int newroot = tot++, tmp = newroot;
  c[newroot] = c[root] + val;
  int l = 1, r = n;
  while (l < r) {
    int mid = (l + r) >> 1;
    if (pos <= mid) {
      lson[newroot] = tot++;
      rson[newroot] = rson[root];
      newroot = lson[newroot];
      root = lson[root];
      r = mid;
    } else {
      rson[newroot] = tot++;
      lson[newroot] = lson[root];
      newroot = rson[newroot];
      root = rson[root];
      l = mid + 1;
    }
    c[newroot] = c[root] + val;
  }
  return tmp;
}
int query(int root, int pos) {
  int ret = 0;
  int l = 1, r = n;
  while (pos < r) {
    int mid = (l + r) >> 1;
    if (pos <= mid) {
      r = mid;
      root = lson[root];
    } else {
      ret += c[lson[root]];
      root = rson[root];
      l = mid + 1;
    }
  }
  return ret + c[root];
}
int main() {
  scanf("%d%d", &n, &q);
  tot = 0;
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  T[n + 1] = build(1, n); // 建立一颗空树
  map<int, int> mp;
  for (int i = n; i >= 1; i--) {
    if (mp.find(a[i]) == mp.end()) {
      T[i] = update(T[i + 1], i, 1);
    } else {
      int tmp = update(T[i + 1], mp[a[i]], -1);
      T[i] = update(tmp, i, 1);
    }
    mp[a[i]] = i;
  }
  while (q--) {
    int l, r;
    scanf("%d%d", &l, &r);
    printf("%d\\n", query(T[l], r));
  }
}