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

#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 = 5e5 + 10;
const int M = MAXN * 100;
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() {
  // freopen("in.txt","r",stdin);
  // freopen("out.txt","w",stdout);
  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));
  }

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define LL long long

const int N = 3e4 + 11;
const int M = 200000 + 11;
const int MAX = 1e6 + 11;

const int mod = 1e9 + 7;
const int inf = 0x3f3f3f3f;
const int inff = 0x3f3f3f3f3f3f3f3f;

int B;
struct Query {
  int le, ri;
  int id;
  bool operator<(const Query &b) const {
    if (le / B == b.le / B)
      return ri < b.ri;
    else
      return le / B < b.le / B;
  }
} Q[M];
int ANS[M];

int cnt[MAX];
int a[N];
int ans;
void Add(int x) {
  x = a[x];
  if (cnt[x] == 0)
    ans++;
  cnt[x]++;
}
void Del(int x) {
  x = a[x];
  cnt[x]--;
  if (cnt[x] == 0)
    ans--;
}

void solve(int q) {
  sort(Q + 1, Q + q + 1);
  memset(cnt, 0, sizeof(cnt));
  int L = 1, R = 0;
  ans = 0;
  for (int i = 1; i <= q; i++) {
    while (R < Q[i].ri)
      Add(++R);
    while (R > Q[i].ri)
      Del(R--);
    while (L < Q[i].le)
      Del(L++);
    while (L > Q[i].le)
      Add(--L);

    ANS[Q[i].id] = ans;
  }
  for (int i = 1; i <= q; i++)
    printf("%d\\n", ANS[i]);
}

int main() {
  int n, q;
  scanf("%d%d", &n, &q);
  B = (int)sqrt(n * 1.0);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  // int q;scanf("%d",&q);
  for (int i = 1; i <= q; i++) {
    scanf("%d%d", &Q[i].le, &Q[i].ri);
    Q[i].id = i;
  }
  solve(q);
  return 0;
}