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

#define DIN freopen("input.txt", "r", stdin);
#define DOUT freopen("output.txt", "w", stdout);
#include <bits/stdc++.h>
#include <cstdio>
#define mem(a, b) memset(a, b, sizeof(a))
#define REP(i, a, b) for (int i = (a); i <= (int)(b); i++)
#define REP_(i, a, b) for (int i = (a); i >= (b); i--)
#define pb push_back
using namespace std;
typedef long long LL;
typedef vector<int> VI;
typedef pair<LL, LL> P;
int read() {
  int x = 0, flag = 1;
  char c = getchar();
  while ((c > '9' || c < '0') && c != '-')
    c = getchar();
  if (c == '-')
    flag = 0, c = getchar();
  while (c <= '9' && c >= '0') {
    x = (x << 3) + (x << 1) + c - '0';
    c = getchar();
  }
  return flag ? x : -x;
}

#define chl k << 1
#define chr k << 1 | 1

const int maxn = 5e5 + 5;
int a[maxn], book[2 * maxn], n, m, tree[maxn * 3], t, ans[maxn];
struct node {
  int l, r, bianhao;
};
node h[maxn];

bool cmp(node &a, node &b) { return a.r < b.r; }

void add(int k, int l, int r, int j, int v) {
  if (j < l || j > r)
    return;
  if (l == r && l == j) {
    tree[k] += v;
    return;
  }
  tree[k] += v;
  int mid = (l + r) >> 1;
  add(chl, l, mid, j, v);
  add(chr, mid + 1, r, j, v);
}

int sum(int k, int l, int r, int ll, int rr) {
  if (rr < l || ll > r)
    return 0;
  if (l >= ll && r <= rr)
    return tree[k];
  int mid = (l + r) >> 1;
  return sum(chl, l, mid, ll, rr) + sum(chr, mid + 1, r, ll, rr);
}

int main() {
  n = read();
  m = read();
  for (int i = 1; i <= n; i++)
    a[i] = read();
  for (int i = 0; i < m; i++)
    h[i].l = read(), h[i].r = read(), h[i].bianhao = i;
  sort(h, h + m, cmp);

  for (int i = 0; i < m; i++) {
    int l = h[i].l, r = h[i].r;
    for (int j = t + 1; j <= r; j++) {
      if (!book[a[j]])
        book[a[j]] = j, add(1, 1, n, j, 1);
      else {
        add(1, 1, n, book[a[j]], -1);
        book[a[j]] = j;
        add(1, 1, n, j, 1);
      }
    }
    t = r;
    ans[h[i].bianhao] = sum(1, 1, n, l, r);
  }
  for (int i = 0; i < m; i++)
    printf("%d\\n", ans[i]);

  return 0;
}


Error: TLE

Fixed submission:

#include <cstdio>
#include <cstdlib>
#include <iostream>

#define L(x) ch[x][0]
#define R(x) ch[x][1]
#define maxn 50005
#define maxm 1000001
#define For(a, b, c) for (a = b; a <= c; ++a)
#define Rep(a, b, c) for (a = b; a >= c; --a)
using namespace std;
int n, m, a[maxn], pro[1000005], ne[maxn];

int read() {
  char x;
  while ((x = getchar()) > '9' || x < '0')
    ;
  int u = x - '0';
  while ((x = getchar()) <= '9' && x >= '0')
    u = (u << 3) + (u << 1) + x - '0';
  return u;
}

inline void pre() {
  int i;
  Rep(i, n, 1) {
    ne[i] = pro[a[i]] ? pro[a[i]] : n + 1;
    pro[a[i]] = i;
  }
}

struct Chairman_Tree {
  int e, rt[maxn], ch[maxm][2], w[maxm];
  void cpy(int x, int y) {
    ch[x][0] = ch[y][0], ch[x][1] = ch[y][1], w[x] = w[y];
  }
  void insert(int &q, int x, int l = 1, int r = n + 1) {
    cpy(++e, q);
    ++w[q = e];
    if (l == r)
      return;
    int mid = (l + r) >> 1;
    if (x <= mid)
      insert(L(q), x, l, mid);
    else
      insert(R(q), x, mid + 1, r);
  }
  int query(int t1, int t2, int num, int l = 1, int r = n + 1) {
    if (l == r)
      return num > l ? w[t2] - w[t1] : 0;
    int mid = (l + r) >> 1, k = w[L(t2)] - w[L(t1)];
    if (num <= mid)
      return query(L(t1), L(t2), num, l, mid) + w[R(t2)] - w[R(t1)];
    else
      return query(R(t1), R(t2), num, mid + 1, r);
  }
} d;

void build() {
  int i;
  pre();
  For(i, 1, n) d.rt[i] = d.rt[i - 1], d.insert(d.rt[i], ne[i]);
}

int main() {
#ifndef ONLINE_JUDGE
  freopen("L.in", "r", stdin);
  freopen("L.out", "w", stdout);
#endif
  int i;
  n = read();
  For(i, 1, n) a[i] = read();
  build();
  m = read();
  For(i, 1, m) {
    int l = read(), r = read();
    printf("%d\\n", d.query(d.rt[l - 1], d.rt[r], r));
  }
  return 0;
}