Problem p03483 (C++)

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

using namespace std;

struct segtree {
  vector<long long> t;
  segtree(int n) : t(n * 2 - 1, 0) {}
  void inc(int a, int o, int l, int r) {
    if (r <= a || a < l)
      return;
    t[o]++;
    if (l + 1 == r)
      return;
    int m = (l + r) / 2;
    inc(a, o + 1, l, m);
    inc(a, o + (m - l) * 2, m, r);
  }
  long long sum(int a, int b, int o, int l, int r) const {
    if (r <= a || b <= l)
      return 0;
    if (a <= l && r <= b)
      return t[o];
    int m = (l + r) / 2;
    return sum(a, b, o + 1, l, m) + sum(a, b, o + (m - l) * 2, m, r);
  }
};

long long bub(const vector<int> &v, int n) {
  segtree s(n);
  auto r = 0ll;
  for (auto i : v) {
    r += s.sum(i + 1, n, 0, 0, n);
    s.inc(i, 0, 0, n);
  }
  return r;
}

struct cm {
  bool operator()(const pair<int, int> &p, const pair<int, int> &q) {
    return p.second < q.second;
  }
};

int main() {
  string s;
  cin >> s;
  vector<pair<int, int>> v(s.length());
  vector<int> u(s.length());
  int ch[26] = {};
  for (int i = 0; i < s.length(); i++) {
    v[i].first = s[i] - 'a';
    ch[v[i].first]++;
  }
  bool o = false;
  for (int i = 0; i < 26; i++) {
    if (ch[i] % 2) {
      if (o) {
        cout << -1 << endl;
        return 0;
      }
      o = true;
    }
  }
  int c[26] = {}, n = 0;
  for (auto &p : v) {
    if (c[p.first] * 2 >= ch[p.first])
      p.second = 2;
    else if (c[p.first] == ch[p.first] / 2)
      p.second = 1;
    u[n++] = p.second;
    c[p.first]++;
  }
  auto r = bub(u, 3);
  stable_sort(v.begin(), v.end(), cm());
  vector<int> chv[26];
  for (int i = 0; i < v.size() / 2; i++)
    chv[v[v.size() - 1 - i].first].push_back(i);
  fill(c, c + 26, 0);
  for (int i = 0; i < v.size() / 2; i++)
    u[i] = chv[v[i].first][c[v[i].first]++];
  u.resize(v.size() / 2);
  cout << r + bub(u, u.size()) << endl;
  return 0;
}


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

using namespace std;

struct segtree {
  vector<long long> t;
  segtree(int n) : t(n * 2 - 1, 0) {}
  void inc(int a, int o, int l, int r) {
    if (r <= a || a < l)
      return;
    t[o]++;
    if (l + 1 == r)
      return;
    int m = (l + r) / 2;
    inc(a, o + 1, l, m);
    inc(a, o + (m - l) * 2, m, r);
  }
  long long sum(int a, int b, int o, int l, int r) const {
    if (r <= a || b <= l)
      return 0;
    if (a <= l && r <= b)
      return t[o];
    int m = (l + r) / 2;
    return sum(a, b, o + 1, l, m) + sum(a, b, o + (m - l) * 2, m, r);
  }
};

long long bub(const vector<int> &v, int n) {
  if (n < 1)
    return 0;
  segtree s(n);
  auto r = 0ll;
  for (auto i : v) {
    r += s.sum(i + 1, n, 0, 0, n);
    s.inc(i, 0, 0, n);
  }
  return r;
}

struct cm {
  bool operator()(const pair<int, int> &p, const pair<int, int> &q) {
    return p.second < q.second;
  }
};

int main() {
  string s;
  cin >> s;
  vector<pair<int, int>> v(s.length());
  vector<int> u(s.length());
  int ch[26] = {};
  for (int i = 0; i < s.length(); i++) {
    v[i].first = s[i] - 'a';
    ch[v[i].first]++;
  }
  bool o = false;
  for (int i = 0; i < 26; i++) {
    if (ch[i] % 2) {
      if (o) {
        cout << -1 << endl;
        return 0;
      }
      o = true;
    }
  }
  int c[26] = {}, n = 0;
  for (auto &p : v) {
    if (c[p.first] * 2 >= ch[p.first])
      p.second = 2;
    else if (c[p.first] == ch[p.first] / 2)
      p.second = 1;
    u[n++] = p.second;
    c[p.first]++;
  }
  auto r = bub(u, 3);
  stable_sort(v.begin(), v.end(), cm());
  vector<int> chv[26];
  for (int i = 0; i < v.size() / 2; i++)
    chv[v[v.size() - 1 - i].first].push_back(i);
  fill(c, c + 26, 0);
  for (int i = 0; i < v.size() / 2; i++)
    u[i] = chv[v[i].first][c[v[i].first]++];
  u.resize(v.size() / 2);
  cout << r + bub(u, u.size()) << endl;
  return 0;
}