Problem p03357 (C++)

Buggy submission (status: Runtime Error):

#include <algorithm>
#include <bitset>
#include <cctype>
#include <climits>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <typeinfo>
#include <utility>
#include <vector>
using namespace std;
using ll = long long;
template <typename T>
using min_priority_queue = priority_queue<T, vector<T>, greater<T>>;
const ll inf = 1145141919810364364LL;
#define all(x) (x).begin(), (x).end()
#define puts(x) cout << x << endl
#define rep(i, m, n) for (ll i = m; i < n; ++i)
#define pb push_back
#define fore(i, a) for (auto &i : a)
#define rrep(i, m, n) for (ll i = m; i >= n; --i)

template <typename T> struct BIT {
  int n;
  vector<T> dat;
  BIT(int n = 0) { initialize(n); }
  void initialize(int temp) {
    n = temp;
    dat.resize(n);
    rep(i, 0, n) dat[i] = 0;
  }
  T sum(int i) {
    T s = 0;
    while (i >= 0) {
      s += dat[i];
      i = (i & (i + 1)) - 1;
    }
    return s;
  }
  T sum_between(int i, int j) {
    if (i > j)
      return 0;
    return sum(j) - sum(i - 1);
  }
  void plus(int i, T x) {
    while (i < n) {
      dat[i] += x;
      i |= i + 1;
    }
  }
  int lower_bound(T x) {
    if (sum(n - 1) < x)
      return -1;
    else if (sum(0) >= x)
      return 0;
    int ng = 0, ok = n - 1;
    while (ok - ng > 1) {
      int mid = (ok + ng) / 2;
      if (sum(mid) >= x)
        ok = mid;
      else
        ng = mid;
    }
    return ok;
  }
};
ll dp[2020][2020];
vector<pair<char, int>> v(4040);
map<pair<char, int>, int> mp;
int cntw[2020][2020], cntb[2020][2020];

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

  int N;
  cin >> N;
  for (int i = 0; i < 2 * N; i++) {
    cin >> v[i].first >> v[i].second;
    mp[{v[i].first, v[i].second}] = i;
  }
  for (int i = 0; i < 2020; i++) {
    for (int j = 0; j < 2020; j++) {
      dp[i][j] = inf;
    }
  }

  BIT<ll> bitw(2020), bitb(2020);
  for (int i = 2 * N - 1; i >= 0; i--) {
    for (int j = 1; j <= N; j++) {
      cntw[i][j] = bitw.sum_between(0, j);
      cntb[i][j] = bitb.sum_between(0, j);
    }
    if (v[i].first == 'W')
      bitw.plus(v[i].second, 1);
    else
      bitb.plus(v[i].second, 1);
  }

  dp[0][0] = 0;
  for (int i = 0; i <= N; i++) {
    for (int j = 0; j <= N; j++) {
      ll cost1 = cntw[mp[{'W', i + 1}]][i] + cntb[mp[{'W', i + 1}]][j];
      ll cost2 = cntw[mp[{'B', j + 1}]][i] + cntb[mp[{'B', j + 1}]][j];
      dp[i + 1][j] = min(dp[i + 1][j], dp[i][j] + cost1);
      dp[i][j + 1] = min(dp[i][j + 1], dp[i][j] + cost2);
    }
  }
  puts(dp[N][N]);

  return 0;
}

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cctype>
#include <climits>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <typeinfo>
#include <utility>
#include <vector>
using namespace std;
using ll = long long;
template <typename T>
using min_priority_queue = priority_queue<T, vector<T>, greater<T>>;
const ll inf = 1145141919810364364LL;
#define all(x) (x).begin(), (x).end()
#define puts(x) cout << x << endl
#define rep(i, m, n) for (ll i = m; i < n; ++i)
#define pb push_back
#define fore(i, a) for (auto &i : a)
#define rrep(i, m, n) for (ll i = m; i >= n; --i)

template <typename T> struct BIT {
  int n;
  vector<T> dat;
  BIT(int n = 0) { initialize(n); }
  void initialize(int temp) {
    n = temp;
    dat.resize(n);
    rep(i, 0, n) dat[i] = 0;
  }
  T sum(int i) {
    T s = 0;
    while (i >= 0) {
      s += dat[i];
      i = (i & (i + 1)) - 1;
    }
    return s;
  }
  T sum_between(int i, int j) {
    if (i > j)
      return 0;
    return sum(j) - sum(i - 1);
  }
  void plus(int i, T x) {
    while (i < n) {
      dat[i] += x;
      i |= i + 1;
    }
  }
  int lower_bound(T x) {
    if (sum(n - 1) < x)
      return -1;
    else if (sum(0) >= x)
      return 0;
    int ng = 0, ok = n - 1;
    while (ok - ng > 1) {
      int mid = (ok + ng) / 2;
      if (sum(mid) >= x)
        ok = mid;
      else
        ng = mid;
    }
    return ok;
  }
};
ll dp[2020][2020];
vector<pair<char, int>> v(4040);
map<pair<char, int>, int> mp;
int cntw[4040][2020], cntb[4040][2020];

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

  int N;
  cin >> N;
  for (int i = 0; i < 2 * N; i++) {
    cin >> v[i].first >> v[i].second;
    mp[{v[i].first, v[i].second}] = i;
  }
  for (int i = 0; i < 2020; i++) {
    for (int j = 0; j < 2020; j++) {
      dp[i][j] = inf;
    }
  }

  BIT<ll> bitw(2020), bitb(2020);
  for (int i = 2 * N - 1; i >= 0; i--) {
    for (int j = 1; j <= N; j++) {
      cntw[i][j] = bitw.sum_between(0, j);
      cntb[i][j] = bitb.sum_between(0, j);
    }
    if (v[i].first == 'W')
      bitw.plus(v[i].second, 1);
    else
      bitb.plus(v[i].second, 1);
  }

  dp[0][0] = 0;
  for (int i = 0; i <= N; i++) {
    for (int j = 0; j <= N; j++) {
      ll cost1 = cntw[mp[{'W', i + 1}]][i] + cntb[mp[{'W', i + 1}]][j];
      ll cost2 = cntw[mp[{'B', j + 1}]][i] + cntb[mp[{'B', j + 1}]][j];
      dp[i + 1][j] = min(dp[i + 1][j], dp[i][j] + cost1);
      dp[i][j + 1] = min(dp[i][j + 1], dp[i][j] + cost2);
    }
  }
  puts(dp[N][N]);

  return 0;
}