Problem p03357 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define lsb(x) (x & (-x))
#define ll long long
#define ull unsigned long long
// 217
// 44

using namespace std;

const int MAXN = 2000;

pair<char, int> v[MAXN + 1];

int dp[2 * MAXN + 1][MAXN + 1];

int aib[2 * MAXN + 1];
int pos[2][MAXN + 1];
int n;

inline void update(int pos, int val) {
  for (int i = pos; i <= 2 * n; i += lsb(i))
    aib[i] += val;
}

inline int query(int pos) {
  int ans = 0;
  for (int i = pos; i > 0; i -= lsb(i))
    ans += aib[i];
  return ans;
}

int main() {
  // ifstream cin("A.in");
  // ofstream cout("A.out");
  int i, j;
  ios::sync_with_stdio(false);
  cin >> n;
  for (i = 1; i <= 2 * n; i++) {
    cin >> v[i].first >> v[i].second;
    if (v[i].first == 'B')
      pos[0][v[i].second] = i;
    else
      pos[1][v[i].second] = i;
  }
  for (int len = 1; len <= 2 * n; len++) {
    for (i = 1; i <= 2 * n; i++) {
      aib[i] = 0;
    }
    for (i = 1; i <= len; i++) {
      if (i <= n)
        update(pos[1][i], 1);
      else
        update(pos[0][i - n], 1);
      dp[len][i] = 1e9;
    }
    for (i = max(0, len - n); i <= min(len, n); i++) {
      int val = 1e9;
      if (i > 0) {
        val = dp[len - 1][i - 1] + pos[0][i] - len + query(2 * n) -
              query(pos[0][i]);
        if (i > len - n)
          update(pos[0][i], 1);
      }
      if (i < len) {
        update(pos[1][len - i], -1);
        val = min(val, dp[len - 1][i] + pos[1][len - i] - len + query(2 * n) -
                           query(pos[1][len - i]));
      }
      dp[len][i] = val;
    }
  }
  cout << dp[2 * n][n];
  // cin.close();
  // cout.close();
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define lsb(x) (x & (-x))
#define ll long long
#define ull unsigned long long
// 217
// 44

using namespace std;

const int MAXN = 2000;

pair<char, int> v[2 * MAXN + 1];

int dp[2 * MAXN + 1][MAXN + 1];

int aib[2 * MAXN + 1];
int pos[2][MAXN + 1];
int n;

inline void update(int pos, int val) {
  for (int i = pos; i <= 2 * n; i += lsb(i))
    aib[i] += val;
}

inline int query(int pos) {
  int ans = 0;
  for (int i = pos; i > 0; i -= lsb(i))
    ans += aib[i];
  return ans;
}

int main() {
  // ifstream cin("A.in");
  // ofstream cout("A.out");
  int i, j;
  ios::sync_with_stdio(false);
  cin >> n;
  for (i = 1; i <= 2 * n; i++) {
    cin >> v[i].first >> v[i].second;
    if (v[i].first == 'B')
      pos[0][v[i].second] = i;
    else
      pos[1][v[i].second] = i;
  }
  for (int len = 1; len <= 2 * n; len++) {
    for (i = 1; i <= 2 * n; i++) {
      aib[i] = 0;
    }
    for (i = 1; i <= len; i++) {
      if (i <= n)
        update(pos[1][i], 1);
      else
        update(pos[0][i - n], 1);
      dp[len][i] = 1e9;
    }
    for (i = max(0, len - n); i <= min(len, n); i++) {
      int val = 1e9;
      if (i > 0) {
        val = dp[len - 1][i - 1] + pos[0][i] - len + query(2 * n) -
              query(pos[0][i]);
        if (i > len - n)
          update(pos[0][i], 1);
      }
      if (i < len) {
        update(pos[1][len - i], -1);
        val = min(val, dp[len - 1][i] + pos[1][len - i] - len + query(2 * n) -
                           query(pos[1][len - i]));
      }
      dp[len][i] = val;
    }
  }
  cout << dp[2 * n][n];
  // cin.close();
  // cout.close();
  return 0;
}