Problem p03483 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <memory.h>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <utility>
#include <vector>

using namespace std;

#define mod 1000000007

class SegTree {
private:
  int n;
  vector<int> v;

public:
  SegTree(int k) {
    n = 1;
    while (n < k)
      n = 2 * n;
    v = vector<int>(2 * n, 0);
  }

  void add(int x, int val) {
    x += n - 1;
    v[x] += val;
    while (x > 0) {
      x = (x - 1) / 2;
      v[x] = v[2 * x + 1] + v[2 * x + 2];
    }
  }

  int getsum(int tl, int tr, int nl, int nr, int index) {
    if (tr <= nl || nr <= tl)
      return 0;
    if (tl <= nl && nr <= tr)
      return v[index];
    int mid = (nl + nr) / 2;
    return getsum(tl, tr, nl, mid, 2 * index + 1) +
           getsum(tl, tr, mid, nr, 2 * index + 2);
  }

  int getsum(int l, int r) { return getsum(l, r, 0, n, 0); }
};

int main() {
  string s;
  cin >> s;
  vector<deque<int>> place(26, deque<int>());
  for (int i = 0; i < s.length(); i++) {
    place[s[i] - 'a'].push_back(i);
  }

  // 各文字について、先に場所を決める
  int correctplace[100001];
  for (int i = 0; i < s.length() / 2; i++) {
    int minindex = -1;
    for (int j = 0; j < 26; j++) {
      if (place[j].size() < 2)
        continue;
      if (minindex < 0 || place[j].front() < place[minindex].front())
        minindex = j;
    }
    if (minindex < 0) {
      cout << -1 << endl;
      return 0;
    }
    correctplace[place[minindex].front()] = i;
    correctplace[place[minindex].back()] = s.length() - 1 - i;
    place[minindex].pop_front();
    place[minindex].pop_back();
  }
  if (s.length() % 2 == 1) {
    for (int j = 0; j < 26; j++) {
      if (place[j].size() == 1) {
        correctplace[place[j].front()] = s.length() / 2;
        break;
      }
    }
  }

  SegTree segTree = SegTree(s.length());
  long long int ans = 0;
  for (int i = 0; i < s.length(); i++) {
    ans += segTree.getsum(correctplace[i], s.length());
    segTree.add(correctplace[i], 1);
  }
  cout << ans << endl;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <memory.h>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <utility>
#include <vector>

using namespace std;

#define mod 1000000007

class SegTree {
private:
  int n;
  vector<int> v;

public:
  SegTree(int k) {
    n = 1;
    while (n < k)
      n = 2 * n;
    v = vector<int>(2 * n, 0);
  }

  void add(int x, int val) {
    x += n - 1;
    v[x] += val;
    while (x > 0) {
      x = (x - 1) / 2;
      v[x] = v[2 * x + 1] + v[2 * x + 2];
    }
  }

  int getsum(int tl, int tr, int nl, int nr, int index) {
    if (tr <= nl || nr <= tl)
      return 0;
    if (tl <= nl && nr <= tr)
      return v[index];
    int mid = (nl + nr) / 2;
    return getsum(tl, tr, nl, mid, 2 * index + 1) +
           getsum(tl, tr, mid, nr, 2 * index + 2);
  }

  int getsum(int l, int r) { return getsum(l, r, 0, n, 0); }
};

int main() {
  string s;
  cin >> s;
  vector<deque<int>> place(26, deque<int>());
  for (int i = 0; i < s.length(); i++) {
    place[s[i] - 'a'].push_back(i);
  }

  // 各文字について、先に場所を決める
  int correctplace[200001];
  for (int i = 0; i < s.length() / 2; i++) {
    int minindex = -1;
    for (int j = 0; j < 26; j++) {
      if (place[j].size() < 2)
        continue;
      if (minindex < 0 || place[j].front() < place[minindex].front())
        minindex = j;
    }
    if (minindex < 0) {
      cout << -1 << endl;
      return 0;
    }
    correctplace[place[minindex].front()] = i;
    correctplace[place[minindex].back()] = s.length() - 1 - i;
    place[minindex].pop_front();
    place[minindex].pop_back();
  }
  if (s.length() % 2 == 1) {
    for (int j = 0; j < 26; j++) {
      if (place[j].size() == 1) {
        correctplace[place[j].front()] = s.length() / 2;
        break;
      }
    }
  }

  SegTree segTree = SegTree(s.length());
  long long int ans = 0;
  for (int i = 0; i < s.length(); i++) {
    ans += segTree.getsum(correctplace[i], s.length());
    segTree.add(correctplace[i], 1);
  }
  cout << ans << endl;
}