Problem p03483 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <ext/pb_ds/priority_queue.hpp> //__gnu_pbds::priority_queue

using std::pair;
using std::string;
using std::vector;

typedef long long ll;
typedef pair<int, int> pii;

#define fst first
#define snd second
#define pb(a) push_back(a)
#define mp(a, b) std::make_pair(a, b)
#define debug(...) fprintf(stderr, __VA_ARGS__)

template <typename T> bool chkmax(T &a, T b) { return a < b ? a = b, 1 : 0; }
template <typename T> bool chkmin(T &a, T b) { return a > b ? a = b, 1 : 0; }

const int N = 400000;
const int oo = 0x3f3f3f3f;

template <typename T> T read(T &x) {
  int f = 1;
  x = 0;
  char ch = getchar();
  for (; !isdigit(ch); ch = getchar())
    if (ch == '-')
      f = -1;
  for (; isdigit(ch); ch = getchar())
    x = x * 10 + ch - 48;
  return x *= f;
}

namespace BIT {
#define lowbit(x) (x & -x)
int c[N + 5];
void add(int x, int y) {
  while (x <= N) {
    c[x] += y;
    x += lowbit(x);
  }
}
int query(int x) {
  int res = 0;
  while (x > 0) {
    res += c[x];
    x -= lowbit(x);
  }
  return res;
}
} // namespace BIT

int n;
char st[N + 5];
std::deque<int> p[30];
std::set<pair<int, char>> s;

int main() {
#ifdef Wearry
  freopen("data.txt", "r", stdin);
  freopen("ans.txt", "w", stdout);
#endif

  ll ans = 0;
  scanf("%s", st);
  n = strlen(st);
  for (int i = 0; i < n; ++i) {
    p[st[i] - 'a'].pb(i);
    s.insert(mp(i, st[i]));
    BIT::add(i + 1, 1);
  }

  bool flag = 0;
  for (int i = 0; i <= n / 2; ++i) {
    char cur = (*s.rbegin()).snd;

    if (p[cur - 'a'].size() <= 1) {
      if (!flag)
        flag = 1;
      else
        return puts("-1"), 0;
    } else {
      ans += BIT::query(p[cur - 'a'].front() + 1) - 1;
      s.erase(s.find(mp(p[cur - 'a'].front(), cur)));

      BIT::add(p[cur - 'a'].front() + 1, -1);
      p[cur - 'a'].pop_back();
      p[cur - 'a'].pop_front();

      if (flag)
        ++ans;
    }
    s.erase(*s.rbegin());
  }
  printf("%lld\n", ans);

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#include <ext/pb_ds/priority_queue.hpp> //__gnu_pbds::priority_queue

using std::pair;
using std::string;
using std::vector;

typedef long long ll;
typedef pair<int, int> pii;

#define fst first
#define snd second
#define pb(a) push_back(a)
#define mp(a, b) std::make_pair(a, b)
#define debug(...) fprintf(stderr, __VA_ARGS__)

template <typename T> bool chkmax(T &a, T b) { return a < b ? a = b, 1 : 0; }
template <typename T> bool chkmin(T &a, T b) { return a > b ? a = b, 1 : 0; }

const int N = 400000;
const int oo = 0x3f3f3f3f;

template <typename T> T read(T &x) {
  int f = 1;
  x = 0;
  char ch = getchar();
  for (; !isdigit(ch); ch = getchar())
    if (ch == '-')
      f = -1;
  for (; isdigit(ch); ch = getchar())
    x = x * 10 + ch - 48;
  return x *= f;
}

namespace BIT {
#define lowbit(x) (x & -x)
int c[N + 5];
void add(int x, int y) {
  while (x <= N) {
    c[x] += y;
    x += lowbit(x);
  }
}
int query(int x) {
  int res = 0;
  while (x > 0) {
    res += c[x];
    x -= lowbit(x);
  }
  return res;
}
} // namespace BIT

int n;
char st[N + 5];
std::deque<int> p[30];
std::set<pair<int, char>> s;

int main() {
#ifdef Wearry
  freopen("data.txt", "r", stdin);
  freopen("ans.txt", "w", stdout);
#endif

  ll ans = 0;
  scanf("%s", st);
  n = strlen(st);
  for (int i = 0; i < n; ++i) {
    p[st[i] - 'a'].pb(i);
    s.insert(mp(i, st[i]));
    BIT::add(i + 1, 1);
  }

  bool flag = 0;
  for (int i = 0; i < 26; ++i)
    if (p[i].size() & 1) {
      if (flag) {
        puts("-1");
        return 0;
      } else
        flag = 1;
    }

  flag = 0;
  for (int i = 0; i < (n + 1) / 2; ++i) {
    char cur = (*s.rbegin()).snd;

    if (p[cur - 'a'].size() <= 1) {
      if (!flag)
        flag = 1;
      else
        return puts("-1"), 0;
    } else {
      ans += BIT::query(p[cur - 'a'].front() + 1) - 1;
      s.erase(s.find(mp(p[cur - 'a'].front(), cur)));

      BIT::add(p[cur - 'a'].front() + 1, -1);
      p[cur - 'a'].pop_back();
      p[cur - 'a'].pop_front();

      if (flag)
        ++ans;
    }
    s.erase(*s.rbegin());
  }
  printf("%lld\n", ans);

  return 0;
}