Problem p03357 (C++)

Buggy submission (status: Runtime Error):
// C:\Program
// Files(x86)\Dev-Cpp\MinGW64\lib\gcc\x86_64-w64-mingw32\4.9.2\include\c++\x86_64-w64-mingw32\bits\stdc++.h
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string>
#include <time.h>
#include <utility>
#include <vector>

using namespace std;

const int INF = 1e9 + 7;

int n;
int a[2005], b[2005];
int dp[2005][2005];

struct Binary_Indexed_Tree {
  int sum[4005];
  void init() { memset(sum, 0, sizeof(sum)); }
  void add(int x) {
    while (x <= 2 * n) {
      //			printf("%d\n",x);
      sum[x] += 1;
      x += x & (-x);
    }
    return;
  }
  int query(int x) {
    int res = 0;
    while (x) {
      //			printf("%d\n",x);
      res += sum[x];
      x -= x & (-x);
    }
    return res;
  }
  void out() {
    for (int i = 1; i <= 2 * n; i++)
      printf("%d ", sum[i]);
    puts("");
    return;
  }
} BIT;

int main() {
  cin >> n;
  for (int i = 1; i <= n * 2; i++) {
    char c;
    int d;
    cin >> c >> d;
    if (c == 'W')
      a[d] = i;
    else
      b[d] = i;
  }
  //	for(int i=1;i<=n;i++){
  //		printf("%d ",a[i]);
  //	}
  //	puts("");
  //	for(int i=1;i<=n;i++){
  //		printf("%d ",b[i]);
  //	}
  //	puts("");
  for (int i = 0; i <= n * 2; i++) {
    for (int j = 0; j <= n * 2; j++)
      dp[i][j] = INF;
  }
  dp[0][0] = 0;
  for (int i = 0; i <= n; i++) {
    BIT.init();
    for (int j = 1; j <= i; j++)
      BIT.add(a[j]);
    for (int j = 0; j <= n; j++) {
      if (j)
        BIT.add(b[j]);
      //			printf("i=%d j=%d\n",i,j);
      //			BIT.out();
      if (i < n) {
        //				printf("i=%d j=%d dp[i][j]=%d res=%d
        //dp[i+1][j]=%d\n",i,j,dp[i][j],(a[i+1]-1-BIT.query(a[i+1]-1)),dp[i+1][j]);
        dp[i + 1][j] = min(dp[i + 1][j],
                           dp[i][j] + (a[i + 1] - 1 - BIT.query(a[i + 1] - 1)));
      }
      if (j < n) {
        //				printf("i=%d j=%d dp[i][j]=%d res=%d
        //dp[i][j+1]=%d\n",i,j,dp[i][j],(b[j+1]-1-BIT.query(b[j+1]-1)),dp[i][j+1]);
        dp[i][j + 1] = min(dp[i][j + 1],
                           dp[i][j] + (b[j + 1] - 1 - BIT.query(b[j + 1] - 1)));
      }
    }
  }
  /*	for(int i=0;i<=n;i++){
                  for(int j=0;j<=n;j++) printf("%d ",dp[i][j]);
                  puts("");
          }
  */
  printf("%d\n", dp[n][n]);
  return 0;
}


Fixed submission:
// C:\Program
// Files(x86)\Dev-Cpp\MinGW64\lib\gcc\x86_64-w64-mingw32\4.9.2\include\c++\x86_64-w64-mingw32\bits\stdc++.h
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string>
#include <time.h>
#include <utility>
#include <vector>

using namespace std;

const int INF = 1e9 + 7;

int n;
int a[4005], b[4005];
int dp[4005][4005];

struct Binary_Indexed_Tree {
  int sum[4005];
  void init() { memset(sum, 0, sizeof(sum)); }
  void add(int x) {
    while (x <= 2 * n) {
      //			printf("%d\n",x);
      sum[x] += 1;
      x += x & (-x);
    }
    return;
  }
  int query(int x) {
    int res = 0;
    while (x) {
      //			printf("%d\n",x);
      res += sum[x];
      x -= x & (-x);
    }
    return res;
  }
  void out() {
    for (int i = 1; i <= 2 * n; i++)
      printf("%d ", sum[i]);
    puts("");
    return;
  }
} BIT;

int main() {
  cin >> n;
  for (int i = 1; i <= n * 2; i++) {
    char c;
    int d;
    cin >> c >> d;
    if (c == 'W')
      a[d] = i;
    else
      b[d] = i;
  }
  //	for(int i=1;i<=n;i++){
  //		printf("%d ",a[i]);
  //	}
  //	puts("");
  //	for(int i=1;i<=n;i++){
  //		printf("%d ",b[i]);
  //	}
  //	puts("");
  for (int i = 0; i <= n * 2; i++) {
    for (int j = 0; j <= n * 2; j++)
      dp[i][j] = INF;
  }
  dp[0][0] = 0;
  for (int i = 0; i <= n; i++) {
    BIT.init();
    for (int j = 1; j <= i; j++)
      BIT.add(a[j]);
    for (int j = 0; j <= n; j++) {
      if (j)
        BIT.add(b[j]);
      //			printf("i=%d j=%d\n",i,j);
      //			BIT.out();
      if (i < n) {
        //				printf("i=%d j=%d dp[i][j]=%d res=%d
        //dp[i+1][j]=%d\n",i,j,dp[i][j],(a[i+1]-1-BIT.query(a[i+1]-1)),dp[i+1][j]);
        dp[i + 1][j] = min(dp[i + 1][j],
                           dp[i][j] + (a[i + 1] - 1 - BIT.query(a[i + 1] - 1)));
      }
      if (j < n) {
        //				printf("i=%d j=%d dp[i][j]=%d res=%d
        //dp[i][j+1]=%d\n",i,j,dp[i][j],(b[j+1]-1-BIT.query(b[j+1]-1)),dp[i][j+1]);
        dp[i][j + 1] = min(dp[i][j + 1],
                           dp[i][j] + (b[j + 1] - 1 - BIT.query(b[j + 1] - 1)));
      }
    }
  }
  /*	for(int i=0;i<=n;i++){
                  for(int j=0;j<=n;j++) printf("%d ",dp[i][j]);
                  puts("");
          }
  */
  printf("%d\n", dp[n][n]);
  return 0;
}