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

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <vector>

using namespace std;

typedef long long ll;

typedef struct node {
  int s, e, val, pos;
  bool operator<(const node &n) const {
    if (e != n.e)
      return e < n.e;
    return val > n.val;
  }
  node(){};
  node(int ss, int ee, int v, int p) { s = ss, e = ee, val = v, pos = p; }
} node;

int occs[(int)1e6 + 5], tree[500005], n, ans[500005];

void update(int x, int val) {
  while (x <= n) {
    tree[x] += val;
    x += x & -x;
  }
}

int query(int x) {
  int ret = 0;
  while (x > 0) {
    ret += tree[x];
    x -= x & -x;
  }
  return ret;
}

int main() {
  int q, s, e;
  vector<node> v;
  scanf("%d", &n);
  scanf("%d", &q);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &s);
    v.push_back(node(i, i, s, -1));
  }

  for (int i = 0; i < q; i++) {
    scanf("%d %d", &s, &e);
    v.push_back(node(s, e, 0, i));
  }

  sort(v.begin(), v.end());

  for (int i = 0; i < v.size(); i++) {
    if (v[i].val == 0) {
      ans[v[i].pos] = query(v[i].e) - query(v[i].s - 1);
    } else {
      if (occs[v[i].val] != 0)
        update(occs[v[i].val], -1);
      update(v[i].s, 1);
      occs[v[i].val] = v[i].s;
    }
  }

  for (int i = 0; i < q; i++) {
    printf("%d\\n", ans[i]);
  }

  return 0;
}


Error: 127
/tmp/60881bf4-816b-4601-b5f2-e2e902be1980.out: error while loading shared libraries: libstdc++.so.6: failed to map segment from shared object


Fixed submission:

#include <bits/stdc++.h>
#define LL long long int
#define s(a) scanf("%d", &a)
#define sl(a) scanf("%lld", &a)
#define ss(a) scanf("%s", a)
#define w(t) while (t--)
#define f(i, n) for (i = 0; i < n; i++)
#define fd(i, n) for (i = n - 1; i >= 0; i--)
#define p(a) printf("%d", a)
#define pl(a) printf("%lld", a)
#define ps(a) printf("%s", a)
#define pc(a) printf("%c", a)
#define ent printf("\\n")
#define mod 1000000007
#define PI 3.14159265
#define gs getline(cin, s)
#define pb push_back
#define mp make_pair
#define INF 1e18

using namespace std;

unordered_map<int, int> pos;
int ar[600005];
int tree[16 * 500000 + 5];
int lft[16 * 500000 + 5];
int rht[16 * 500000 + 5];
int root[1000005];
int rt[500005];
int idx;

inline void build(int node, int start, int end) {
  if (start == end) {
    tree[node] = 0;
    return;
  }
  lft[node] = idx++;
  rht[node] = idx++;
  int mid = (start + end) / 2;
  build(lft[node], start, mid);
  build(rht[node], mid + 1, end);
  tree[node] = 0;
}

inline int update(int node, int start, int end, int pos, int val) {
  int x;
  x = idx++;
  if (start == end) {
    tree[x] = val;
    return x;
  }
  lft[x] = lft[node];
  rht[x] = rht[node];
  int mid = (start + end) / 2;
  if (pos <= mid)
    lft[x] = update(lft[x], start, mid, pos, val);
  else
    rht[x] = update(rht[x], mid + 1, end, pos, val);

  tree[x] = tree[lft[x]] + tree[rht[x]];

  return x;
}

inline int query(int node, int start, int end, int l, int r) {
  if (start > r || end < l)
    return 0;
  else if (start >= l && end <= r)
    return tree[node];
  int mid = (start + end) / 2;
  int q1 = query(lft[node], start, mid, l, r);
  int q2 = query(rht[node], mid + 1, end, l, r);

  return (q1 + q2);
}

int main() {
std:
  ios_base::sync_with_stdio(false);
  int n;
  s(n);
  int m;
  s(m);

  int i;

  idx = 0;
  root[0] = 0;
  build(0, 1, n);

  int t = 1;
  for (i = 1; i <= n; i++) {
    s(ar[i]);
    int k = pos[ar[i]];
    if (k != 0) {
      root[t] = update(root[t - 1], 1, n, k, 0);
      t++;
      root[t] = update(root[t - 1], 1, n, i, 1);
      t++;
    } else {
      root[t] = update(root[t - 1], 1, n, i, 1);
      t++;
    }

    rt[i] = t - 1;
    pos[ar[i]] = i;
  }

  while (m--) {
    int a, b;
    s(a);
    s(b);
    p(query(root[rt[b]], 1, n, a, b));
    ent;
  }

  return 0;
}