Problem p03483 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define lsb(x) (x & (-x))
#define ll long long

using namespace std;

const int MAXN = (int)2e5;

string str;

int fr[30];

vector<int> pos[30];

int ind[30];

int aib[MAXN + 1][2];
int n;

inline void update(int pos, bool t) {
  for (int i = pos; i <= n; i++)
    aib[i][t]++;
}

inline int query(int pos, bool t) {
  int ans = 0;
  for (int i = pos; i > 0; i -= lsb(i))
    ans += aib[i][t];
  return ans;
}

inline int check(int p) {
  return (query(n, 1) - query(p, 1)) - query(p - 1, 0);
}

int main() {
  // ifstream cin("A.in");
  // ofstream cout("A.out");
  int i, j;
  ios::sync_with_stdio(false);
  cin >> str;
  n = str.size();
  str = " " + str;
  for (i = 1; i <= n; i++) {
    str[i] -= 'a';
    fr[str[i]]++;
    pos[str[i]].push_back(i);
  }
  int cnt = 0;
  for (i = 0; i < 26; i++) {
    if (fr[i] % 2 == 1) {
      cnt++;
    }
  }
  if (cnt > 1) {
    cout << -1;
    return 0;
  }
  ll ans = 0;
  pos[29].push_back(n + 1);
  pos[29].push_back(0);
  int lim = n / 2;
  if (n % 2 == 0)
    lim--;
  for (i = 1; i <= lim; i++) {
    int ch1 = 29, ch2 = 29;
    for (j = 0; j < 26; j++) {
      if (pos[j].size() > ind[j]) {
        if (pos[j][ind[j]] < pos[ch1][ind[ch1]])
          ch1 = j;
        if (pos[j].back() > pos[ch2].back())
          ch2 = j;
      }
    }
    int pa = pos[ch1].back(), pb = pos[ch2][ind[ch2]];
    // printf("%c %c\n" ,ch1 + 'a',ch2 + 'a');
    if (pos[ch1].size() == 1)
      pa = -n;
    if (pos[ch2].size() == 1)
      pb = 2 * n;
    if (pa != -n)
      pa = pa + check(pa);
    if (pb != 2 * n)
      pb = pb + check(pb);
    if (pb - i < n - i + 1 - pa) {
      ans += pb - i;
      update(pos[ch2][ind[ch2]], 1);
      ind[ch2]++;
      pos[ch2].pop_back();
    } else {
      ans += n - i + 1 - pa;
      update(pos[ch1].back(), 0);
      ind[ch1]++;
      pos[ch1].pop_back();
    }
  }
  cout << ans;
  // cin.close();
  // cout.close();
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define lsb(x) (x & (-x))
#define ll long long

using namespace std;

const int MAXN = (int)2e5;

string str;

int fr[30];

vector<int> pos[30];

int ind[30];

int aib[MAXN + 1][2];
int n;

inline void update(int pos, bool t) {
  for (int i = pos; i <= n; i += lsb(i))
    aib[i][t]++;
}

inline int query(int pos, bool t) {
  int ans = 0;
  for (int i = pos; i > 0; i -= lsb(i))
    ans += aib[i][t];
  return ans;
}

inline int check(int p) {
  return (query(n, 1) - query(p, 1)) - query(p - 1, 0);
}

int main() {
  // ifstream cin("A.in");
  // ofstream cout("A.out");
  int i, j;
  ios::sync_with_stdio(false);
  cin >> str;
  n = str.size();
  str = " " + str;
  for (i = 1; i <= n; i++) {
    str[i] -= 'a';
    fr[str[i]]++;
    pos[str[i]].push_back(i);
  }
  int cnt = 0;
  for (i = 0; i < 26; i++) {
    if (fr[i] % 2 == 1) {
      cnt++;
    }
  }
  if (cnt > 1) {
    cout << -1;
    return 0;
  }
  ll ans = 0;
  pos[29].push_back(n + 1);
  pos[29].push_back(0);
  int lim = n / 2;
  if (n % 2 == 0)
    lim--;
  for (i = 1; i <= lim; i++) {
    int ch1 = 29, ch2 = 29;
    for (j = 0; j < 26; j++) {
      if (pos[j].size() > ind[j]) {
        if (pos[j][ind[j]] < pos[ch1][ind[ch1]])
          ch1 = j;
        if (pos[j].back() > pos[ch2].back())
          ch2 = j;
      }
    }
    int pa = pos[ch1].back(), pb = pos[ch2][ind[ch2]];
    // printf("%c %c\n" ,ch1 + 'a',ch2 + 'a');
    if (pos[ch1].size() == 1)
      pa = -n;
    if (pos[ch2].size() == 1)
      pb = 2 * n;
    if (pa != -n)
      pa = pa + check(pa);
    if (pb != 2 * n)
      pb = pb + check(pb);
    if (pb - i < n - i + 1 - pa) {
      ans += pb - i;
      update(pos[ch2][ind[ch2]], 1);
      ind[ch2]++;
      pos[ch2].pop_back();
    } else {
      ans += n - i + 1 - pa;
      update(pos[ch1].back(), 0);
      ind[ch1]++;
      pos[ch1].pop_back();
    }
  }
  cout << ans;
  // cin.close();
  // cout.close();
  return 0;
}