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

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

typedef int _loop_int;
#define REP(i, n) for (_loop_int i = 0; i < (_loop_int)(n); ++i)
#define FOR(i, a, b) for (_loop_int i = (_loop_int)(a); i < (_loop_int)(b); ++i)
#define FORR(i, a, b)                                                          \\
  for (_loop_int i = (_loop_int)(b)-1; i >= (_loop_int)(a); --i)

#define DEBUG(x) cout << #x << ": " << x << endl
#define DEBUG_VEC(v)                                                           \\
  cout << #v << ":";                                                           \\
  REP(i, v.size()) cout << " " << v[i];                                        \\
  cout << endl
#define ALL(a) (a).begin(), (a).end()

#define CHMIN(a, b) a = min((a), (b))
#define CHMAX(a, b) a = max((a), (b))

// mod
const ll MOD = 1000000007ll;
#define FIX(a) ((a) % MOD + MOD) % MOD

// floating
typedef double Real;
const Real EPS = 1e-11;
#define EQ0(x) (abs(x) < EPS)
#define EQ(a, b) (abs(a - b) < EPS)
typedef complex<Real> P;

typedef struct Fenwick {
  int n;
  int *bit;
} Fenwick;
Fenwick *new_Fenwick(int n) {
  Fenwick *ret = (Fenwick *)malloc(sizeof(Fenwick));
  ret->n = n;
  ret->bit = (int *)malloc(sizeof(int) * (n + 1));
  memset(ret->bit, 0, sizeof(int) * (n + 1));
  return ret;
}
void free_Fenwick(Fenwick *f) {
  free(f->bit);
  free(f);
}
void add_FW(Fenwick *f, int a, int w) {
  int x;
  for (x = a; x <= f->n; x += x & -x)
    f->bit[x] += w;
}
int sum_FW(Fenwick *f, int a) {
  int ret = 0;
  int x;
  for (x = a; x > 0; x -= x & -x)
    ret += f->bit[x];
  return ret;
}
int lower_bound_FW(Fenwick *f, int w) {
  int x = 0;
  int k = 1;
  if (w <= 0)
    return 0;
  while ((k << 1) <= f->n)
    k <<= 1;
  for (; k > 0; k >>= 1) {
    if (x + k <= f->n && f->bit[x + k] < w) {
      w -= f->bit[x + k];
      x += k;
    }
  }
  return x + 1;
}

int cnt[26];
int tmp[26];
int po[525252];
vi ord[26];
int tail[26];

