Problem p03311 (C++)

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

using namespace std;

struct random_class {
  struct xorshift {
    using result_type = unsigned int;
    result_type x = 123456789u, y = 362436069u, z = 521288629u, w;
    static constexpr result_type min() { return 0u; }
    static constexpr result_type max() { return UINT_MAX; }
    xorshift(result_type a) : w(a) {}
    result_type operator()() {
      result_type t;
      t = x ^ (x << 11);
      x = y;
      y = z;
      z = w;
      return w = (w ^ (w >> 19)) ^ (t ^ (t >> 8));
    }
  };
  random_device rd;
  xorshift xor128;
  random_class() : rd(), xor128(rd()) {}
  random_class(unsigned int seed) : rd(), xor128(seed) {}
  double make_random(double min_v, double max_v) {
    return uniform_real_distribution<double>(min_v, max_v)(xor128);
  }
};

random_class R(123);

template <class T> struct Treap {
  struct node {
    T val;
    double pri;
    node *lch, *rch;
    int cnt;
    node(T v) : val(v), lch(nullptr), rch(nullptr), cnt(1) {
      pri = R.make_random(DBL_MIN, DBL_MAX);
    }
  };
  node *top;
  Treap(node *t = nullptr) : top(t) {}
  node *update(node *n) {
    n->cnt = count(n->lch) + count(n->rch) + 1;
    return n;
  }
  inline int count(node *n) { return !n ? 0 : n->cnt; }
  node *merge(node *l, node *r) {
    if (!l || !r)
      return !l ? r : l;
    if (l->pri > r->pri) {
      l->rch = merge(l->rch, r);
      return update(l);
    } else {
      r->lch = merge(l, r->lch);
      return update(r);
    }
  }
  pair<node *, node *> split(node *t, int k) {
    if (!t)
      return make_pair(nullptr, nullptr);
    if (k <= count(t->lch)) {
      pair<node *, node *> s = split(t->lch, k);
      t->lch = s.second;
      return make_pair(s.first, update(t));
    } else {
      pair<node *, node *> s = split(t->rch, k - count(t->lch) - 1);
      t->rch = s.first;
      return make_pair(update(t), s.second);
    }
  }
  void insert(const T &&val) {
    int pos = 0;
    node *n = top;
    while (n) {
      if (val < n->val)
        n = n->lch;
      else
        pos += count(n->lch) + 1, n = n->rch;
    }
    auto t = split(top, pos);
    n = merge(t.first, new node(val));
    top = update(merge(n, t.second));
  }
  const T &at(int idx) {
    node *n = top;
    int sz;
    while ((sz = count(n->lch)) != idx) {
      if (sz + 1 <= idx)
        idx -= sz + 1, n = n->rch;
      else
        n = n->lch;
    }
    return n->val;
  }
  inline const int size() { return count(top); }
  // void dfs(node* n) {
  // 	if (!n) return;
  // 	cout << "node " << n->val << " ";
  // 	if (n->lch) cout << "left : " << n->lch->val << " ";
  // 	if (n->rch) cout << "right : " << n->rch->val << " ";
  // 	cout << endl;
  // 	dfs(n->lch);
  // 	dfs(n->rch);
  // }
  // void print() {
  // 	dfs(top);
  // }
};

int main() {
  Treap<int> T;
  int N;
  cin >> N;
  vector<int> v;
  for (int i = 0; i < N; i++) {
    int t;
    cin >> t;
    T.insert(t - i);
    v.push_back(t - i);
  }
  int b = T.at(N / 2 + 1);
  long long res = 0;
  for (auto x : v) {
    res += abs(x - b);
  }
  cout << res << endl;
}

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

using namespace std;

struct random_class {
  struct xorshift {
    using result_type = unsigned int;
    result_type x = 123456789u, y = 362436069u, z = 521288629u, w;
    static constexpr result_type min() { return 0u; }
    static constexpr result_type max() { return UINT_MAX; }
    xorshift(result_type a) : w(a) {}
    result_type operator()() {
      result_type t;
      t = x ^ (x << 11);
      x = y;
      y = z;
      z = w;
      return w = (w ^ (w >> 19)) ^ (t ^ (t >> 8));
    }
  };
  random_device rd;
  xorshift xor128;
  random_class() : rd(), xor128(rd()) {}
  random_class(unsigned int seed) : rd(), xor128(seed) {}
  double make_random(double min_v, double max_v) {
    return uniform_real_distribution<double>(min_v, max_v)(xor128);
  }
};

random_class R(123);

template <class T> struct Treap {
  struct node {
    T val;
    double pri;
    node *lch, *rch;
    int cnt;
    node(T v) : val(v), lch(nullptr), rch(nullptr), cnt(1) {
      pri = R.make_random(DBL_MIN, DBL_MAX);
    }
  };
  node *top;
  Treap(node *t = nullptr) : top(t) {}
  node *update(node *n) {
    n->cnt = count(n->lch) + count(n->rch) + 1;
    return n;
  }
  inline int count(node *n) { return !n ? 0 : n->cnt; }
  node *merge(node *l, node *r) {
    if (!l || !r)
      return !l ? r : l;
    if (l->pri > r->pri) {
      l->rch = merge(l->rch, r);
      return update(l);
    } else {
      r->lch = merge(l, r->lch);
      return update(r);
    }
  }
  pair<node *, node *> split(node *t, int k) {
    if (!t)
      return make_pair(nullptr, nullptr);
    if (k <= count(t->lch)) {
      pair<node *, node *> s = split(t->lch, k);
      t->lch = s.second;
      return make_pair(s.first, update(t));
    } else {
      pair<node *, node *> s = split(t->rch, k - count(t->lch) - 1);
      t->rch = s.first;
      return make_pair(update(t), s.second);
    }
  }
  void insert(const T &&val) {
    int pos = 0;
    node *n = top;
    while (n) {
      if (val < n->val)
        n = n->lch;
      else
        pos += count(n->lch) + 1, n = n->rch;
    }
    auto t = split(top, pos);
    n = merge(t.first, new node(val));
    top = update(merge(n, t.second));
  }
  const T &at(int idx) {
    node *n = top;
    int sz;
    while ((sz = count(n->lch)) != idx) {
      if (sz + 1 <= idx)
        idx -= sz + 1, n = n->rch;
      else
        n = n->lch;
    }
    return n->val;
  }
  inline const int size() { return count(top); }
  // void dfs(node* n) {
  // 	if (!n) return;
  // 	cout << "node " << n->val << " ";
  // 	if (n->lch) cout << "left : " << n->lch->val << " ";
  // 	if (n->rch) cout << "right : " << n->rch->val << " ";
  // 	cout << endl;
  // 	dfs(n->lch);
  // 	dfs(n->rch);
  // }
  // void print() {
  // 	dfs(top);
  // }
};

int main() {
  Treap<int> T;
  int N;
  cin >> N;
  vector<int> v;
  for (int i = 0; i < N; i++) {
    int t;
    cin >> t;
    T.insert(t - i);
    v.push_back(t - i);
  }
  int b = T.at(N / 2);
  long long res = 0;
  for (auto x : v) {
    res += abs(x - b);
  }
  cout << res << endl;
}