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

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define rep(i, N) for (int i = 0; i < (int)N; i++)
#pragma GCC optimize("O3")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
static inline char GET(void) {
  static char buf[1 << 15], *ptr = buf, *end = buf;
  if (ptr == end) {
    ptr = buf, end = buf + fread(buf, 1, 1 << 15, stdin);
  }
  return *ptr++;
}
static inline void PUT(char c) {
  static char buf[1 << 15], *ptr = buf;
  if (ptr == buf + strlen(buf) || c == 0) {
    fwrite(buf, 1, ptr - buf, stdout), ptr = buf;
  }
  *ptr++ = c;
}
static inline int IN(void) {
  int x = 0, f = 0, c = getchar();
  while (c < 48 || c > 57) {
    f ^= c == 45, c = getchar();
  }
  while (c > 47 && c < 58) {
    x = x * 10 + c - 48, c = getchar();
  }
  return f ? -x : x;
}
static inline void OUT(int a) {
  if (a < 0)
    PUT('-'), a = -a;
  int d[40], i = 0;
  do {
    d[i++] = a % 10;
  } while (a /= 10);
  while (i--) {
    PUT(d[i] + 48);
  }
  PUT('\\n');
}
static inline void Add(int *fwt, int idx, int v) {
  while (idx <= *fwt) {
    fwt[idx] += v;
    idx += (idx & -idx);
  }
}
static inline int Sum(int *fwt, int x) {
  int v = 0;
  while (x) {
    v += fwt[x];
    x -= (x & -x);
  }
  return v;
}
int main() {
  int N = IN(), Q = IN(), after[500001], dat[500001], fenwick[500001] = {0},
      id[500001], left[500001], pos[500001] = {0}, result[500001];
  rep(i, N) { dat[i + 1] = IN(); }
  rep(i, Q) {
    left[i + 1] = IN();
    int r = IN();
    after[i + 1] = id[r];
    id[r] = i + 1;
  }
  fenwick[0] = N;
  for (int r = 1; r <= N; ++r) {
    if (pos[dat[r]]) {
      Add(fenwick, pos[dat[r]], -1);
    }
    Add(fenwick, r, 1);
    for (int i = id[r]; i; i = after[i]) {
      result[i] = Sum(fenwick, r) - Sum(fenwick, left[i] - 1);
    }
    pos[dat[r]] = r;
  }
  rep(i, Q) { OUT(result[i + 1]); }
}

Error: WA

Fixed submission:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define rep(i, N) for (int i = 0; i < (int)N; i++)
#pragma GCC optimize("O3")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
static inline char GET(void) {
  static char buf[1 << 15], *ptr = buf, *end = buf;
  if (ptr == end) {
    ptr = buf, end = buf + fread(buf, 1, 1 << 15, stdin);
  }
  return *ptr++;
}
static inline void PUT(char c) {
  static char buf[1 << 15], *ptr = buf;
  if (ptr == buf + strlen(buf) || c == 0) {
    fwrite(buf, 1, ptr - buf, stdout), ptr = buf;
  }
  *ptr++ = c;
}
static inline int IN(void) {
  int x = 0, f = 0, c = getchar();
  while (c < 48 || c > 57) {
    f ^= c == 45, c = getchar();
  }
  while (c > 47 && c < 58) {
    x = x * 10 + c - 48, c = getchar();
  }
  return f ? -x : x;
}
static inline void OUT(int a) {
  if (a < 0)
    PUT('-'), a = -a;
  int d[40], i = 0;
  do {
    d[i++] = a % 10;
  } while (a /= 10);
  while (i--) {
    PUT(d[i] + 48);
  }
  PUT('\\n');
}
int fenwick[500001] = {0}, len = 0;
typedef struct TUPLE tuple;
struct TUPLE {
  int first, second, id;
} *ask;
static inline int Asc(const void *a, const void *b) {
  if ((*(const tuple **)a)->second == (*(const tuple **)b)->second)
    return (*(const tuple **)a)->first - (*(const tuple **)b)->first;
  else
    return (*(const tuple **)a)->second - (*(const tuple **)b)->second;
}
static inline void Add(int x, int v) {
  while (x <= len) {
    fenwick[x] += v;
    x += (x & -x);
  }
}
static inline int Sum(int x) {
  int v = 0;
  while (x > 0) {
    v += fenwick[x];
    x -= (x & -x);
  }
  return v;
}
static inline int Diff(int x, int y) { return Sum(y) - (x ? Sum(x - 1) : 0); }
int main() {
  int j = 1, N = IN(), Q = IN(), dat[500001], result[500001], temp[500001];
  len = N;
  rep(i, N) { dat[i + 1] = IN(); }
  ask = (tuple *)malloc((Q + 1) * sizeof(tuple));
  if (ask == NULL) {
    exit(EXIT_FAILURE);
  }
  tuple **ref = (tuple **)malloc((Q + 1) * sizeof(tuple *));
  if (ref == NULL) {
    exit(EXIT_FAILURE);
  }
  rep(i, Q) {
    ask[i].first = IN(), ask[i].second = IN(), ask[i].id = i;
    ref[i] = (tuple *)malloc(1 * sizeof(tuple));
    if (ref[i] == NULL) {
      exit(EXIT_FAILURE);
    }
    ref[i] = &ask[i];
  }
  qsort(ref, Q, sizeof(tuple *), Asc);
  rep(i, Q) {
    while (j <= ref[i]->second) {
      if (temp[dat[j]]) {
        Add(temp[dat[j]], -1);
      }
      Add(j, 1), temp[dat[j]] = j, j++;
    }
    result[ref[i]->id] = Diff(ref[i]->first, ref[i]->second);
  }
  rep(i, Q) { OUT(result[i]); }
  free(ref);
  return free(ask), 0;
}