Problem p03483 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <tuple>
#include <vector>
#define mkp make_pair
#define mkt make_tuple
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;
const ll MOD = 1e9 + 7;
template <class T> void chmin(T &a, const T &b) {
  if (a > b)
    a = b;
}
template <class T> void chmax(T &a, const T &b) {
  if (a < b)
    a = b;
}

class BIT { // 1-indexed
public:
  vector<ll> bit;
  BIT() {}
  BIT(int size) { bit.resize(size, 0); }

  ll sum(int i) {
    ll s = 0;
    while (i > 0) {
      s += bit[i];
      i -= i & (-i);
    }
    return s;
  }

  void add(int i, ll x) { // i!=0
    while (i < bit.size()) {
      bit[i] += x;
      i += i & (-i);
    }
  }
};

int alp[26];

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

  string S;
  cin >> S;
  int N = S.size();
  if (N == 1) {
    cout << 0 << endl;
    return 0;
  }
  rep(i, N) alp[S[i] - 'a']++;

  int odd = 0;
  rep(i, 26) if (alp[i] % 2) odd++;
  if (odd > 1) {
    cout << -1 << endl;
    return 0;
  }

  int num[26] = {};
  vector<int> dir(N, 0);
  int cen = -1;
  rep(i, N) {
    int now = S[i] - 'a';
    int n = alp[now];
    if (n % 2 && 2 * num[now] + 1 == n)
      cen = i;
    else if (2 * num[now] < n)
      dir[i] = -1;
    else
      dir[i] = 1;
    num[now]++;
  }

  ll ans = 0;
  if (cen != -1) {
    int res = 0;
    for (int i = cen - 1; i >= 0; i--)
      if (dir[i] == 1)
        res++;
    for (int i = cen + 1; i < N; i++)
      if (dir[i] == -1)
        res++;
    ans += res;
    S.erase(S.begin() + cen);
    N--;
  }

  ll move = 0, rig = 0;
  for (int i = 0; i < N / 2; i++) {
    if (dir[i] == 1)
      rig++;
    else
      move += rig;
  }
  ll lef = 0;
  for (int i = N - 1; i >= N / 2; i--) {
    if (dir[i] == -1)
      lef++;
    else
      move += lef;
  }
  ans += move;
  ans += rig * lef;

  string L = "", R = "";
  for (int i = 0; i < N; i++) {
    if (dir[i] == -1)
      L += S[i];
    else
      R += S[i];
  }
  // cout<<L<<" "<<R<<endl;
  reverse(R.begin(), R.end());

  map<pair<char, int>, int> lmp;
  rep(i, 26) alp[i] = 0;
  rep(i, L.size()) {
    int tar = L[i] - 'a';
    lmp[mkp(L[i], alp[tar])] = i;
    alp[tar]++;
  }
  map<pair<char, int>, int> rmp;
  rep(i, 26) alp[i] = 0;
  rep(i, R.size()) {
    int tar = R[i] - 'a';
    rmp[mkp(R[i], alp[tar])] = i;
    alp[tar]++;
  }

  rep(i, 26) alp[i] = 0;
  vector<int> lalp(26, 0), ralp(26, 0);
  vector<int> lused(L.size(), 0), rused(R.size(), 0);
  set<pair<int, int>> se;
  string last = "";
  rep(i, L.size()) {
    int l = L[i] - 'a';
    int r = R[i] - 'a';
    int pal = rmp[mkp(L[i], lalp[l])];
    int par = lmp[mkp(R[i], ralp[r])];
    if (lused[i] == 0 && rused[pal] == 0) {
      if (i == pal)
        se.insert(mkp(i, 0));
      else if (pal < i)
        se.insert(mkp(i, -1));
      else
        se.insert(mkp(pal, 1));
      rused[pal] = 1;
    }
    if (rused[i] == 0 && lused[par] == 0) {
      if (i == par)
        se.insert(mkp(i, 0));
      else if (par < i)
        se.insert(mkp(i, 1));
      else
        se.insert(mkp(par, -1));
      lused[par] = 1;
    }
    lused[i] = 1;
    rused[i] = 1;

    auto itr = se.begin();
    auto f = *itr;
    char target;
    if (f.second <= 0)
      target = L[f.first];
    else
      target = R[f.first];
    last += target;
    se.erase(itr);
    lalp[l]++;
    ralp[r]++;
  }
  /*set<pair<int,int>> se;
  rep(i,R.size()){
      int tar=R[i]-'a';
      int lef=mp[mkp(R[i],alp[tar])];
      if(lef==i) se.insert(mkp(i,0));
      else if(lef<i) se.insert(mkp(i,1));
      else se.insert(mkp(lef,-1));
      alp[tar]++;
  }

  string last="";
  while(!se.empty()){
      auto itr=se.begin();
      auto f=*itr;
      char tar;
      if(f.second<=0) tar=L[f.first];
      else tar=R[f.first];
      last+=tar;
      se.erase(itr);
  }*/
  // cout<<last<<endl;
  map<pair<char, int>, int> mp;
  rep(i, 26) alp[i] = 0;
  rep(i, last.size()) {
    int tar = last[i] - 'a';
    mp[mkp(last[i], alp[tar])] = i;
    alp[tar]++;
  }
  // cout<<ans<<endl;

  rep(k, 2) {
    rep(i, 26) alp[i] = 0;
    vector<int> v(last.size(), -1);
    rep(i, L.size()) {
      int tar = L[i] - 'a';
      int p = mp[mkp(L[i], alp[tar])];
      v[i] = p + 1;
      alp[tar]++;
    }

    BIT bit(v.size() + 1);
    ll res = 0;
    rep(i, v.size()) {
      int p = bit.sum(v[i]);
      bit.add(v[i], 1);
      res += v[i] - p - 1;
    }
    ans += res;
    swap(L, R);
  }

  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <tuple>
