Problem p03483 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

#define pii pair<int, int>
#define f first
#define s second
#define pb push_back
#define mp make_pair
#define ll long long
#define ld long double
#define sz(v) int(v.size())
#define all(v) v.begin(), v.end()
#define vec vector<int>
#define dead not_bad
#define bad gooood

#define left not_right
#define y1 what

using namespace std;

const int N = (int)150;
const int M = (int)20;
const ll LINF = (ll)1e18;
const int INF = (int)1e9 + 7;
const int ALPHA = 26;
const int mod = INF + 2;
const double PI = 3.14159265359;
const ld EPS = (ld)1e-12;

const int nx[4] = {0, 0, -1, 1};
const int ny[4] = {1, -1, 0, 0};

string st;
set<int> v[26];

int t[N];
int n;

void upd(int x, int val) {
  for (; x < n; x |= x + 1)
    t[x] += val;
}

int get(int x) {
  int ans = 0;
  while (x >= 0) {
    ans += t[x];
    x = (x & (x + 1)) - 1;
  }
  return ans;
}

int get_sum(int l, int r) { return get(r) - get(l - 1); }

int main() {
#define fn "saddle"
#ifdef witch
  freopen("input.txt", "r", stdin);
//    freopen("output.txt", "w", stdout);
#else
  // freopen(fn".in", "r", stdin);
  // freopen(fn".out", "w", stdout);
#endif
  cin >> st;
  n = sz(st);
  int cnt = 0;
  for (int i = 0; i < n; i++) {
    v[st[i] - 'a'].insert(i);
    upd(i, 1);
  }
  for (int i = 0; i < 26; i++) {
    cnt += sz(v[i]) % 2;
  }
  if ((sz(st) % 2 && cnt != 1) || (sz(st) % 2 == 0 && cnt != 0))
    return cout << -1, 0;
  ll ans = 0;
  while (true) {
    int cur = INF;
    for (int i = 0; i < 26; i++) {
      if (sz(v[i]) < 2)
        continue;
      int lo = *v[i].begin();
      int hi = *(--v[i].end());
      cur = min(cur, get_sum(0, lo - 1) + get_sum(hi + 1, n - 1));
    }
    if (cur == INF)
      break;
    for (int i = 0; i < 26; i++) {
      if (sz(v[i]) < 2)
        continue;
      int lo = *v[i].begin();
      int hi = *(--v[i].end());
      if (cur == get_sum(0, lo - 1) + get_sum(hi + 1, n - 1)) {
        upd(lo, -1);
        upd(hi, -1);
        v[i].erase(v[i].begin());
        v[i].erase(--v[i].end());
        break;
      }
    }
    ans += cur;
  }
  cout << ans;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

#define pii pair<int, int>
#define f first
#define s second
#define pb push_back
#define mp make_pair
#define ll long long
#define ld long double
#define sz(v) int(v.size())
#define all(v) v.begin(), v.end()
#define vec vector<int>
#define dead not_bad
#define bad gooood

#define left not_right
#define y1 what

using namespace std;

const int N = (int)2e5 + 10;
const int M = (int)20;
const ll LINF = (ll)1e18;
const int INF = (int)1e9 + 7;
const int ALPHA = 26;
const int mod = INF + 2;
const double PI = 3.14159265359;
const ld EPS = (ld)1e-12;

const int nx[4] = {0, 0, -1, 1};
const int ny[4] = {1, -1, 0, 0};

string st;
set<int> v[26];

int t[N];
int n;

void upd(int x, int val) {
  for (; x < n; x |= x + 1)
    t[x] += val;
}

int get(int x) {
  int ans = 0;
  while (x >= 0) {
    ans += t[x];
    x = (x & (x + 1)) - 1;
  }
  return ans;
}

int get_sum(int l, int r) { return get(r) - get(l - 1); }

int main() {
#define fn "saddle"
#ifdef witch
  freopen("input.txt", "r", stdin);
//    freopen("output.txt", "w", stdout);
#else
  // freopen(fn".in", "r", stdin);
  // freopen(fn".out", "w", stdout);
#endif
  cin >> st;
  n = sz(st);
  int cnt = 0;
  for (int i = 0; i < n; i++) {
    v[st[i] - 'a'].insert(i);
    upd(i, 1);
  }
  for (int i = 0; i < 26; i++) {
    cnt += sz(v[i]) % 2;
  }
  if ((sz(st) % 2 && cnt != 1) || (sz(st) % 2 == 0 && cnt != 0))
    return cout << -1, 0;
  ll ans = 0;
  while (true) {
    int cur = INF;
    for (int i = 0; i < 26; i++) {
      if (sz(v[i]) < 2)
        continue;
      int lo = *v[i].begin();
      int hi = *(--v[i].end());
      cur = min(cur, get_sum(0, lo - 1) + get_sum(hi + 1, n - 1));
    }
    if (cur == INF)
      break;
    for (int i = 0; i < 26; i++) {
      if (sz(v[i]) < 2)
        continue;
      int lo = *v[i].begin();
      int hi = *(--v[i].end());
      if (cur == get_sum(0, lo - 1) + get_sum(hi + 1, n - 1)) {
        upd(lo, -1);
        upd(hi, -1);
        v[i].erase(v[i].begin());
        v[i].erase(--v[i].end());
        break;
      }
    }
    ans += cur;
  }
  cout << ans;
  return 0;
}