Problem p03483 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <string>
#include <vector>
#define FOR(i, n) for (int i = 0; i < n; i++)
using namespace std;

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

const int MAXN = 100100;

char s[MAXN];
int n;

void load() {
  scanf("%s", s);
  n = strlen(s);
}

vector<int> v[26];
int par[MAXN], dest[MAXN];

ll sol = 0;

vector<int> merge(vector<int> v1, vector<int> v2) {
  vector<int> ret;
  int id1 = 0, id2 = 0;
  while (true) {
    if (id1 < (int)v1.size()) {
      if (id2 < (int)v2.size()) {
        if (v1[id1] < v2[id2]) {
          ret.push_back(v1[id1++]);
        } else {
          sol += v1.size() - id1;
          ret.push_back(v2[id2++]);
        }
      } else {
        ret.push_back(v1[id1++]);
      }
    } else if (id2 < (int)v2.size()) {
      ret.push_back(v2[id2++]);
    } else
      break;
  }
  return ret;
}

vector<int> divide(vector<int> v) {
  if (v.size() == 1)
    return v;
  int mid = v.size() / 2;
  vector<int> v1, v2;
  FOR(i, mid) v1.push_back(v[i]);
  for (int i = mid; i < (int)v.size(); i++)
    v2.push_back(v[i]);
  v1 = divide(v1);
  v2 = divide(v2);
  return merge(v1, v2);
}

bool solve() {
  FOR(i, n) v[s[i] - 'a'].push_back(i);
  int brOdd = 0;
  FOR(i, n) dest[i] = -1;
  FOR(i, 26) if (v[i].size() & 1)++ brOdd;
  if (brOdd > 0 && n % 2 == 0)
    return false;
  if (brOdd != 1 && n & 1)
    return false;
  FOR(i, 26) {
    int id1 = 0, id2 = v[i].size() - 1;
    while (id1 < id2) {
      par[v[i][id1]] = v[i][id2];
      par[v[i][id2]] = v[i][id1];
      ++id1;
      --id2;
    }
    if (id1 == id2) {
      par[v[i][id1]] = -1;
      dest[v[i][id1]] = n / 2;
    }
  }
  int ind = 0;
  FOR(i, n) {
    if (dest[i] != -1)
      continue;
    int para = par[i];
    dest[i] = ind;
    dest[para] = n - ind - 1;
    ind++;
  }
  // FOR(i, n) printf("%d ", dest[i]); printf("\n");
  divide(vector<int>(dest, dest + n));
  printf("%lld\n", sol);
  return true;
}

int main() {
  load();
  if (!solve())
    printf("-1\n");
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <string>
#include <vector>
#define FOR(i, n) for (int i = 0; i < n; i++)
using namespace std;

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

const int MAXN = 200100;

char s[MAXN];
int n;

void load() {
  scanf("%s", s);
  n = strlen(s);
}

vector<int> v[26];
int par[MAXN], dest[MAXN];

ll sol = 0;

vector<int> merge(vector<int> v1, vector<int> v2) {
  vector<int> ret;
  int id1 = 0, id2 = 0;
  while (true) {
    if (id1 < (int)v1.size()) {
      if (id2 < (int)v2.size()) {
        if (v1[id1] < v2[id2]) {
          ret.push_back(v1[id1++]);
        } else {
          sol += v1.size() - id1;
          ret.push_back(v2[id2++]);
        }
      } else {
        ret.push_back(v1[id1++]);
      }
    } else if (id2 < (int)v2.size()) {
      ret.push_back(v2[id2++]);
    } else
      break;
  }
  return ret;
}

vector<int> divide(vector<int> v) {
  if (v.size() == 1)
    return v;
  int mid = v.size() / 2;
  vector<int> v1, v2;
  FOR(i, mid) v1.push_back(v[i]);
  for (int i = mid; i < (int)v.size(); i++)
    v2.push_back(v[i]);
  v1 = divide(v1);
  v2 = divide(v2);
  return merge(v1, v2);
}

bool solve() {
  FOR(i, n) v[s[i] - 'a'].push_back(i);
  int brOdd = 0;
  FOR(i, n) dest[i] = -1;
  FOR(i, 26) if (v[i].size() & 1)++ brOdd;
  if (brOdd > 0 && n % 2 == 0)
    return false;
  if (brOdd != 1 && n & 1)
    return false;
  FOR(i, 26) {
    int id1 = 0, id2 = v[i].size() - 1;
    while (id1 < id2) {
      par[v[i][id1]] = v[i][id2];
      par[v[i][id2]] = v[i][id1];
      ++id1;
      --id2;
    }
    if (id1 == id2) {
      par[v[i][id1]] = -1;
      dest[v[i][id1]] = n / 2;
    }
  }
  int ind = 0;
  FOR(i, n) {
    if (dest[i] != -1)
      continue;
    int para = par[i];
    dest[i] = ind;
    dest[para] = n - ind - 1;
    ind++;
  }
  // FOR(i, n) printf("%d ", dest[i]); printf("\n");
  divide(vector<int>(dest, dest + n));
  printf("%lld\n", sol);
  return true;
}

int main() {
  load();
  if (!solve())
    printf("-1\n");
  return 0;
}