Problem p03357 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
#define ll long long
#define pii pair<int, int>
#define ull unsigned long long
#define size(v) int((v).size())
#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#define rep(i, n) for (int i = 0; i < int(n); i++)
template <typename T> inline void read(T &a) {
  register int b = 1, c = getchar();
  a = 0;
  for (; !isdigit(c); c = getchar())
    if (c == 45)
      b *= -1;
  for (; isdigit(c); c = getchar())
    a = (a << 3) + (a << 1) + (c ^ 48);
  a *= b;
}
// Template end

const int maxn = 2005;
const int INF = 1000000007;
char c[maxn];
int n, a[maxn], tp[maxn], pos[maxn][2], dp[maxn][maxn];

struct BIT {
  int arr[maxn];
  void init() { rep(i, maxn) arr[i] = 0; }
  void add(int x, int v) {
    while (x <= n * 2) {
      arr[x] += v;
      x += x & -x;
    }
  }
  int sum(int x) {
    int ans = 0;
    while (x) {
      ans += arr[x];
      x -= x & -x;
    }
    return ans;
  }
};
BIT bit;

inline void chkmin(int &a, int b) {
  if (a > b)
    a = b;
}

int main() {
  // Work start
  read(n);
  memset(pos, 0, sizeof pos);
  for (int i = 1; i <= n * 2; i++) {
    scanf("%s", c);
    tp[i] = (c[0] == 'B');
    read(a[i]);
    pos[a[i]][tp[i]] = i;
  }
  rep(i, n + 1) rep(j, n + 1) dp[i][j] = INF;
  dp[0][0] = 0;
  rep(i, n + 1) {
    bit.init();
    for (int j = 1; j <= i; j++)
      bit.add(pos[j][0], 1);
    rep(j, n + 1) {
      if (j)
        bit.add(pos[j][1], 1);
      if (i < n) {
        chkmin(dp[i + 1][j],
               dp[i][j] + pos[i + 1][0] - 1 - bit.sum(pos[i + 1][0]));
      }
      if (j < n) {
        chkmin(dp[i][j + 1],
               dp[i][j] + pos[j + 1][1] - 1 - bit.sum(pos[j + 1][1]));
      }
    }
  }
  printf("%d\n", dp[n][n]);
#ifdef DEBUG
  printf("\nRunning Time : %ldms", clock());
#endif
  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
#define ll long long
#define pii pair<int, int>
#define ull unsigned long long
#define size(v) int((v).size())
#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#define rep(i, n) for (int i = 0; i < int(n); i++)
template <typename T> inline void read(T &a) {
  register int b = 1, c = getchar();
  a = 0;
  for (; !isdigit(c); c = getchar())
    if (c == 45)
      b *= -1;
  for (; isdigit(c); c = getchar())
    a = (a << 3) + (a << 1) + (c ^ 48);
  a *= b;
}
// Template end

const int maxn = 4005;
const int INF = 1000000007;
char c[maxn];
int n, a[maxn], tp[maxn], pos[maxn][2], dp[maxn][maxn];

struct BIT {
  int arr[maxn];
  void init() { rep(i, maxn) arr[i] = 0; }
  void add(int x, int v) {
    while (x <= n * 2) {
      arr[x] += v;
      x += x & -x;
    }
  }
  int sum(int x) {
    int ans = 0;
    while (x) {
      ans += arr[x];
      x -= x & -x;
    }
    return ans;
  }
};
BIT bit;

inline void chkmin(int &a, int b) {
  if (a > b)
    a = b;
}

int main() {
  // Work start
  read(n);
  memset(pos, 0, sizeof pos);
  for (int i = 1; i <= n * 2; i++) {
    scanf("%s", c);
    tp[i] = (c[0] == 'B');
    read(a[i]);
    pos[a[i]][tp[i]] = i;
  }
  rep(i, n + 1) rep(j, n + 1) dp[i][j] = INF;
  dp[0][0] = 0;
  rep(i, n + 1) {
    bit.init();
    for (int j = 1; j <= i; j++)
      bit.add(pos[j][0], 1);
    rep(j, n + 1) {
      if (j)
        bit.add(pos[j][1], 1);
      if (i < n) {
        chkmin(dp[i + 1][j],
               dp[i][j] + pos[i + 1][0] - 1 - bit.sum(pos[i + 1][0]));
      }
      if (j < n) {
        chkmin(dp[i][j + 1],
               dp[i][j] + pos[j + 1][1] - 1 - bit.sum(pos[j + 1][1]));
      }
    }
  }
  printf("%d\n", dp[n][n]);
#ifdef DEBUG
  printf("\nRunning Time : %ldms", clock());
#endif
  return 0;
}