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');
}
int N = 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 *fwt, int i, int v) {
  while (i < N) {
    fwt[i] += v;
    i |= i + 1;
  }
}
static inline int Sum(int *fwt, int i) {
  int v = 0;
  while (i >= 0) {
    v += fwt[i];
    i = (i & (i + 1)) - 1;
  }
  return v;
}
static inline int Diff(int *fwt, int x, int y, int sz) {
  if (y == -1) {
    y = sz - 1;
  }
  return Sum(fwt, y) - Sum(fwt, x - 1);
}
int main(void) {
  N = IN();
  int Q = IN(), *fenwick, k = 0, dat[500000], result[500000], temp[500000];
  rep(i, N) { dat[i] = IN() - 1; }
  ask = (tuple *)malloc((Q) * sizeof(tuple));
  if (ask == NULL) {
    exit(EXIT_FAILURE);
  }
  fenwick = (int *)malloc((N) * sizeof(int));
  if (fenwick == NULL) {
    exit(EXIT_FAILURE);
  }
  rep(i, Q) { ask[i].first = IN() - 1, ask[i].second = IN(), ask[i].id = i; }
  qsort(&ask[0], Q, sizeof(tuple), Asc);
  memset(temp, -1, sizeof(temp));
  rep(i, Q) {
    while (k < ask[i].second) {
      int j = dat[k];
      if (temp[j] != -1) {
        Add(fenwick, temp[j], -1);
      }
      temp[j] = k, Add(fenwick, temp[j], 1);
      k++;
    }
    result[ask[i].id] = Diff(fenwick, ask[i].first, ask[i].second - 1, N);
  }
  rep(i, Q) { OUT(result[i]); }
  free(fenwick);
  return free(ask), 0;
}

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}, N = 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 *fwt, int i, int v) {
  while (i <= *fwt) {
    fwt[i] += v;
    i += (i & -i);
  }
}
static inline int Sum(int i) {
  int v = 0;
  while (i > 0) {
    v += fenwick[i];
    i -= (i & -i);
  }
  return v;
}
static inline int Diff(int x, int y) { return Sum(y) - Sum(x - 1); }
int main() {
  N = IN();
  int Q = IN(), r = 0, dat[500001], result[500001], temp[500001];
  rep(i, N) { dat[i + 1] = IN(); }
  ask = (tuple *)malloc((Q + 1) * sizeof(tuple));
  if (ask == NULL) {
    exit(EXIT_FAILURE);
  }
  rep(i, Q) {
    ask[i + 1].first = IN(), ask[i + 1].second = IN(), ask[i + 1].id = i + 1;
  }
  qsort(&ask[1], Q, sizeof(tuple), Asc);
  fenwick[0] = N;
  rep(i, Q) {
    while (r < ask[i + 1].second) {
      r++;
      if (!temp[dat[r]]) {
        temp[dat[r]] = r, Add(fenwick, r, 1);
      } else
        Add(fenwick, temp[dat[r]], -1), temp[dat[r]] = r, Add(fenwick, r, 1);
    }
    result[ask[i + 1].id] = Diff(ask[i + 1].first, ask[i + 1].second);
  }
  rep(i, Q) { OUT(result[i + 1]); }
  return free(ask), 0;
}