Problem p03483 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;

const double EPS = 1e-10;
const ll INF = 100000000;
const ll MOD = 1000000007;

const int MAX_N = 18;

struct segtree {
  int n, dat[2 * MAX_N - 1]; //(1<<17) = 130000

  void init(int n_) {
    n = 1;
    while (n < n_)
      n *= 2;
    for (int i = 0; i < 2 * n - 1; i++)
      dat[i] = 0;
  }

  int search(int k) {
    k += n - 1;
    int ret = 0;
    ret += dat[k];
    while (k > 0) {
      k = (k - 1) / 2;
      ret += dat[k];
    }
    return ret;
  }

  //(a,b,0,0,seg.n)で呼ぶ
  void add(int a, int b, int k, int l, int r) {
    if (r <= a || b <= l)
      return;
    if (a <= l && r <= b) {
      dat[k]++;
    } else {
      add(a, b, k * 2 + 1, l, (l + r) / 2);
      add(a, b, k * 2 + 2, (l + r) / 2, r);
    }
    return;
  }
} seg;

string s;
bool used[200000];
deque<int> deq[26];

int main() {
  cin >> s;
  while (true) {
    bool end = true;
    rep(i, 26) deq[i].clear();
    rep(i, s.size()) { deq[(int)(s[i] - 'a')].push_back(i); }
    bool ki = false;
    rep(i, 26) {
      if (deq[i].size() % 2 == 1) {
        if (ki) {
          cout << -1 << endl;
          return 0;
        }
        ki = true;
        if (deq[i][deq[i].size() / 2] > s.size() / 2) {
          end = false;
          reverse(s.begin(), s.end());
          break;
        }
      }
    }
    if (end)
      break;
  }
  seg.init(s.size());
  ll ans = 0;
  int pos = 0;
  bool mid = false;
  for (int i = s.size() - 1; i >= 0; i--) {
    if (used[i])
      continue;
    if (mid)
      ans++;
    used[i] = true;
    int ind = s[i] - 'a';
    deq[ind].pop_back();
    if (deq[ind].size() == 0) {
      mid = true;
      continue;
    }
    int x = deq[ind].front();
    deq[ind].pop_front();
    used[x] = true;
    int dis = seg.search(x) + x;
    ans += abs(dis - pos);
    seg.add(0, x, 0, 0, seg.n);
    pos++;
  }
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;

const double EPS = 1e-10;
const ll INF = 100000000;
const ll MOD = 1000000007;

const int MAX_N = (1 << 18);

struct segtree {
  int n, dat[2 * MAX_N - 1]; //(1<<17) = 130000

  void init(int n_) {
    n = 1;
    while (n < n_)
      n *= 2;
    for (int i = 0; i < 2 * n - 1; i++)
      dat[i] = 0;
  }

  int search(int k) {
    k += n - 1;
    int ret = 0;
    ret += dat[k];
    while (k > 0) {
      k = (k - 1) / 2;
      ret += dat[k];
    }
    return ret;
  }

  //(a,b,0,0,seg.n)で呼ぶ
  void add(int a, int b, int k, int l, int r) {
    if (r <= a || b <= l)
      return;
    if (a <= l && r <= b) {
      dat[k]++;
    } else {
      add(a, b, k * 2 + 1, l, (l + r) / 2);
      add(a, b, k * 2 + 2, (l + r) / 2, r);
    }
    return;
  }
} seg;

string s;
bool used[200000];
deque<int> deq[26];

int main() {
  cin >> s;
  while (true) {
    bool end = true;
    rep(i, 26) deq[i].clear();
    rep(i, s.size()) { deq[(int)(s[i] - 'a')].push_back(i); }
    bool ki = false;
    rep(i, 26) {
      if (deq[i].size() % 2 == 1) {
        if (ki) {
          cout << -1 << endl;
          return 0;
        }
        ki = true;
        if (deq[i][deq[i].size() / 2] > s.size() / 2) {
          end = false;
          reverse(s.begin(), s.end());
          break;
        }
      }
    }
    if (end)
      break;
  }
  seg.init(s.size());
  ll ans = 0;
  int pos = 0;
  bool mid = false;
  for (int i = s.size() - 1; i >= 0; i--) {
    if (used[i])
      continue;
    if (mid)
      ans++;
    used[i] = true;
    int ind = s[i] - 'a';
    deq[ind].pop_back();
    if (deq[ind].size() == 0) {
      mid = true;
      continue;
    }
    int x = deq[ind].front();
    deq[ind].pop_front();
    used[x] = true;
    int dis = seg.search(x) + x;
    ans += abs(dis - pos);
    seg.add(0, x, 0, 0, seg.n);
    pos++;
  }
  cout << ans << endl;
}