Problem p02599 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <map>
#include <stdio.h>
using namespace std;
const int maxn = 300005;
struct node {
  int l, r, sum;
} tree[maxn * 4];
struct ques {
  int l, r, index;
} ask[maxn];
int a[maxn];
int ans[maxn];
void build(int i, int left, int right) {
  tree[i].l = left, tree[i].r = right;
  tree[i].sum = 0;
  if (left == right)
    return;
  int mid = left + right >> 1;
  build(i << 1, left, mid);
  build(i << 1 | 1, mid + 1, right);
}
map<int, int> has;
void update(int i, int pos, int w) {
  if (tree[i].l == pos && tree[i].r == pos) {
    tree[i].sum += w;
    return;
  }
  int mid = (tree[i].l + tree[i].r) >> 1;
  if (pos <= mid)
    update(i << 1, pos, w);
  else
    update(i << 1 | 1, pos, w);
  tree[i].sum = tree[i << 1].sum + tree[i << 1 | 1].sum;
}
int query(int i, int left, int right) {
  if (tree[i].l == left && tree[i].r == right) {
    return tree[i].sum;
  }
  int mid = (tree[i].l + tree[i].r) >> 1;
  if (right <= mid)
    return query(i << 1, left, right);
  else if (left > mid)
    return query(i << 1 | 1, left, right);
  else
    return query(i << 1, left, mid) + query(i << 1 | 1, mid + 1, right);
}
bool cmp(ques a, ques b) { return a.r < b.r; }
int main() {
  int n;
  scanf("%d", &n);
  int m;
  scanf("%d", &m);
  build(1, 1, n);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);

  for (int i = 1; i <= m; i++) {
    ask[i].index = i;
    scanf("%d%d", &ask[i].l, &ask[i].r);
  }
  sort(ask + 1, ask + 1 + m, cmp);
  int cur = 1;
  for (int i = 1; i <= m; i++) {
    for (int j = cur; j <= ask[i].r; j++) {
      if (has.find(a[j]) != has.end()) {
        int p = has[a[j]];
        update(1, p, -1);
      }
      update(1, j, 1);
      has[a[j]] = j;
    }
    cur = ask[i].r + 1;
    ans[ask[i].index] = query(1, ask[i].l, ask[i].r);
  }
  for (int i = 1; i <= m; i++)
    printf("%d\n", ans[i]);
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <map>
#include <stdio.h>
using namespace std;
const int maxn = 500005;
struct node {
  int l, r, sum;
} tree[maxn * 4];
struct ques {
  int l, r, index;
} ask[maxn];
int a[maxn];
int ans[maxn];
void build(int i, int left, int right) {
  tree[i].l = left, tree[i].r = right;
  tree[i].sum = 0;
  if (left == right)
    return;
  int mid = left + right >> 1;
  build(i << 1, left, mid);
  build(i << 1 | 1, mid + 1, right);
}
map<int, int> has;
void update(int i, int pos, int w) {
  if (tree[i].l == pos && tree[i].r == pos) {
    tree[i].sum += w;
    return;
  }
  int mid = (tree[i].l + tree[i].r) >> 1;
  if (pos <= mid)
    update(i << 1, pos, w);
  else
    update(i << 1 | 1, pos, w);
  tree[i].sum = tree[i << 1].sum + tree[i << 1 | 1].sum;
}
int query(int i, int left, int right) {
  if (tree[i].l == left && tree[i].r == right) {
    return tree[i].sum;
  }
  int mid = (tree[i].l + tree[i].r) >> 1;
  if (right <= mid)
    return query(i << 1, left, right);
  else if (left > mid)
    return query(i << 1 | 1, left, right);
  else
    return query(i << 1, left, mid) + query(i << 1 | 1, mid + 1, right);
}
bool cmp(ques a, ques b) { return a.r < b.r; }
int main() {
  int n;
  scanf("%d", &n);
  int m;
  scanf("%d", &m);
  build(1, 1, n);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);

  for (int i = 1; i <= m; i++) {
    ask[i].index = i;
    scanf("%d%d", &ask[i].l, &ask[i].r);
  }
  sort(ask + 1, ask + 1 + m, cmp);
  int cur = 1;
  for (int i = 1; i <= m; i++) {
    for (int j = cur; j <= ask[i].r; j++) {
      if (has.find(a[j]) != has.end()) {
        int p = has[a[j]];
        update(1, p, -1);
      }
      update(1, j, 1);
      has[a[j]] = j;
    }
    cur = ask[i].r + 1;
    ans[ask[i].index] = query(1, ask[i].l, ask[i].r);
  }
  for (int i = 1; i <= m; i++)
    printf("%d\n", ans[i]);
}