Problem p03357 (C++)

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

typedef long long ll;

struct BIT {
  int size;
  vector<ll> t;
  void init(int n) {
    size = n;
    t.resize(n, 0);
  }

  void add(int i, ll x) {
    i++;
    while (i < size) {
      t[i] += x;
      i += (i & -i);
    }
  }

  ll sum(int i) {
    i++;
    ll res = 0;
    while (i) {
      res += t[i];
      i -= (i & -i);
    }
    return res;
  }
};

int n;
char c[2000];
int a[2000];

int B[2000];
int W[2000];

int dp[2005][2005];
int countW[2005][2005];
int countB[2005][2005];

int main() {
  cin >> n;

  for (int i = 0; i < n * 2; i++) {
    cin >> c[i] >> a[i];
    a[i]--;
    if (c[i] == 'B')
      B[a[i]] = i;
    if (c[i] == 'W')
      W[a[i]] = i;
  }

  for (int i = 0; i <= n; i++) {
    BIT T;
    T.init(5000);
    for (int j = 0; j < 4500; j++)
      T.add(j, 1);
    for (int j = 0; j < i; j++)
      T.add(B[j], -1);

    for (int j = 0; j < n; j++) {
      T.add(W[j], -1);
      countW[i][j] = T.sum(W[j]);
    }
  }

  for (int i = 0; i <= n; i++) {
    BIT T;
    T.init(5000);
    for (int j = 0; j < 4500; j++)
      T.add(j, 1);
    for (int j = 0; j < i; j++)
      T.add(W[j], -1);

    for (int j = 0; j < n; j++) {
      T.add(B[j], -1);
      countB[j][i] = T.sum(B[j]);
    }
  }
  for (int i = 0; i <= n; i++)
    for (int j = 0; j <= n; j++)
      dp[i][j] = 1e9;

  dp[0][0] = 0;
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= n; j++) {
      //      cout<<i<<j<<' '<<countW[i][j]<<' '<<countB[i][j]<<endl;
      if (j < n)
        dp[i][j + 1] = min(dp[i][j + 1], dp[i][j] + countW[i][j]);

      if (i < n)
        dp[i + 1][j] = min(dp[i + 1][j], dp[i][j] + countB[i][j]);
    }
  }
  cout << dp[n][n] << endl;
  return 0;
}


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

typedef long long ll;

struct BIT {
  int size;
  vector<ll> t;
  void init(int n) {
    size = n;
    t.resize(n, 0);
  }

  void add(int i, ll x) {
    i++;
    while (i < size) {
      t[i] += x;
      i += (i & -i);
    }
  }

  ll sum(int i) {
    i++;
    ll res = 0;
    while (i) {
      res += t[i];
      i -= (i & -i);
    }
    return res;
  }
};

int n;
char c[4000];
int a[4000];

int B[2000];
int W[2000];

int dp[2005][2005];
int countW[2005][2005];
int countB[2005][2005];

int main() {
  cin >> n;

  for (int i = 0; i < n * 2; i++) {
    cin >> c[i] >> a[i];
    a[i]--;
    if (c[i] == 'B')
      B[a[i]] = i;
    if (c[i] == 'W')
      W[a[i]] = i;
  }

  for (int i = 0; i <= n; i++) {
    BIT T;
    T.init(5000);
    for (int j = 0; j < 4500; j++)
      T.add(j, 1);
    for (int j = 0; j < i; j++)
      T.add(B[j], -1);

    for (int j = 0; j < n; j++) {
      T.add(W[j], -1);
      countW[i][j] = T.sum(W[j]);
    }
  }

  for (int i = 0; i <= n; i++) {
    BIT T;
    T.init(5000);
    for (int j = 0; j < 4500; j++)
      T.add(j, 1);
    for (int j = 0; j < i; j++)
      T.add(W[j], -1);

    for (int j = 0; j < n; j++) {
      T.add(B[j], -1);
      countB[j][i] = T.sum(B[j]);
    }
  }
  for (int i = 0; i <= n; i++)
    for (int j = 0; j <= n; j++)
      dp[i][j] = 1e9;

  dp[0][0] = 0;
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= n; j++) {
      //      cout<<i<<j<<' '<<countW[i][j]<<' '<<countB[i][j]<<endl;
      if (j < n)
        dp[i][j + 1] = min(dp[i][j + 1], dp[i][j] + countW[i][j]);

      if (i < n)
        dp[i + 1][j] = min(dp[i + 1][j], dp[i][j] + countB[i][j]);
    }
  }
  cout << dp[n][n] << endl;
  return 0;
}