Problem p02599 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

/// ----------------------------- (Debug) ------------------------------
#define sim template <class c
#define ris return *this
#define dor > debug &operator<<
#define eni(x)                                                                 \
  sim > typename enable_if<sizeof dud<c>(0) x 1, debug &>::type operator<<(    \
            c i) {
sim > struct rge {
  c b, e;
};
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c *x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifndef ONLINE_JUDGE
  eni(!=) cerr << boolalpha << i;
  ris;
} eni(==) ris << range(begin(i), end(i));
}
sim, class b dor(pair<b, c> d) {
  ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
  *this << "[";
  for (auto it = d.b; it != d.e; ++it)
    *this << ", " + 2 * (it == d.b) << *it;
  ris << "]";
}
#else
  sim dor(const c &) { ris; }
#endif
}
;
vector<char *> tokenizer(const char *args) {
  char *token = new char[111];
  strcpy(token, args);
  token = strtok(token, ", ");
  vector<char *> v({token});
  while (token = strtok(NULL, ", "))
    v.push_back(token);
  return reverse(v.begin(), v.end()), v;
}
void debugg(vector<char *> args) { cerr << "\b\b "; }
template <typename Head, typename... Tail>
void debugg(vector<char *> args, Head H, Tail... T) {
  debug() << " [" << args.back() << ": " << H << "] ";
  args.pop_back();
  debugg(args, T...);
}
#define harg(...) #__VA_ARGS__
#ifndef ONLINE_JUDGE
#define dbg(...)                                                               \
  {                                                                            \
    debugg(tokenizer(harg(__VA_ARGS__, \b\t-- > Line)), __VA_ARGS__,           \
           __LINE__);                                                          \
    cerr << endl;                                                              \
  }
#else
#define dbg(...)                                                               \
  {}
#endif
/// -----------------------------------------------------------------------

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pi;
typedef vector<pi> vpi;
#define IOS                                                                    \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define F first
#define S second
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define print(x) cout << x << "\n";
#define REP(i, a, b) for (i = a; i <= b; i++)
#define RAP(i, a, b) for (i = a; i >= b; i--)
#define spa << " " <<
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)x.size()
ll mod = 1e9 + 7;
const int MX = 0x3f3f3f3f;

int seg[int(1e6) + 1];
int c[int(5e5) + 1], last[int(5e5) + 1], ans[int(5e5) + 5];

void update(int v, int l, int r, int idx, int num) {
  if (r < idx || l > idx)
    return;
  else if (l == r)
    seg[v] += num;
  else {
    update(2 * v, l, (l + r) >> 1, idx, num);
    update(2 * v + 1, ((l + r) >> 1) + 1, r, idx, num);
    seg[v] = seg[2 * v] + seg[2 * v + 1];
  }
}

int query(int v, int i, int j, int l, int r) {
  if (j < l || i > r)
    return 0;
  else if (l >= i && r <= j)
    return seg[v];
  else {
    return query(2 * v, i, j, l, (l + r) >> 1) +
           query(2 * v + 1, i, j, (l + r >> 1) + 1, r);
  }
}

int main() {
  memset(last, -1, sizeof(last));
  int n, q, i;
  cin >> n >> q;
  REP(i, 0, n - 1) cin >> c[i];
  vector<pair<pair<int, int>, int>> que(q);
  REP(i, 0, q - 1) {
    cin >> que[i].F.S >> que[i].F.F;
    que[i].S = i;
  }
  sort(all(que));
  int j = 0;
  dbg(que);
  REP(i, 0, n - 1) {
    if (last[c[i]] != -1) {
      update(1, 0, n - 1, last[c[i]], -1);
    }
    last[c[i]] = i;
    update(1, 0, n - 1, i, 1);
    dbg(seg[1]);
    while (j < q && que[j].F.F == i + 1) {
      dbg(que[j].S);
      ans[que[j].S] = query(1, que[j].F.S - 1, que[j].F.F - 1, 0, n - 1);
      j++;
    }
  }
  dbg(ans);
  REP(i, 0, q - 1) cout << ans[i] << "\n";
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;

/// ----------------------------- (Debug) ------------------------------
#define sim template <class c
#define ris return *this
#define dor > debug &operator<<
#define eni(x)                                                                 \
  sim > typename enable_if<sizeof dud<c>(0) x 1, debug &>::type operator<<(    \
            c i) {
sim > struct rge {
  c b, e;
};
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c *x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifndef ONLINE_JUDGE
  eni(!=) cerr << boolalpha << i;
  ris;
} eni(==) ris << range(begin(i), end(i));
}
sim, class b dor(pair<b, c> d) {
  ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
  *this << "[";
  for (auto it = d.b; it != d.e; ++it)
    *this << ", " + 2 * (it == d.b) << *it;
  ris << "]";
}
#else
  sim dor(const c &) { ris; }
