Problem p02599 (C++)

Buggy submission (status: Runtime Error):
// luogu-judger-enable-o2
#pragma GCC optimize(2)

#include <bits/stdc++.h>

using namespace std;
#define rg register
typedef long long ll;

template <typename T> bool read(T &n) {
  T ans = 0, flag = 1;
  char ch;
  while ((ch = getchar()) < '0' || ch > '9')
    if (ch == '-')
      flag = -1;
    else if (ch == EOF)
      return false;
  ans = ch - '0';
  while ((ch = getchar()) >= '0' && ch <= '9')
    ans = ans * 10 + ch - '0';
  n = ans * flag;
  return true;
}

template <typename T> T read() {
  T ans = 0, flag = 1;
  char ch;
  while ((ch = getchar()) < '0' || ch > '9')
    if (ch == '-')
      flag = -1;
    else if (ch == EOF)
      return false;
  ans = ch - '0';
  while ((ch = getchar()) >= '0' && ch <= '9')
    ans = ans * 10 + ch - '0';
  return ans * flag;
};
struct QUE {
  int l, r, id;
} que[50004];
int sz, belong[50004], n, m, pre[50004], cnt[50004];
ll fz[50004], fm[50004], ans;

bool cmp(QUE a, QUE b) {
  return belong[a.l] == belong[b.l] ? (belong[a.l] % 2 ? a.r < b.r : a.r > b.r)
                                    : belong[a.l] < belong[b.l];
}

int main() {
  read(n), read(m);
  sz = sqrt(n);
  for (int i = 1; i <= n; i++)
    read(pre[i]);
  for (int i = 1; i <= m; i++)
    read(que[i].l), read(que[i].r), que[i].id = i, belong[i] = (i - 1) / sz + 1;
  sort(que + 1, que + m + 1, cmp);
  int nl = que[1].l, nr = que[1].l;
  cnt[pre[nl]]++;
  ans = 1;
  for (int i = 1; i <= m; i++) {
    while (nl < que[i].l) {
      if (cnt[pre[nl]] == 1)
        ans--;
      cnt[pre[nl]]--, nl++;
    }
    while (nr > que[i].r) {
      if (cnt[pre[nr]] == 1)
        ans--;
      cnt[pre[nr]]--, nr--;
    }
    while (nl > que[i].l) {
      nl--;
      if (cnt[pre[nl]] == 0)
        ans++;
      cnt[pre[nl]]++;
    }
    while (nr < que[i].r) {
      nr++;
      if (cnt[pre[nr]] == 0)
        ans++;
      cnt[pre[nr]]++;
    }
    fz[que[i].id] = ans;
  }
  for (int i = 1; i <= m; i++) {
    printf("%lld\n", fz[i]);
  }
  return 0;
}

Fixed submission:
// luogu-judger-enable-o2
#pragma GCC optimize(2)

#include <bits/stdc++.h>

using namespace std;
#define rg register
typedef long long ll;

template <typename T> bool read(T &n) {
  T ans = 0, flag = 1;
  char ch;
  while ((ch = getchar()) < '0' || ch > '9')
    if (ch == '-')
      flag = -1;
    else if (ch == EOF)
      return false;
  ans = ch - '0';
  while ((ch = getchar()) >= '0' && ch <= '9')
    ans = ans * 10 + ch - '0';
  n = ans * flag;
  return true;
}

template <typename T> T read() {
  T ans = 0, flag = 1;
  char ch;
  while ((ch = getchar()) < '0' || ch > '9')
    if (ch == '-')
      flag = -1;
    else if (ch == EOF)
      return false;
  ans = ch - '0';
  while ((ch = getchar()) >= '0' && ch <= '9')
    ans = ans * 10 + ch - '0';
  return ans * flag;
};
struct QUE {
  int l, r, id;
} que[500004];
int sz, belong[500004], n, m, pre[500004], cnt[500004];
ll fz[500004], fm[500004], ans;

bool cmp(QUE a, QUE b) {
  return belong[a.l] == belong[b.l] ? (belong[a.l] % 2 ? a.r < b.r : a.r > b.r)
                                    : belong[a.l] < belong[b.l];
}

int main() {
  read(n), read(m);
  sz = sqrt(n);
  for (int i = 1; i <= n; i++)
    read(pre[i]);
  for (int i = 1; i <= m; i++)
    read(que[i].l), read(que[i].r), que[i].id = i, belong[i] = (i - 1) / sz + 1;
  sort(que + 1, que + m + 1, cmp);
  int nl = que[1].l, nr = que[1].l;
  cnt[pre[nl]]++;
  ans = 1;
  for (int i = 1; i <= m; i++) {
    while (nl < que[i].l) {
      if (cnt[pre[nl]] == 1)
        ans--;
      cnt[pre[nl]]--, nl++;
    }
    while (nr > que[i].r) {
      if (cnt[pre[nr]] == 1)
        ans--;
      cnt[pre[nr]]--, nr--;
    }
    while (nl > que[i].l) {
      nl--;
      if (cnt[pre[nl]] == 0)
        ans++;
      cnt[pre[nl]]++;
    }
    while (nr < que[i].r) {
      nr++;
      if (cnt[pre[nr]] == 0)
        ans++;
      cnt[pre[nr]]++;
    }
    fz[que[i].id] = ans;
  }
  for (int i = 1; i <= m; i++) {
    printf("%lld\n", fz[i]);
  }
  return 0;
}