Problem p03483 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <tuple>
#include <vector>
using namespace std;

typedef long long ll;
typedef pair<int, int> P;
typedef pair<int, double> Pid;
typedef pair<double, int> Pdi;
typedef pair<ll, int> Pl;
typedef pair<int, pair<int, int>> PP;
const double PI = 3.1415926535897932; // acos(-1)
const double EPS = 1e-15;
const ll mod = 1e+9 + 7;
const int INF = 1001001001;

#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
#define chadd(x, y) x = (x + y) % mod

template <typename Monoid> struct SegmentTree {
  using F = function<Monoid(Monoid, Monoid)>;

  int sz;
  vector<Monoid> seg;

  const F f; // モノイドに対して二項演算を行う関数オブジェクト
  const Monoid M1;

  SegmentTree(int n, const F f, const Monoid &M1) : f(f), M1(M1) {
    sz = 1;
    while (sz < n)
      sz <<= 1;
    seg.assign(2 * sz, M1);
  }

  void set(int k, const Monoid &x) { seg[k + sz] = x; }

  void build() {
    for (int k = sz - 1; k > 0; --k) {
      seg[k] = f(seg[k << 1], seg[k << 1 | 1]);
    }
  }

  void update(int k, const Monoid &x) {
    k += sz;
    seg[k] = x;
    while (k >>= 1) {
      seg[k] = f(seg[k << 1], seg[k << 1 | 1]);
    }
  }

  Monoid query(int a, int b) {
    Monoid L = M1, R = M1;
    for (a += sz, b += sz; a < b; a >>= 1, b >>= 1) {
      if (a & 1)
        L = f(L, seg[a++]);
      if (b & 1)
        R = f(seg[--b], R);
    }
    return f(L, R);
  }

  Monoid operator[](const int &k) const { return seg[k + sz]; }
};

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  string s;
  cin >> s;
  int n = s.length();
  vector<vector<int>> pos(26);
  for (int i = 0; i < n; ++i) {
    int id = s[i] - 'a';
    pos[id].push_back(i);
  }
  int oddcnt = 0, evencnt = 0;
  for (int i = 0; i < 26; ++i) {
    if (pos[i].size() == 0)
      continue;
    if (pos[i].size() % 2 == 0)
      ++evencnt;
    else
      ++oddcnt;
  }
  if (n % 2 == 0 && oddcnt > 0) {
    cout << -1 << endl;
    return 0;
  }
  if (n % 2 != 0 && oddcnt != 1) {
    cout << -1 << endl;
    return 0;
  }

  vector<P> left, middle;        // (pos, char)
  vector<vector<int>> right(26); // (char, pos)
  for (int i = 0; i < 26; ++i) {
    int sz = pos[i].size();
    if (sz == 0)
      continue;

    if (sz % 2 == 0) {
      for (int j = 0; j < sz / 2; ++j)
        left.push_back(P(pos[i][j], i));
      for (int j = sz / 2; j < sz; ++j)
        right[i].push_back(pos[i][j]);
    } else {
      for (int j = 0; j < sz / 2; ++j)
        left.push_back(P(pos[i][j], i));
      middle.push_back(P(pos[i][sz / 2], i));
      for (int j = sz / 2 + 1; j < sz; ++sz)
        right[i].push_back(pos[i][j]);
    }
    sort(right[i].begin(), right[i].end());
  }
  sort(left.begin(), left.end());

  vector<int> next(26, 0); // right において、次はどの index であるかを表す
  vector<P> p(n);
  priority_queue<P> que;
  for (int i = 0; i < left.size(); ++i) {
    p[i] = left[i];
    que.push(P(p[i].first, i));
  }
  if (n % 2 == 0) {
    for (int i = n / 2; i > 0; --i) {
      int char_id = p[i - 1].second;
      p[n - i] = P(right[char_id][next[char_id]++], char_id);
      que.push(P(p[n - i].first, n - i));
    }
  } else { // n % 2 != 0
    p[n / 2] = middle[0];
    que.push(P(p[n / 2].first, n / 2));
    for (int i = n / 2; i > 0; --i) {
      int char_id = p[i - 1].second;
      p[n - i] = P(right[char_id][next[char_id]++], char_id);
      que.push(P(p[n - i].first, n - i));
    }
  }

  SegmentTree<int> inversion(
      n, [](int a, int b) { return a + b; }, 0);
  ll res = 0;
  while (!que.empty()) {
    P hoge = que.top();
    que.pop();

    int val = hoge.first, id = hoge.second;
    inversion.update(id, 1);
    res += inversion.query(0, id);
  }
  cout << res << endl;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <tuple>
