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

#include <bits/stdc++.h>

using namespace std;

typedef long long tint;

#define forsn(i, s, n) for (int i = s; i < int(n); i++)
#define forn(i, n) forsn(i, 0, n)
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define NACHO                                                                  \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(NULL);
#define dbg cout << "hu" << endl;

const tint INF = 11000000;
const int MOD = 1000000007;

const int N = 5e5 + 5;
int t[4 * N];
int n;
map<int, int> id;

void build(int v, int tl, int tr) {
  if (tl == tr) {
    t[v] = 0;
  } else {
    int tm = (tl + tr) / 2;
    build(2 * v, tl, tm);
    build(2 * v + 1, tm + 1, tr);
    t[v] = t[2 * v] + t[2 * v + 1];
  }
}

int sum(int v, int tl, int tr, int l, int r) {
  if (l > r)
    return 0;
  if (l == tl && r == tr)
    return t[v];
  int tm = (tl + tr) / 2;
  return sum(2 * v, tl, tm, l, min(r, tm)) +
         sum(2 * v + 1, tm + 1, tr, max(l, tm + 1), r);
}

void update(int v, int tl, int tr, int pos, int val) {
  if (tl == tr)
    t[v] = val;
  else {
    int tm = (tl + tr) / 2;
    if (pos <= tm)
      update(2 * v, tl, tm, pos, val);
    else
      update(2 * v + 1, tm + 1, tr, pos, val);
    t[v] = t[2 * v] + t[2 * v + 1];
  }
}

struct Q {
  int l, r, id;
  bool operator<(const Q &ot) const { return r < ot.r; }
};

int main() {
  NACHO;
  int q;
  cin >> n >> q;
  vector<int> a(n);
  forn(i, n) {
    cin >> a[i];
    if (id[a[i]] == 0)
      id[a[i]] = int(id.size());
    a[i] = id[a[i]];
  }
  build(1, 0, n - 1);
  vector<Q> query(q);
  forn(i, q) {
    cin >> query[i].l >> query[i].r;
    query[i].l--;
    query[i].r--;
    query[i].id = i;
  }
  sort(all(query));
  vector<int> last(N, -1);
  int ret[q];
  int contQ = 0;
  forn(i, n) {
    if (last[a[i]] != -1) {
      update(1, 0, n - 1, last[a[i]], 0);
    }
    last[a[i]] = i;
    update(1, 0, n - 1, i, 1);
    while (contQ < q && query[contQ].r == i) {
      if (query[contQ].l == 0) {
        ret[query[contQ].id] = sum(1, 0, n - 1, 0, query[contQ].r);
      } else {
        ret[query[contQ].id] = sum(1, 0, n - 1, 0, query[contQ].r) -
                               sum(1, 0, n - 1, 0, query[contQ].l - 1);
      }
      contQ++;
    }
  }
  forn(i, q) { cout << ret[i] << "\\n"; }
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

typedef long long tint;

#define forsn(i, s, n) for (int i = s; i < int(n); i++)
#define forn(i, n) forsn(i, 0, n)
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define NACHO                                                                  \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(NULL);
#define dbg cout << "hu" << endl;

const tint INF = 11000000;
const int MOD = 1000000007;

const int N = 2e5 + 1;
int t[4 * N];
int n;
map<int, int> id;

void build(int v, int tl, int tr) {
  if (tl == tr) {
    t[v] = 0;
  } else {
    int tm = (tl + tr) / 2;
    build(2 * v, tl, tm);
    build(2 * v + 1, tm + 1, tr);
    t[v] = t[2 * v] + t[2 * v + 1];
  }
}

int sum(int v, int tl, int tr, int l, int r) {
  if (l > r)
    return 0;
  if (l == tl && r == tr)
    return t[v];
  int tm = (tl + tr) / 2;
  return sum(2 * v, tl, tm, l, min(r, tm)) +
         sum(2 * v + 1, tm + 1, tr, max(l, tm + 1), r);
}

void update(int v, int tl, int tr, int pos, int val) {
  if (tl == tr)
    t[v] = val;
  else {
    int tm = (tl + tr) / 2;
    if (pos <= tm)
      update(2 * v, tl, tm, pos, val);
    else
      update(2 * v + 1, tm + 1, tr, pos, val);
    t[v] = t[2 * v] + t[2 * v + 1];
  }
}

struct Q {
  int l, r, id;
  bool operator<(const Q &ot) const { return r < ot.r; }
};

int main() {
  NACHO;
  int q;
  cin >> n >> q;
  vector<int> a(n);
  forn(i, n) {
    cin >> a[i];
    if (id[a[i]] == 0)
      id[a[i]] = int(id.size());
    a[i] = id[a[i]];
  }
  build(1, 0, n - 1);
  vector<Q> query(q);
  forn(i, q) {
    cin >> query[i].l >> query[i].r;
    query[i].l--;
    query[i].r--;
    query[i].id = i;
  }
  sort(all(query));
  vector<int> last(N, -1);
  int ret[q];
  int contQ = 0;
  forn(i, n) {
    if (last[a[i]] != -1) {
      update(1, 0, n - 1, last[a[i]], 0);
    }
    last[a[i]] = i;
    update(1, 0, n - 1, i, 1);
    while (contQ < q && query[contQ].r == i) {
      if (query[contQ].l == 0) {
        ret[query[contQ].id] = sum(1, 0, n - 1, 0, query[contQ].r);
      } else {
        ret[query[contQ].id] = sum(1, 0, n - 1, 0, query[contQ].r) -
                               sum(1, 0, n - 1, 0, query[contQ].l - 1);
      }
      contQ++;
    }
  }
  forn(i, q) { cout << ret[i] << "\\n"; }
}