Problem p03483 (C++)

Buggy submission (status: Memory Limit Exceeded):
#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <cassert>
#include <cctype>
#include <complex>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <time.h>
#include <typeinfo>
#include <unordered_map>
#include <utility>
#include <vector>
using namespace std;
typedef string::const_iterator State;
#define Ma_PI 3.141592653589793
#define eps 0.00000001
#define LONG_INF 1e18
#define GOLD 1.61803398874989484820458
#define MAX_MOD 1000000007
#define MOD 998244353
#define seg_size 262144
#define REP(i, n) for (long long i = 0; i < n; ++i)
long long gogo[400000];
long long cnt[400000];
long long now_cnt[400000];
long long seg_tree[seg_size * 4];
queue<long long> findings[400000];
long long seg_find(long long now, long long n_l, long long n_r, long long w_l,
                   long long w_r) {
  if (w_l <= n_l && n_r <= w_r)
    return seg_tree[now];
  if (w_r <= n_l || n_r <= w_l)
    return 0;
  return seg_find(now * 2, n_l, (n_l + n_r) / 2, w_l, w_r) +
         seg_find(now * 2 + 1, (n_l + n_r) / 2, n_r, w_l, w_r);
}
long long seg_set(long long now) {
  seg_tree[now] = seg_tree[now * 2] + seg_tree[now * 2 + 1];
  if (now != 1)
    seg_set(now / 2);
  return 0;
}
int main() {
#define int long long
  string s;
  cin >> s;
  REP(i, s.length()) { cnt[s[i] - 'a']++; }
  int hoge = 0;
  REP(i, 26) { hoge += (cnt[i]) % 2; }
  if (hoge >= 2) {
    cout << -1 << endl;
    return 0;
  }
  string a, b;
  long long ans = 0;
  long long tmp = 0;
  REP(i, s.length()) {
    int hoge = s[i] - 'a';
    now_cnt[hoge]++;
    if (cnt[hoge] % 2 == 1 && (cnt[hoge] + 1) / 2 == now_cnt[hoge]) {
      ans += b.length();
      tmp++;
    } else {
      if (cnt[hoge] / 2 >= now_cnt[hoge]) {
        a.push_back(s[i]);
        ans += tmp;
      } else {
        b.push_back(s[i]);
        tmp++;
      }
    }
  }
  reverse(b.begin(), b.end());
  REP(i, b.length()) {
    seg_tree[(seg_size + i)]++;
    seg_set((seg_size + i) / 2);
    findings[b[i] - 'a'].push(i);
  }
  REP(i, a.length()) {
    int hoge = a[i] - 'a';
    long long now = findings[hoge].front();
    findings[hoge].pop();
    ans += seg_find(1, 0, seg_size, 0, now);
    seg_tree[(seg_size + now)]--;
    seg_set((seg_size + now) / 2);
  }
  cout << ans << endl;
}

Fixed submission:
#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <cassert>
#include <cctype>
#include <complex>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <time.h>
#include <typeinfo>
#include <unordered_map>
#include <utility>
#include <vector>
using namespace std;
typedef string::const_iterator State;
#define Ma_PI 3.141592653589793
#define eps 0.00000001
#define LONG_INF 1e18
#define GOLD 1.61803398874989484820458
#define MAX_MOD 1000000007
#define MOD 998244353
#define seg_size 262144
#define REP(i, n) for (long long i = 0; i < n; ++i)
long long gogo[400000];
long long cnt[26];
long long now_cnt[26];
long long seg_tree[seg_size * 2];
queue<long long> findings[26];
long long seg_find(long long now, long long n_l, long long n_r, long long w_l,
                   long long w_r) {
  if (w_l <= n_l && n_r <= w_r)
    return seg_tree[now];
  if (w_r <= n_l || n_r <= w_l)
    return 0;
  return seg_find(now * 2, n_l, (n_l + n_r) / 2, w_l, w_r) +
         seg_find(now * 2 + 1, (n_l + n_r) / 2, n_r, w_l, w_r);
}
long long seg_set(long long now) {
  seg_tree[now] = seg_tree[now * 2] + seg_tree[now * 2 + 1];
  if (now != 1)
    seg_set(now / 2);
  return 0;
}
int main() {
#define int long long
  string s;
  cin >> s;
  REP(i, s.length()) { cnt[s[i] - 'a']++; }
  int hoge = 0;
  REP(i, 26) { hoge += (cnt[i]) % 2; }
  if (hoge >= 2) {
    cout << -1 << endl;
    return 0;
  }
  string a, b;
  long long ans = 0;
  long long tmp = 0;
  REP(i, s.length()) {
    int hoge = s[i] - 'a';
    now_cnt[hoge]++;
    if (cnt[hoge] % 2 == 1 && (cnt[hoge] + 1) / 2 == now_cnt[hoge]) {
      ans += b.length();
      tmp++;
    } else {
      if (cnt[hoge] / 2 >= now_cnt[hoge]) {
        a.push_back(s[i]);
        ans += tmp;
      } else {
        b.push_back(s[i]);
        tmp++;
      }
    }
  }
  reverse(b.begin(), b.end());
  REP(i, b.length()) {
    seg_tree[(seg_size + i)]++;
    seg_set((seg_size + i) / 2);
    findings[b[i] - 'a'].push(i);
  }
  REP(i, a.length()) {
    int hoge = a[i] - 'a';
    long long now = findings[hoge].front();
    findings[hoge].pop();
    ans += seg_find(1, 0, seg_size, 0, now);
    seg_tree[(seg_size + now)]--;
    seg_set((seg_size + now) / 2);
  }
  cout << ans << endl;
}