#include <vector>
using namespace std;

typedef long long ll;
typedef pair<int, int> P;
typedef pair<int, double> Pid;
typedef pair<double, int> Pdi;
typedef pair<ll, int> Pl;
typedef pair<int, pair<int, int>> PP;
const double PI = 3.1415926535897932; // acos(-1)
const double EPS = 1e-15;
const ll mod = 1e+9 + 7;
const int INF = 1001001001;

#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
#define chadd(x, y) x = (x + y) % mod

template <typename Monoid> struct SegmentTree {
  using F = function<Monoid(Monoid, Monoid)>;

  int sz;
  vector<Monoid> seg;

  const F f; // モノイドに対して二項演算を行う関数オブジェクト
  const Monoid M1;

  SegmentTree(int n, const F f, const Monoid &M1) : f(f), M1(M1) {
    sz = 1;
    while (sz < n)
      sz <<= 1;
    seg.assign(2 * sz, M1);
  }

  void set(int k, const Monoid &x) { seg[k + sz] = x; }

  void build() {
    for (int k = sz - 1; k > 0; --k) {
      seg[k] = f(seg[k << 1], seg[k << 1 | 1]);
    }
  }

  void update(int k, const Monoid &x) {
    k += sz;
    seg[k] = x;
    while (k >>= 1) {
      seg[k] = f(seg[k << 1], seg[k << 1 | 1]);
    }
  }

  Monoid query(int a, int b) {
    Monoid L = M1, R = M1;
    for (a += sz, b += sz; a < b; a >>= 1, b >>= 1) {
      if (a & 1)
        L = f(L, seg[a++]);
      if (b & 1)
        R = f(seg[--b], R);
    }
    return f(L, R);
  }

  Monoid operator[](const int &k) const { return seg[k + sz]; }
};

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  string s;
  cin >> s;
  int n = s.length();
  vector<vector<int>> pos(26);
  for (int i = 0; i < n; ++i) {
    int id = s[i] - 'a';
    pos[id].push_back(i);
  }
  int oddcnt = 0, evencnt = 0;
  for (int i = 0; i < 26; ++i) {
    if (pos[i].size() == 0)
      continue;
    if (pos[i].size() % 2 == 0)
      ++evencnt;
    else
      ++oddcnt;
  }
  if (n % 2 == 0 && oddcnt > 0) {
    cout << -1 << endl;
    return 0;
  }
  if (n % 2 != 0 && oddcnt != 1) {
    cout << -1 << endl;
    return 0;
  }

  vector<P> left, middle;        // (pos, char)
  vector<vector<int>> right(26); // (char, pos)
  for (int i = 0; i < 26; ++i) {
    int sz = pos[i].size();
    if (sz == 0)
      continue;

    if (sz % 2 == 0) {
      for (int j = 0; j < sz / 2; ++j)
        left.push_back(P(pos[i][j], i));
      for (int j = sz / 2; j < sz; ++j)
        right[i].push_back(pos[i][j]);
    } else {
      for (int j = 0; j < sz / 2; ++j)
        left.push_back(P(pos[i][j], i));
      middle.push_back(P(pos[i][sz / 2], i));
      for (int j = sz / 2 + 1; j < sz; ++j)
        right[i].push_back(pos[i][j]);
    }
    sort(right[i].begin(), right[i].end());
  }
  sort(left.begin(), left.end());

  vector<int> next(26, 0); // right において、次はどの index であるかを表す
  vector<P> p(n);
  priority_queue<P> que;
  for (int i = 0; i < left.size(); ++i) {
    p[i] = left[i];
    que.push(P(p[i].first, i));
  }
  if (n % 2 == 0) {
    for (int i = n / 2; i > 0; --i) {
      int char_id = p[i - 1].second;
      p[n - i] = P(right[char_id][next[char_id]++], char_id);
      que.push(P(p[n - i].first, n - i));
    }
  } else { // n % 2 != 0
    p[n / 2] = middle[0];
    que.push(P(p[n / 2].first, n / 2));
    for (int i = n / 2; i > 0; --i) {
      int char_id = p[i - 1].second;
      p[n - i] = P(right[char_id][next[char_id]++], char_id);
      que.push(P(p[n - i].first, n - i));
    }
  }

  SegmentTree<int> inversion(
      n, [](int a, int b) { return a + b; }, 0);
  ll res = 0;
  while (!que.empty()) {
    P hoge = que.top();
    que.pop();

    int val = hoge.first, id = hoge.second;
    inversion.update(id, 1);
    res += inversion.query(0, id);
  }
  cout << res << endl;
}