#include <vector>
#define mkp make_pair
#define mkt make_tuple
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;
const ll MOD = 1e9 + 7;
template <class T> void chmin(T &a, const T &b) {
  if (a > b)
    a = b;
}
template <class T> void chmax(T &a, const T &b) {
  if (a < b)
    a = b;
}

class BIT { // 1-indexed
public:
  vector<ll> bit;
  BIT() {}
  BIT(int size) { bit.resize(size, 0); }

  ll sum(int i) {
    ll s = 0;
    while (i > 0) {
      s += bit[i];
      i -= i & (-i);
    }
    return s;
  }

  void add(int i, ll x) { // i!=0
    while (i < bit.size()) {
      bit[i] += x;
      i += i & (-i);
    }
  }
};

int alp[26];

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

  string S;
  cin >> S;
  int N = S.size();
  if (N == 1) {
    cout << 0 << endl;
    return 0;
  }
  rep(i, N) alp[S[i] - 'a']++;

  int odd = 0;
  rep(i, 26) if (alp[i] % 2) odd++;
  if (odd > 1) {
    cout << -1 << endl;
    return 0;
  }

  int num[26] = {};
  vector<int> dir(N, 0);
  int cen = -1;
  rep(i, N) {
    int now = S[i] - 'a';
    int n = alp[now];
    if (n % 2 && 2 * num[now] + 1 == n)
      cen = i;
    else if (2 * num[now] < n)
      dir[i] = -1;
    else
      dir[i] = 1;
    num[now]++;
  }

  ll ans = 0;
  if (cen != -1) {
    int res = 0;
    for (int i = cen - 1; i >= 0; i--)
      if (dir[i] == 1)
        res++;
    for (int i = cen + 1; i < N; i++)
      if (dir[i] == -1)
        res++;
    ans += res;
    for (int i = cen; i < N - 1; i++)
      dir[i] = dir[i + 1];
    S.erase(S.begin() + cen);
    N--;
  }

  ll move = 0, rig = 0;
  for (int i = 0; i < N / 2; i++) {
    if (dir[i] == 1)
      rig++;
    else
      move += rig;
  }
  ll lef = 0;
  for (int i = N - 1; i >= N / 2; i--) {
    if (dir[i] == -1)
      lef++;
    else
      move += lef;
  }
  ans += move;
  ans += rig * lef;

  string L = "", R = "";
  for (int i = 0; i < N; i++) {
    if (dir[i] == -1)
      L += S[i];
    else
      R += S[i];
  }
  // cout<<L<<" "<<R<<endl;
  reverse(R.begin(), R.end());

  map<pair<char, int>, int> lmp;
  rep(i, 26) alp[i] = 0;
  rep(i, L.size()) {
    int tar = L[i] - 'a';
    lmp[mkp(L[i], alp[tar])] = i;
    alp[tar]++;
  }
  map<pair<char, int>, int> rmp;
  rep(i, 26) alp[i] = 0;
  rep(i, R.size()) {
    int tar = R[i] - 'a';
    rmp[mkp(R[i], alp[tar])] = i;
    alp[tar]++;
  }

  rep(i, 26) alp[i] = 0;
  vector<int> lalp(26, 0), ralp(26, 0);
  vector<int> lused(L.size(), 0), rused(R.size(), 0);
  set<pair<int, int>> se;
  string last = "";
  rep(i, L.size()) {
    int l = L[i] - 'a';
    int r = R[i] - 'a';
    int pal = rmp[mkp(L[i], lalp[l])];
    int par = lmp[mkp(R[i], ralp[r])];
    if (lused[i] == 0 && rused[pal] == 0) {
      if (i == pal)
        se.insert(mkp(i, 0));
      else if (pal < i)
        se.insert(mkp(i, -1));
      else
        se.insert(mkp(pal, 1));
      rused[pal] = 1;
    }
    if (rused[i] == 0 && lused[par] == 0) {
      if (i == par)
        se.insert(mkp(i, 0));
      else if (par < i)
        se.insert(mkp(i, 1));
      else
        se.insert(mkp(par, -1));
      lused[par] = 1;
    }
    lused[i] = 1;
    rused[i] = 1;

    auto itr = se.begin();
    auto f = *itr;
    char target;
    if (f.second <= 0)
      target = L[f.first];
    else
      target = R[f.first];
    last += target;
    se.erase(itr);
    lalp[l]++;
    ralp[r]++;
  }
  /*set<pair<int,int>> se;
  rep(i,R.size()){
      int tar=R[i]-'a';
      int lef=mp[mkp(R[i],alp[tar])];
      if(lef==i) se.insert(mkp(i,0));
      else if(lef<i) se.insert(mkp(i,1));
      else se.insert(mkp(lef,-1));
      alp[tar]++;
  }

  string last="";
  while(!se.empty()){
      auto itr=se.begin();
      auto f=*itr;
      char tar;
      if(f.second<=0) tar=L[f.first];
      else tar=R[f.first];
      last+=tar;
      se.erase(itr);
  }*/
  // cout<<last<<endl;
  map<pair<char, int>, int> mp;
  rep(i, 26) alp[i] = 0;
  rep(i, last.size()) {
    int tar = last[i] - 'a';
    mp[mkp(last[i], alp[tar])] = i;
    alp[tar]++;
  }
  // cout<<ans<<endl;

  rep(k, 2) {
    rep(i, 26) alp[i] = 0;
    vector<int> v(last.size(), -1);
    rep(i, L.size()) {
      int tar = L[i] - 'a';
      int p = mp[mkp(L[i], alp[tar])];
      v[i] = p + 1;
      alp[tar]++;
    }

    BIT bit(v.size() + 1);
    ll res = 0;
    rep(i, v.size()) {
      int p = bit.sum(v[i]);
      bit.add(v[i], 1);
      res += v[i] - p - 1;
    }
    ans += res;
    swap(L, R);
  }

  cout << ans << endl;

  return 0;
}