Problem p03703 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

#define INF_LL (int64)1e18
#define INF (int32)1e9
#define REP(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define all(x) x.begin(), x.end()
#define fs first
#define sc second

using int32 = int_fast32_t;
using uint32 = uint_fast32_t;
using int64 = int_fast64_t;
using uint64 = uint_fast64_t;
using PII = pair<int32, int32>;
using PLL = pair<int64, int64>;

const double eps = 1e-10;

template <typename A, typename B> inline void chmin(A &a, B b) {
  if (a > b)
    a = b;
}
template <typename A, typename B> inline void chmax(A &a, B b) {
  if (a < b)
    a = b;
}

const int64 mod = 1e9 + 7;

class BIT {
private:
  vector<int64> bit;
  int32 n;

public:
  BIT() {}
  BIT(int32 n) : n(n + 1) { bit.resize(this->n, 0); }

  void add(int32 k, int32 x) {
    k++;
    while (k <= n) {
      bit[k] += x;
      k += k & -k;
    }
  }

  int64 sum(int32 k) {
    k++;
    int64 res = 0;
    while (k) {
      res += bit[k];
      k -= k & -k;
    }
    return res;
  }
};

int main(void) {
  int32 n;
  int64 K;
  vector<int64> a;
  cin >> n >> K;
  a.resize(n);
  REP(i, n) {
    cin >> a[i];
    a[i] -= K;
    if (i)
      a[i] += a[i - 1];
  }
  vector<int64> b = a;
  b.push_back(0);
  sort(all(b));
  b.erase(unique(all(b)), b.end());
  map<int64, int64> zip, unzip;
  REP(i, b.size()) {
    zip[b[i]] = i;
    unzip[i] = b[i];
  }
  BIT bit(n);
  bit.add(zip[0], 1);
  int64 res = 0;
  REP(i, n) {
    res += bit.sum(zip[a[i]]);
    bit.add(zip[a[i]], 1);
  }
  cout << res << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

#define INF_LL (int64)1e18
#define INF (int32)1e9
#define REP(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define all(x) x.begin(), x.end()
#define fs first
#define sc second

using int32 = int_fast32_t;
using uint32 = uint_fast32_t;
using int64 = int_fast64_t;
using uint64 = uint_fast64_t;
using PII = pair<int32, int32>;
using PLL = pair<int64, int64>;

const double eps = 1e-10;

template <typename A, typename B> inline void chmin(A &a, B b) {
  if (a > b)
    a = b;
}
template <typename A, typename B> inline void chmax(A &a, B b) {
  if (a < b)
    a = b;
}

const int64 mod = 1e9 + 7;

class BIT {
private:
  vector<int64> bit;
  int32 n;

public:
  BIT() {}
  BIT(int32 n) : n(n + 1) { bit.resize(this->n, 0); }

  void add(int32 k, int32 x) {
    k++;
    while (k <= n) {
      bit[k] += x;
      k += k & -k;
    }
  }

  int64 sum(int32 k) {
    k++;
    int64 res = 0;
    while (k) {
      res += bit[k];
      k -= k & -k;
    }
    return res;
  }
};

int main(void) {
  int32 n;
  int64 K;
  vector<int64> a;
  cin >> n >> K;
  a.resize(n);
  REP(i, n) {
    cin >> a[i];
    a[i] -= K;
    if (i)
      a[i] += a[i - 1];
  }
  vector<int64> b = a;
  b.push_back(0);
  sort(all(b));
  b.erase(unique(all(b)), b.end());
  map<int64, int64> zip, unzip;
  REP(i, b.size()) {
    zip[b[i]] = i;
    unzip[i] = b[i];
  }
  BIT bit(n + 1);
  bit.add(zip[0], 1);
  int64 res = 0;
  REP(i, n) {
    res += bit.sum(zip[a[i]]);
    bit.add(zip[a[i]], 1);
  }
  cout << res << endl;
}