Problem p01081 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cfloat>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <time.h>
#include <vector>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;

int N;
int *BIT;

void add(int loc, int value) {

  BIT[loc] += value;

  loc += loc & -loc;

  while (loc <= N) {
    BIT[loc] += value;
    loc += loc & -loc;
  }
}

int getSum(int loc) {

  int sum = BIT[loc];

  loc -= loc & -loc;

  while (loc > 0) {
    sum += BIT[loc];
    loc -= loc & -loc;
  }
  return sum;
}

char buf[400001], next_buf[400001];
int table[26], index[26], loc[26][200000], ruisekiwa[400000];
bool must_left[400000];

int main() {

  for (int i = 0; i < 26; i++) {
    table[i] = 0;
    index[i] = 0;
  }

  int length, tmp;

  scanf("%s", buf);

  for (length = 0; buf[length] != '\\0'; length++)
    ;

  for (int i = 0; i < length; i++) {
    tmp = buf[i] - 'a';
    table[tmp]++;
    if (index[tmp] < length / 2) {
      loc[tmp][index[tmp]] = i;
      index[tmp]++;
    }
  }

  for (int i = 0; i < 26; i++) {
    if (table[i] % 2 == 1) {
      printf("-1\\n");
      return 0;
    }
  }

  for (int i = 0; i < length; i++)
    must_left[i] = 0;

  for (int i = 0; i < 26; i++) {
    for (int k = 0; k < table[i] / 2; k++) {
      must_left[loc[i][k]] = true;
    }
  }

  ll ans = 0;

  int left_index = 0, right_index = 0;

  for (int i = 0; i < length; i++)
    ruisekiwa[i] = 0;
  for (int i = 0; i < length; i++) {
    if (must_left[i] == false) {
      next_buf[length / 2 + right_index++] = buf[i];
      ruisekiwa[i]++;
    } else {
      next_buf[left_index++] = buf[i];
    }
    if (i > 0) {
      ruisekiwa[i] += ruisekiwa[i - 1];
    }
    if (must_left[i]) {
      ans += ruisekiwa[i];
    }
  }

  for (int i = 0; i < 26; i++)
    index[i] = 0;

  for (int i = 0; i < length / 2; i++) {
    tmp = next_buf[i] - 'a';

    loc[tmp][(table[tmp] / 2 - 1) - index[tmp]] = (length / 2) - i;
    index[tmp]++;
  }

  for (int i = 0; i < 26; i++)
    index[i] = 0;

  int *right_half = new int[length / 2];

  for (int i = 0; i < length / 2; i++) {
    tmp = next_buf[(length / 2) + i] - 'a';
    right_half[i] = loc[tmp][index[tmp]];
    index[tmp]++;
  }

  N = length / 2;
  BIT = new int[N + 1];

  for (int i = 0; i <= N; i++)
    BIT[i] = 0;

  for (int i = 0; i < N; i++) {
    ans += i - getSum(right_half[i]);
    add(right_half[i], 1);
  }

  printf("%lld\\n", ans);

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cfloat>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <time.h>
#include <vector>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;

int N;
int *BIT;

void add(int loc, int value) {

  BIT[loc] += value;

  loc += loc & -loc;

  while (loc <= N) {
    BIT[loc] += value;
    loc += loc & -loc;
  }
}

int getSum(int loc) {

  int sum = BIT[loc];

  loc -= loc & -loc;

  while (loc > 0) {
    sum += BIT[loc];
    loc -= loc & -loc;
  }
  return sum;
}

char buf[400001];
int table[26], index[26], loc[26][200000];
bool must_left[400000];

int main() {

  for (int i = 0; i < 26; i++) {
    table[i] = 0;
    index[i] = 0;
  }

  int length, tmp;

  scanf("%s", buf);

  for (length = 0; buf[length] != '\\0'; length++)
    ;

  for (int i = 0; i < length; i++) {
    tmp = buf[i] - 'a';
    table[tmp]++;
    if (index[tmp] < length / 2) {
      loc[tmp][index[tmp]] = i;
      index[tmp]++;
    }
  }

  for (int i = 0; i < 26; i++) {
    if (table[i] % 2 == 1) {
      printf("-1\\n");
      return 0;
    }
  }

  for (int i = 0; i < length; i++)
    must_left[i] = 0;

  for (int i = 0; i < 26; i++) {
    for (int k = 0; k < table[i] / 2; k++) {
      must_left[loc[i][k]] = true;
    }
  }

  ll ans = 0;

  for (int i = 0; i < length / 2; i++) {
    if (must_left[i])
      continue;
    for (tmp = i + 1; tmp < length; tmp++) {
      if (must_left[tmp]) {
        break;
      }
    }
    while (must_left[tmp - 1] == false) {
      swap(buf[tmp - 1], buf[tmp]);
      swap(must_left[tmp - 1], must_left[tmp]);
      ans++;
      tmp -= 1;
    }
  }

  for (int i = 0; i < 26; i++)
    index[i] = 0;

  for (int i = 0; i < length / 2; i++) {
    tmp = buf[i] - 'a';

    loc[tmp][(table[tmp] / 2 - 1) - index[tmp]] = (length / 2) - i;
    index[tmp]++;
  }

  for (int i = 0; i < 26; i++)
    index[i] = 0;

  int *right_half = new int[length / 2];

  for (int i = 0; i < length / 2; i++) {
    tmp = buf[(length / 2) + i] - 'a';
    right_half[i] = loc[tmp][index[tmp]];
    index[tmp]++;
  }

  N = length / 2;
  BIT = new int[N + 1];

  for (int i = 0; i <= N; i++)
    BIT[i] = 0;

  for (int i = 0; i < N; i++) {
    ans += i - getSum(right_half[i]);
    add(right_half[i], 1);
  }

  printf("%lld\\n", ans);

  return 0;
}