Problem p02599 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
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;
}
int buf[105];
inline void write(int i) {
  int p = 0;
  if (i == 0)
    p++;
  else
    while (i) {
      buf[p++] = i % 10;
      i /= 10;
    }
  for (int j = p - 1; j >= 0; j--)
    putchar('0' + buf[j]);
}
int a[500005], ans[500005], cnt[500005], res = 0, n, q, bl;
struct node {
  int l, r, id;
  int block;
} e[500005];

inline bool cmp(node a, node b) {
  if (a.block ^ b.block) {
    if (a.block & 1)
      return a.r < b.r;
    return a.r > b.r;
  }
  return a.l < b.l;
}

inline void del(int x) {
  --cnt[a[x]];
  if (!cnt[a[x]])
    --res;
}
inline void add(int x) {
  if (!cnt[a[x]])
    ++res;
  ++cnt[a[x]];
}

int main() {
  n = read();
  q = read();
  bl = n / sqrt(q * 2 / 3);
  for (int i = 1; i <= n; ++i)
    a[i] = read();
  for (int i = 1; i <= q; ++i) {
    e[i].l = read();
    e[i].r = read();
    e[i].id = i;
    e[i].block = e[i].l / bl;
  }
  sort(e + 1, e + 1 + q, cmp);
  int l = e[1].l, r = e[1].l - 1;
  for (int i = 1; i <= q; ++i) {
    int stdl = e[i].l, stdr = e[i].r;
    while (l < stdl)
      del(l++);
    while (l > stdl)
      add(--l);
    while (r < stdr)
      add(++r);
    while (r > stdr)
      del(r--);
    ans[e[i].id] = res;
  }
  for (int i = 1; i <= q; ++i)
    write(ans[i]), printf("\n");
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
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;
}
int buf[105];
inline void write(int i) {
  int p = 0;
  if (i == 0)
    p++;
  else
    while (i) {
      buf[p++] = i % 10;
      i /= 10;
    }
  for (int j = p - 1; j >= 0; j--)
    putchar('0' + buf[j]);
}
int a[500005], ans[500005], cnt[500005], res = 0, n, q, bl;
struct node {
  int l, r, id;
  int block;
} e[500005];

inline bool cmp(const node &a, const node &b) {
  if (a.block ^ b.block)
    return a.l < b.l;

  if (a.block & 1)
    return a.r < b.r;
  return a.r > b.r;
}

inline void del(int x) {
  --cnt[a[x]];
  if (!cnt[a[x]])
    --res;
}
inline void add(int x) {
  if (!cnt[a[x]])
    ++res;
  ++cnt[a[x]];
}

int main() {
  n = read();
  q = read();
  bl = n / sqrt(q * 2 / 3);
  for (int i = 1; i <= n; ++i)
    a[i] = read();
  for (int i = 1; i <= q; ++i) {
    e[i].l = read();
    e[i].r = read();
    e[i].id = i;
    e[i].block = e[i].l / bl;
  }
  sort(e + 1, e + 1 + q, cmp);
  int l = e[1].l, r = e[1].l - 1;
  for (int i = 1; i <= q; ++i) {
    int stdl = e[i].l, stdr = e[i].r;
    while (l < stdl)
      del(l++);
    while (l > stdl)
      add(--l);
    while (r < stdr)
      add(++r);
    while (r > stdr)
      del(r--);
    ans[e[i].id] = res;
  }
  for (int i = 1; i <= q; ++i)
    write(ans[i]), printf("\n");
  return 0;
}