Problem p02599 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const int N = 200010;
template <typename T> void inline read(T &x) {
  T f = 1;
  char ch = getchar();
  for (; '0' > ch || ch > '9'; ch = getchar())
    if (ch == '-')
      f = -1;
  for (x = 0; '0' <= ch && ch <= '9'; ch = getchar())
    x = x * 10 + ch - '0';
  x *= f;
}
template <typename T> void write(T x) {
  if (x > 9)
    write(x / 10);
  putchar(x % 10 + '0');
}
template <typename T> void print(T x) {
  if (x < 0)
    putchar('-'), x = -x;
  write(x);
  putchar('\n');
}
int n, m;
int col[N];
int len, bl[N];
int ans[N];
struct node {
  int l, r, id;
  friend bool operator<(node a, node b) {
    if (bl[a.l] != bl[b.l])
      return bl[a.l] < bl[b.l];
    if (bl[a.l] % 2)
      return a.r < b.r;
    return a.r > b.r;
  }
} q[N];
int nowl = 1, nowr = 0, nowans;
int cnt[1000010];
void add(int c) {
  if (!cnt[c])
    nowans++;
  cnt[c]++;
}
void del(int c) {
  cnt[c]--;
  if (!cnt[c])
    nowans--;
}
int main() {
  read(n);
  read(m);
  for (int i = 1; i <= n; i++)
    read(col[i]);
  len = pow(n, 1.0 / 2.0);
  for (int i = 1; i <= n; i++)
    bl[i] = (i - 1) / len + 1;
  for (int i = 1; i <= m; i++) {
    read(q[i].l);
    read(q[i].r);
    q[i].id = i;
  }
  sort(q + 1, q + 1 + m);
  for (int i = 1; i <= m; i++) {
    while (nowl > q[i].l)
      add(col[--nowl]);
    while (nowr < q[i].r)
      add(col[++nowr]);
    while (nowl < q[i].l)
      del(col[nowl++]);
    while (nowr > q[i].r)
      del(col[nowr--]);
    ans[q[i].id] = nowans;
  }
  for (int i = 1; i <= m; i++)
    print(ans[i]);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int N = 500010;
template <typename T> void inline read(T &x) {
  T f = 1;
  char ch = getchar();
  for (; '0' > ch || ch > '9'; ch = getchar())
    if (ch == '-')
      f = -1;
  for (x = 0; '0' <= ch && ch <= '9'; ch = getchar())
    x = x * 10 + ch - '0';
  x *= f;
}
template <typename T> void write(T x) {
  if (x > 9)
    write(x / 10);
  putchar(x % 10 + '0');
}
template <typename T> void print(T x) {
  if (x < 0)
    putchar('-'), x = -x;
  write(x);
  putchar('\n');
}
int n, m;
int col[N];
int len, bl[N];
int ans[N];
struct node {
  int l, r, id;
  friend bool operator<(node a, node b) {
    if (bl[a.l] != bl[b.l])
      return bl[a.l] < bl[b.l];
    if (bl[a.l] % 2)
      return a.r < b.r;
    return a.r > b.r;
  }
} q[N];
int nowl = 1, nowr = 0, nowans;
int cnt[1000010];
void add(int c) {
  if (!cnt[c])
    nowans++;
  cnt[c]++;
}
void del(int c) {
  cnt[c]--;
  if (!cnt[c])
    nowans--;
}
int main() {
  read(n);
  read(m);
  for (int i = 1; i <= n; i++)
    read(col[i]);
  len = pow(n, 1.0 / 2.0);
  for (int i = 1; i <= n; i++)
    bl[i] = (i - 1) / len + 1;
  for (int i = 1; i <= m; i++) {
    read(q[i].l);
    read(q[i].r);
    q[i].id = i;
  }
  sort(q + 1, q + 1 + m);
  for (int i = 1; i <= m; i++) {
    while (nowl > q[i].l)
      add(col[--nowl]);
    while (nowr < q[i].r)
      add(col[++nowr]);
    while (nowl < q[i].l)
      del(col[nowl++]);
    while (nowr > q[i].r)
      del(col[nowr--]);
    ans[q[i].id] = nowans;
  }
  for (int i = 1; i <= m; i++)
    print(ans[i]);
  return 0;
}