#endif
}
;
vector<char *> tokenizer(const char *args) {
  char *token = new char[111];
  strcpy(token, args);
  token = strtok(token, ", ");
  vector<char *> v({token});
  while (token = strtok(NULL, ", "))
    v.push_back(token);
  return reverse(v.begin(), v.end()), v;
}
void debugg(vector<char *> args) { cerr << "\b\b "; }
template <typename Head, typename... Tail>
void debugg(vector<char *> args, Head H, Tail... T) {
  debug() << " [" << args.back() << ": " << H << "] ";
  args.pop_back();
  debugg(args, T...);
}
#define harg(...) #__VA_ARGS__
#ifndef ONLINE_JUDGE
#define dbg(...)                                                               \
  {                                                                            \
    debugg(tokenizer(harg(__VA_ARGS__, \b\t-- > Line)), __VA_ARGS__,           \
           __LINE__);                                                          \
    cerr << endl;                                                              \
  }
#else
#define dbg(...)                                                               \
  {}
#endif
/// -----------------------------------------------------------------------

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pi;
typedef vector<pi> vpi;
#define IOS                                                                    \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define F first
#define S second
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define print(x) cout << x << "\n";
#define REP(i, a, b) for (i = a; i <= b; i++)
#define RAP(i, a, b) for (i = a; i >= b; i--)
#define spa << " " <<
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)x.size()
ll mod = 1e9 + 7;
const int MX = 0x3f3f3f3f;

int seg[int(2e6) + 1];
int c[int(5e5) + 1], last[int(5e5) + 5], ans[int(5e5) + 5];

void update(int v, int l, int r, int idx, int num) {
  if (r < idx || l > idx)
    return;
  else if (l == r)
    seg[v] += num;
  else {
    update(2 * v, l, (l + r) >> 1, idx, num);
    update(2 * v + 1, ((l + r) >> 1) + 1, r, idx, num);
    seg[v] = seg[2 * v] + seg[2 * v + 1];
  }
}

int query(int v, int i, int j, int l, int r) {
  if (j < l || i > r)
    return 0;
  else if (l >= i && r <= j)
    return seg[v];
  else {
    return query(2 * v, i, j, l, (l + r) >> 1) +
           query(2 * v + 1, i, j, (l + r >> 1) + 1, r);
  }
}

int main() {
  memset(last, -1, sizeof(last));
  int n, q, i;
  cin >> n >> q;
  REP(i, 0, n - 1) cin >> c[i];
  vector<pair<pair<int, int>, int>> que(q);
  REP(i, 0, q - 1) {
    cin >> que[i].F.S >> que[i].F.F;
    que[i].S = i;
  }
  sort(all(que));
  int j = 0;
  dbg(que);
  REP(i, 0, n - 1) {
    if (last[c[i]] != -1) {
      update(1, 0, n - 1, last[c[i]], -1);
    }
    last[c[i]] = i;
    update(1, 0, n - 1, i, 1);
    dbg(seg[1]);
    while (j < q && que[j].F.F == i + 1) {
      dbg(que[j].S);
      ans[que[j].S] = query(1, que[j].F.S - 1, que[j].F.F - 1, 0, n - 1);
      j++;
    }
  }
  dbg(ans);
  REP(i, 0, q - 1) cout << ans[i] << "\n";
  return 0;
}