int main() {
  string s;
  cin >> s;
  int n = s.size();
  // ?????§??????
  REP(i, n) cnt[s[i] - 'a']++;
  REP(i, 26) if (cnt[i] % 2 == 1) {
    puts("-1");
    return 0;
  }
  // ?????????
  ll yose = 0;
  int c1 = 0;
  string t;
  REP(i, n) {
    int c = s[i] - 'a';
    tmp[c]++;
    if (tmp[c] > cnt[c] / 2) {
      po[i] = 1;
      c1 += 1;
    } else {
      po[i] = 0;
      t += s[i];
      yose += c1;
    }
  }
  REP(i, n) if (po[i]) t += s[i];
  n = n / 2;
  string a = t.substr(0, n);
  string b = t.substr(n);
  reverse(ALL(b));
  REP(i, n) {
    int c = a[i] - 'a';
    ord[c].push_back(i + 1);
  }
  Fenwick *f = new_Fenwick(n + 30);
  ll ans = 0;
  REP(i, n) {
    int c = b[i] - 'a';
    int x = ord[c][tail[c]++];
    ans += sum_FW(f, n + 30) - sum_FW(f, x);
    add_FW(f, x, 1);
  }
  printf("%lld\\n", ans + yose);
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

typedef int _loop_int;
#define REP(i, n) for (_loop_int i = 0; i < (_loop_int)(n); ++i)
#define FOR(i, a, b) for (_loop_int i = (_loop_int)(a); i < (_loop_int)(b); ++i)
#define FORR(i, a, b)                                                          \\
  for (_loop_int i = (_loop_int)(b)-1; i >= (_loop_int)(a); --i)

#define DEBUG(x) cout << #x << ": " << x << endl
#define DEBUG_VEC(v)                                                           \\
  cout << #v << ":";                                                           \\
  REP(i, v.size()) cout << " " << v[i];                                        \\
  cout << endl
#define ALL(a) (a).begin(), (a).end()

#define CHMIN(a, b) a = min((a), (b))
#define CHMAX(a, b) a = max((a), (b))

// mod
const ll MOD = 1000000007ll;
#define FIX(a) ((a) % MOD + MOD) % MOD

// floating
typedef double Real;
const Real EPS = 1e-11;
#define EQ0(x) (abs(x) < EPS)
#define EQ(a, b) (abs(a - b) < EPS)
typedef complex<Real> P;

struct Treap {
private:
  static const ll rd_msk = (1ll << 32) - 1;
  static ll rd() {
    static ll rd_x = 123456789;
    static ll rd_y = 362436069;
    static ll rd_z = 521288629;
    static ll rd_w = 88675123;
    ll t = (rd_x ^ (rd_x << 11)) & rd_msk;
    rd_x = rd_y;
    rd_y = rd_z;
    rd_z = rd_w;
    return rd_w = (rd_w ^ (rd_w >> 19)) ^ (t ^ (t >> 8));
  }
  struct node {
    ll val, pri, cnt, lazy;
    node *lch, *rch;
    node(ll val) : val(val), cnt(1), lazy(0) {
      pri = rd();
      lch = rch = NULL;
    }
  };

public:
  node *root;
  Treap() : root(NULL) {}

private:
  ll count(node *t) { return t == NULL ? 0 : t->cnt; }
  node *push(node *t) {
    if (t && t->lazy != 0) {
      t->val += t->lazy;
      if (t->lch)
        t->lch->lazy += t->lazy;
      if (t->rch)
        t->rch->lazy += t->lazy;
      t->lazy = 0;
    }
    return t;
  }
  node *update(node *t) {
    t->cnt = count(t->lch) + count(t->rch) + 1;
    return t;
  }
  node *merge(node *l, node *r) {
    push(l);
    push(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, ll k) {
    push(t);
    if (!t)
      return make_pair<node *, node *>(NULL, NULL);
    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);
    }
  }
  ll __find(node *p, ll v) {
    push(p);
    if (!p)
      return 0;
    if (p->val == v)
      return count(p->lch);
    return (p->val > v) ? __find(p->lch, v)
                        : (count(p->lch) + 1 + __find(p->rch, v));
  }
  ll __less(node *p, ll v) {
    if (!p)
      return 0;
    push(p);
    return (p->val >= v) ? __less(p->lch, v)
                         : (count(p->lch) + 1 + __less(p->rch, v));
  }
  ll __less_eq(node *p, ll v) {
    if (!p)
      return 0;
    push(p);
    return (p->val > v) ? __less_eq(p->lch, v)
                        : (count(p->lch) + 1 + __less_eq(p->rch, v));
  }
  ll __get_at(node *p, ll k, ll acc) {
    if (!p)
      return -1;
    push(p);
    ll lc = count(p->lch);
    if (acc + lc == k)
      return p->val;
    if (acc + lc > k)
      return __get_at(p->lch, k, acc);
    if (acc + lc < k)
      return __get_at(p->rch, k, acc + lc + 1);
  }

public:
  void merge(Treap that) { root = merge(root, that.root); }
  Treap split(ll k) {
    Treap that;
    pair<node *, node *> p = split(root, k);
    root = p.first;
    that.root = p.second;
    return that;
  }
  void insert_at(ll v, ll k) {
    pair<node *, node *> p = split(root, k);
    node *nn = new node(v);
    root = merge(p.first, nn);
    root = merge(root, p.second);
  }
  void erase_at(ll k) {
    pair<node *, node *> p, q;
    q = split(root, k + 1);
    p = split(q.first, k);
    root = merge(p.first, q.second);
  }
  void insert_by(ll v) { insert_at(v, __less(root, v)); }
  void erase_by(ll v) { erase_at(__find(root, v)); }
  ll count_less(ll v) { return __less(root, v); }
  ll count_less_equal(ll v) { return __less_eq(root, v); }
  ll get_at(ll k) { return __get_at(root, k, 0); }
  ll size() { return count(root); }
  void add(int l, int r, ll v) {
    if (r <= l)
      return;
    pair<node *, node *> p, q;
    q = split(root, r);
    p = split(q.first, l);
    p.second->lazy += v;
    push(p.second);
    root = merge(p.first, p.second);
    root = merge(q.first, q.second);
  }
};

string s;
Treap *pos[26];
int cnt[26];
int po[125252];

int main() {
  cin >> s;
  int n = s.size();
  REP(i, 26) pos[i] = new Treap();
  REP(i, n) pos[s[i] - 'a']->insert_by(i);
  REP(i, 26) if (pos[i]->size() % 2 == 1) {
    puts("-1");
    return 0;
  }
  int n2 = n / 2;
  // ?????????
  int c0 = 0;
  int c1 = 0;
  int poyo = 0;
  REP(i, n) {
    int c = s[i] - 'a';
    cnt[c] += 1;
    if (cnt[c] > pos[c]->size() / 2) {
      po[i] = 1;
      c1 += 1;
    } else {
      po[i] = 0;
      poyo += c1;
    }
  }
  string t;
  REP(i, n) {
    if (po[i] == 0)
      t += s[i];
  }
  REP(i, n) {
    if (po[i] == 1)
      t += s[i];
  }
  s = t;
  int ans = 0;
  REP(i, n2) {
    int x, y;
    int xx = 830252521, yy = -1;
    REP(j, 26) {
      int head = pos[j]->get_at(0);
      int tail = pos[j]->get_at(pos[j]->size() - 1);
      if (head != -1 && head < xx) {
        xx = head;
        x = j;
      }
      if (tail > yy) {
        yy = tail;
        y = j;
      }
    }
    if (x == y) {
      // erase only
      pos[x]->erase_at(pos[x]->size() - 1);
      pos[x]->erase_at(0);
      continue;
    }
    // x--x
    int dx = pos[x]->get_at(pos[x]->size() - 1);
    // y--y
    int dy = pos[y]->get_at(0);
    if (yy - dx < dy - xx) {
      // x--x
      pos[x]->erase_at(pos[x]->size() - 1);
      pos[x]->erase_at(0);
      REP(j, 26) {
        int c = pos[j]->count_less(dx);
        pos[j]->add(c, pos[j]->size(), -1);
      }
      ans += yy - dx;
    } else {
      // y--y
      pos[y]->erase_at(pos[y]->size() - 1);
      pos[y]->erase_at(0);
      REP(j, 26) {
        int c = pos[j]->count_less(dy);
        pos[j]->add(0, c, 1);
      }
      ans += dy - xx;
    }
  }
  printf("%d\\n", ans + poyo);
  return 0;
}