Problem p03357 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"

using namespace std;

typedef long long ll;
constexpr int INF = numeric_limits<int>::max() / 2;

// BIT
// initialize : FenwickTree(n)
// add(pos, n): data[pos] += n;
// sum(l, r)  : sum [l, r)
template <typename T> class FenwickTree {
  const int n;
  vector<T> data;

public:
  FenwickTree(int count) : n(count), data(count, 0) { ; }

  void add(int pos, const T &value) {
    assert(0 <= pos && pos < n);
    for (int i = pos; i < n; i |= i + 1)
      data[i] += value;
  }

  T sum(int pos) const {
    assert(0 <= pos && pos <= n);
    T res = 0;
    for (int i = pos - 1; i >= 0; i = (i & (i + 1)) - 1) {
      res += data[i];
    }
    return res;
  }

  T sum(int l, int r) const {
    assert(0 <= l && l <= r && r <= n);
    return sum(r) + (-sum(l));
  }
  using value_type = T;
  using update_type = T;
};

vector<vector<ll>> dp(2010, vector<ll>(2010, INF));
// costb[i][j] := i black, j white -> i+1 black
vector<vector<ll>> costb(1010, vector<ll>(1010, 0)),
    costw(1010, vector<ll>(1010, 0));

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int n;
  cin >> n;
  vector<ll> a(2 * n);
  vector<char> c(2 * n);
  for (int i = 0; i < 2 * n; i++) {
    cin >> c[i] >> a[i];
  }

  for (int j = 0; j <= n; j++) {
    int wh = 0;
    FenwickTree<int> ft(n + 10);
    for (int i = 0; i < 2 * n; i++) {
      if (c[i] == 'W') {
        if (a[i] > j) {
          wh++;
        }
      } else {
        int x = a[i];
        costb[x - 1][j] = ft.sum(x + 1, n + 1) + wh;
        ft.add(x, 1);
      }
    }
  }

  for (int i = 0; i <= n; i++) {
    int bl = 0;
    FenwickTree<int> ft(n + 10);
    for (int j = 0; j < 2 * n; j++) {
      if (c[j] == 'B') {
        if (a[j] > i) {
          bl++;
        }
      } else {
        int y = a[j];
        costw[i][y - 1] = ft.sum(y + 1, n + 1) + bl;
        ft.add(y, 1);
      }
    }
  }

  dp[0][0] = 0;
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= n; j++) {
      if (i == 0 && j == 0)
        continue;
      if (i == 0) {
        dp[i][j] = dp[i][j - 1] + costw[i][j - 1];
      } else if (j == 0) {
        dp[i][j] = dp[i - 1][j] + costb[i - 1][j];
      } else {
        dp[i][j] =
            min(dp[i][j - 1] + costw[i][j - 1], dp[i - 1][j] + costb[i - 1][j]);
      }
    }
  }
  cout << dp[n][n] << endl;
}


Fixed submission:
#include "bits/stdc++.h"

using namespace std;

typedef long long ll;
constexpr int INF = numeric_limits<int>::max() / 2;

// BIT
// initialize : FenwickTree(n)
// add(pos, n): data[pos] += n;
// sum(l, r)  : sum [l, r)
template <typename T> class FenwickTree {
  const int n;
  vector<T> data;

public:
  FenwickTree(int count) : n(count), data(count, 0) { ; }

  void add(int pos, const T &value) {
    assert(0 <= pos && pos < n);
    for (int i = pos; i < n; i |= i + 1)
      data[i] += value;
  }

  T sum(int pos) const {
    assert(0 <= pos && pos <= n);
    T res = 0;
    for (int i = pos - 1; i >= 0; i = (i & (i + 1)) - 1) {
      res += data[i];
    }
    return res;
  }

  T sum(int l, int r) const {
    assert(0 <= l && l <= r && r <= n);
    return sum(r) + (-sum(l));
  }
  using value_type = T;
  using update_type = T;
};

vector<vector<ll>> dp(2010, vector<ll>(2010, INF));
// costb[i][j] := i black, j white -> i+1 black
vector<vector<ll>> costb(2020, vector<ll>(2020, 0)),
    costw(2020, vector<ll>(2020, 0));

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int n;
  cin >> n;
  vector<ll> a(2 * n);
  vector<char> c(2 * n);
  for (int i = 0; i < 2 * n; i++) {
    cin >> c[i] >> a[i];
  }

  for (int j = 0; j <= n; j++) {
    int wh = 0;
    FenwickTree<int> ft(n + 10);
    for (int i = 0; i < 2 * n; i++) {
      if (c[i] == 'W') {
        if (a[i] > j) {
          wh++;
        }
      } else {
        int x = a[i];
        costb[x - 1][j] = ft.sum(x + 1, n + 1) + wh;
        ft.add(x, 1);
      }
    }
  }

  for (int i = 0; i <= n; i++) {
    int bl = 0;
    FenwickTree<int> ft(n + 10);
    for (int j = 0; j < 2 * n; j++) {
      if (c[j] == 'B') {
        if (a[j] > i) {
          bl++;
        }
      } else {
        int y = a[j];
        costw[i][y - 1] = ft.sum(y + 1, n + 1) + bl;
        ft.add(y, 1);
      }
    }
  }

  dp[0][0] = 0;
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= n; j++) {
      if (i == 0 && j == 0)
        continue;
      if (i == 0) {
        dp[i][j] = dp[i][j - 1] + costw[i][j - 1];
      } else if (j == 0) {
        dp[i][j] = dp[i - 1][j] + costb[i - 1][j];
      } else {
        dp[i][j] =
            min(dp[i][j - 1] + costw[i][j - 1], dp[i - 1][j] + costb[i - 1][j]);
      }
    }
  }
  cout << dp[n][n] << endl;
}