Problem p03483 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
#define fi first
#define se second
#define mp make_pair
#define rep(i, n) for (int i = 0; i < n; ++i)
#define rrep(i, n) for (int i = n; i >= 0; --i)
const int inf = 1e9 + 7;
const ll mod = 1e9 + 7;
const ll mod1 = 998244353;
const ll big = 1e18;
const double PI = 2 * asin(1);

int bit[200005];

void add(int x) {
  while (x < 200005) {
    bit[x]++;
    x += x & -x;
  }
}

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

int main() {
  string S;
  cin >> S;
  vector<char> arr;
  map<char, int> amap;
  for (int i = 0; i < S.size(); ++i) {
    if (amap[S[i]] == 0)
      arr.push_back(S[i]);
    amap[S[i]]++;
  }
  bool ok = true;
  if (S.size() % 2 == 0) {
    for (int i = 0; i < arr.size(); ++i) {
      if (amap[arr[i]] % 2 == 1)
        ok = false;
    }
  } else {
    int one = 0;
    for (int i = 0; i < arr.size(); ++i) {
      if (amap[arr[i]] % 2 == 1)
        one++;
    }
    if (one != 1)
      ok = false;
  }
  if (!ok) {
    cout << -1 << endl;
    return 0;
  }
  map<char, int> bmap;
  string ans;
  char tmp;
  for (int i = 0; i < S.size(); ++i) {
    if (amap[S[i]] == 1) {
      tmp = S[i];
      continue;
    }
    if (bmap[S[i]] < amap[S[i]] / 2) {
      ans.push_back(S[i]);
      bmap[S[i]]++;
    }
  }
  string ans1 = ans;
  if (S.size() % 2 == 1)
    ans.push_back(tmp);
  for (int i = ans1.size() - 1; i >= 0; --i)
    ans.push_back(ans1[i]);
  vector<int> arr1;
  map<char, vector<int>> cmap;
  for (int i = 0; i < ans.size(); ++i) {
    cmap[ans[i]].push_back(i + 1);
  }
  map<char, int> dmap;
  for (int i = 0; i < S.size(); ++i) {
    arr1.push_back(cmap[S[i]][dmap[S[i]]]);
    dmap[S[i]]++;
  }
  ll ans2 = 0;
  for (int i = 0; i < arr1.size(); ++i) {
    ans2 += i - sum(arr1[i]);
    add(arr1[i]);
  }
  cout << ans2 << endl;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
#define fi first
#define se second
#define mp make_pair
#define rep(i, n) for (int i = 0; i < n; ++i)
#define rrep(i, n) for (int i = n; i >= 0; --i)
const int inf = 1e9 + 7;
const ll mod = 1e9 + 7;
const ll mod1 = 998244353;
const ll big = 1e18;
const double PI = 2 * asin(1);

int bit[200005];

void add(int x) {
  while (x < 200005) {
    bit[x]++;
    x += x & -x;
  }
}

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

int main() {
  string S;
  cin >> S;
  vector<char> arr;
  map<char, int> amap;
  for (int i = 0; i < S.size(); ++i) {
    if (amap[S[i]] == 0)
      arr.push_back(S[i]);
    amap[S[i]]++;
  }
  bool ok = true;
  if (S.size() % 2 == 0) {
    for (int i = 0; i < arr.size(); ++i) {
      if (amap[arr[i]] % 2 == 1)
        ok = false;
    }
  } else {
    int one = 0;
    for (int i = 0; i < arr.size(); ++i) {
      if (amap[arr[i]] % 2 == 1)
        one++;
    }
    if (one != 1)
      ok = false;
  }
  if (!ok) {
    cout << -1 << endl;
    return 0;
  }
  map<char, int> bmap;
  string ans;
  char tmp;
  for (int i = 0; i < S.size(); ++i) {
    if (amap[S[i]] % 2 == 1) {
      if (bmap[S[i]] < amap[S[i]] / 2) {
        ans.push_back(S[i]);
        bmap[S[i]]++;
      }
      tmp = S[i];
      continue;
    }
    if (bmap[S[i]] < amap[S[i]] / 2) {
      ans.push_back(S[i]);
      bmap[S[i]]++;
    }
  }
  string ans1 = ans;
  if (S.size() % 2 == 1)
    ans.push_back(tmp);
  for (int i = ans1.size() - 1; i >= 0; --i)
    ans.push_back(ans1[i]);
  vector<int> arr1;
  map<char, vector<int>> cmap;
  for (int i = 0; i < ans.size(); ++i) {
    cmap[ans[i]].push_back(i + 1);
  }
  map<char, int> dmap;
  for (int i = 0; i < S.size(); ++i) {
    arr1.push_back(cmap[S[i]][dmap[S[i]]]);
    dmap[S[i]]++;
  }
  ll ans2 = 0;
  for (int i = 0; i < arr1.size(); ++i) {
    ans2 += i - sum(arr1[i]);
    add(arr1[i]);
  }
  cout << ans2 << endl;
}