Problem p03483 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using LL = long long;

class SegmentTreeRSQ {
private:
  int n;
  vector<LL> node, lazy;

public:
  void init(vector<LL> v) {
    int sz = (int)v.size();
    n = 1;
    while (n < sz)
      n *= 2;
    node.resize(2 * n - 1);
    lazy.resize(2 * n - 1, 0);

    for (int i = 0; i < sz; i++)
      node[i + n - 1] = v[i];
    for (int i = n - 2; i >= 0; i--)
      node[i] = node[i * 2 + 1] + node[i * 2 + 2];
  }

  void eval(int k, int l, int r) {
    if (lazy[k] != 0) {
      node[k] += lazy[k];
      if (r - l > 1) {
        lazy[2 * k + 1] += lazy[k] / 2;
        lazy[2 * k + 2] += lazy[k] / 2;
      }
      lazy[k] = 0;
    }
  }

  void add(int a, int b, LL x, int k = 0, int l = 0, int r = -1) {
    if (r < 0)
      r = n;
    eval(k, l, r);
    if (b <= l || r <= a)
      return;
    if (a <= l && r <= b) {
      lazy[k] += (r - l) * x;
      eval(k, l, r);
    } else {
      add(a, b, x, 2 * k + 1, l, (l + r) / 2);
      add(a, b, x, 2 * k + 2, (l + r) / 2, r);
      node[k] = node[2 * k + 1] + node[2 * k + 2];
    }
  }

  LL getsum(int a, int b, int k = 0, int l = 0, int r = -1) {
    if (r < 0)
      r = n;
    eval(k, l, r);
    if (b <= l || r <= a)
      return 0;
    if (a <= l && r <= b)
      return node[k];
    LL vl = getsum(a, b, 2 * k + 1, l, (l + r) / 2);
    LL vr = getsum(a, b, 2 * k + 2, (l + r) / 2, r);
    return vl + vr;
  }
};

int main() {
  string s;
  cin >> s;
  vector<deque<int>> cnt(26);
  vector<LL> vec;
  for (int a = 0; a < s.size(); a++) {
    cnt.at(s.at(a) - 'a').push_back(a);
    vec.push_back(a);
  }
  map<char, int> mp;
  for (int a = 0; a < s.size(); a++) {
    mp[s[a]]++;
  }
  int result = 0;
  for (auto a : mp) {
    if (a.second % 2 == 1)
      result++;
  }
  if (s.size() % 2 != result) {
    cout << -1 << endl;
    return 0;
  }
  SegmentTreeRSQ Seg;
  Seg.init(vec);
  LL ans = 0;
  for (int i = 0; i < s.size() / 2; i++) {
    int cost = 1e9;
    int index = -1;
    for (int j = 0; j < 26; j++) {
      if (cnt.at(j).size() <= 1)
        continue;
      if (cost >
          Seg.getsum(cnt.at(j).front(), cnt.at(j).front() + 1) +
              (s.size() - 1 -
               Seg.getsum(cnt.at(j).back(), cnt.at(j).back() + 1) - i * 2)) {
        cost = Seg.getsum(cnt.at(j).front(), cnt.at(j).front() + 1) +
               (s.size() - 1 -
                Seg.getsum(cnt.at(j).back(), cnt.at(j).back() + 1) - i * 2);
        index = j;
      }
    }
    //     cout<<cnt.at(index).front()<<" "<<cnt.at(index).back()<<endl;
    ans += cost;
    Seg.add(cnt.at(index).front(), s.size() - 1, -1);
    Seg.add(cnt.at(index).back(), s.size() - 1, -1);
    cnt.at(index).pop_front();
    cnt.at(index).pop_back();
  }
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using LL = long long;

class SegmentTreeRSQ {
private:
  int n;
  vector<LL> node, lazy;

public:
  void init(vector<LL> v) {
    int sz = (int)v.size();
    n = 1;
    while (n < sz)
      n *= 2;
    node.resize(2 * n - 1);
    lazy.resize(2 * n - 1, 0);

    for (int i = 0; i < sz; i++)
      node[i + n - 1] = v[i];
    for (int i = n - 2; i >= 0; i--)
      node[i] = node[i * 2 + 1] + node[i * 2 + 2];
  }

  void eval(int k, int l, int r) {
    if (lazy[k] != 0) {
      node[k] += lazy[k];
      if (r - l > 1) {
        lazy[2 * k + 1] += lazy[k] / 2;
        lazy[2 * k + 2] += lazy[k] / 2;
      }
      lazy[k] = 0;
    }
  }

  void add(int a, int b, LL x, int k = 0, int l = 0, int r = -1) {
    if (r < 0)
      r = n;
    eval(k, l, r);
    if (b <= l || r <= a)
      return;
    if (a <= l && r <= b) {
      lazy[k] += (r - l) * x;
      eval(k, l, r);
    } else {
      add(a, b, x, 2 * k + 1, l, (l + r) / 2);
      add(a, b, x, 2 * k + 2, (l + r) / 2, r);
      node[k] = node[2 * k + 1] + node[2 * k + 2];
    }
  }

  LL getsum(int a, int b, int k = 0, int l = 0, int r = -1) {
    if (r < 0)
      r = n;
    eval(k, l, r);
    if (b <= l || r <= a)
      return 0;
    if (a <= l && r <= b)
      return node[k];
    LL vl = getsum(a, b, 2 * k + 1, l, (l + r) / 2);
    LL vr = getsum(a, b, 2 * k + 2, (l + r) / 2, r);
    return vl + vr;
  }
};

int main() {
  string s;
  cin >> s;
  vector<deque<int>> cnt(26);
  vector<LL> vec;
  for (int a = 0; a < s.size(); a++) {
    cnt.at(s.at(a) - 'a').push_back(a);
    vec.push_back(a);
  }
  map<char, int> mp;
  for (int a = 0; a < s.size(); a++) {
    mp[s[a]]++;
  }
  int result = 0;
  for (auto a : mp) {
    if (a.second % 2 == 1)
      result++;
  }
  if (s.size() % 2 != result) {
    cout << -1 << endl;
    return 0;
  }
  SegmentTreeRSQ Seg;
  Seg.init(vec);
  LL ans = 0;
  for (int i = 0; i < s.size() / 2; i++) {
    int cost = 1e9;
    int index = -1;
    for (int j = 0; j < 26; j++) {
      if (cnt.at(j).size() <= 1)
        continue;
      if (cost >
          Seg.getsum(cnt.at(j).front(), cnt.at(j).front() + 1) +
              (s.size() - 1 -
               Seg.getsum(cnt.at(j).back(), cnt.at(j).back() + 1) - i * 2)) {
        cost = Seg.getsum(cnt.at(j).front(), cnt.at(j).front() + 1) +
               (s.size() - 1 -
                Seg.getsum(cnt.at(j).back(), cnt.at(j).back() + 1) - i * 2);
        index = j;
      }
    }
    //     cout<<cnt.at(index).front()<<" "<<cnt.at(index).back()<<endl;
    ans += cost;
    Seg.add(cnt.at(index).front(), s.size(), -1);
    Seg.add(cnt.at(index).back(), s.size(), -1);
    cnt.at(index).pop_front();
    cnt.at(index).pop_back();
  }
  cout << ans << endl;
}