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

#include <bits/stdc++.h>

using namespace std;

#define fastio                                                                 \\
  std::ios_base::sync_with_stdio(false);                                       \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define PAUSE_EXE assert(false)
#define MAX 500001
#define MAX2 500001
#define SIZE 500001
#define MOD 1000000007LL
#define REP(i, n) for (__typeof(n) i = 0; i < n; ++i)
#define CREP(i, n) for (__typeof(n) i = n - 1; i >= 0; --i)
#define MYREP(i, a, b) for (__typeof(a) i = a; i <= b; ++i)
#define MYCREP(i, a, b) for (__typeof(a) i = b; i >= a; --i)
#define sz(a) int((a).size())
#define pb push_back
#define mp make_pair
#define fi first
#define sec second
#define all(c) (c).begin(), (c).end()
#define allr(c) (c).rbegin(), (c).rend()
#define loop(c, i) for (typeof(c.begin()) i = c.begin(); i != c.end(); i++)
#define loopr(c, i) for (typeof(c.end()) i = c.end(); i != c.begin();)

int bucket_size;
vector<int> num(MAX), freq(SIZE);
vector<int> ans(MAX2);

struct query {
  int ind, l, r;
} q[MAX2];

bool cmp(query a, query b) {
  int ax = a.l / bucket_size;
  int bx = b.l / bucket_size;
  if (ax != bx)
    return ax < bx;
  return a.r < b.r;
}

int main() {
  int n, m;
  scanf("%d %d", &n, &m);
  MYREP(i, 1, n)
  scanf("%d", &num[i]);
  REP(i, m) {
    // inPos(q[i].l) , inPos(q[i].r);
    scanf("%d %d", &q[i].l, &q[i].r);
    q[i].ind = i;
  }
  bucket_size = ceil(sqrt(n));
  sort(q, q + m, cmp);
  int start, end;
  int sum;
  start = end = 0;
  sum = 0;
  REP(i, m) {
    while (start < q[i].l) {
      --freq[num[start]];
      if (freq[num[start]] == 0)
        sum--;
      ++start;
    }
    while (end > q[i].r) {
      --freq[num[end]];
      if (freq[num[end]] == 0)
        sum--;
      --end;
    }

    while (start > q[i].l) {
      --start;
      ++freq[num[start]];
      if (freq[num[start]] == 1)
        sum++;
    }
    while (end < q[i].r) {
      ++end;
      ++freq[num[end]];
      if (freq[num[end]] == 1)
        sum++;
    }

    ans[q[i].ind] = sum;
  }
  REP(i, m)
  printf("%d\\n", ans[i]);
  return 0;
}


Error: 127
/tmp/6f33c988-92b1-4cc8-9f8c-61246a614fff.out: error while loading shared libraries: libc.so.6: failed to map segment from shared object


Fixed submission:

#include <bits/stdc++.h>

using namespace std;

const int maxn = 5e5 + 1, maxk = 1e6 + 1;
int root[maxn], L[16 * maxn], R[16 * maxn], sum[16 * maxn];
int rt = 1, sz = 1;
int lpos[maxk];

int copy(int v, int &u) {
  L[sz] = L[v];
  R[sz] = R[v];
  sum[sz] = sum[v];
  return u = sz++;
}

void make_root() {
  copy(root[rt - 1], root[rt]);
  rt++;
}

void add(int pos, int x, int v = root[rt - 1], int l = 0, int r = maxn) {
  sum[v] += x;
  if (r - l == 1)
    return;
  int m = (l + r) / 2;
  if (pos < m)
    add(pos, x, copy(L[v], L[v]), l, m);
  else
    add(pos, x, copy(R[v], R[v]), m, r);
}

int get(int a, int b, int v, int l = 0, int r = maxn) {
  if (a <= l && r <= b)
    return sum[v];
  if (r <= a || b <= l)
    return 0;
  int m = (l + r) / 2;
  return get(a, b, L[v], l, m) + get(a, b, R[v], m, r);
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int n, q;
  cin >> n >> q;
  for (int i = 1; i <= n; i++) {
    int t;
    cin >> t;
    make_root();
    add(lpos[t], -1);
    lpos[t] = i;
    add(lpos[t], 1);
  }

  int l, r;
  while (q--) {
    cin >> l >> r;
    cout << get(l, r + 1, root[r]) << "\\n";
  }
  return 0;
}