Problem p03703 (C++)

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

#define forn(i, n) for (llong i = 0ll; i < (llong)n; ++i)
#define fornn(i, l, r) for (llong i = (llong)l; i < (llong)r; ++i)
#define size(x) ((int)(x.size()))

using namespace std;

typedef long long llong;
const llong inf = (llong)1e+9 + 7ll;
const llong linf = (llong)1e+18 + 7ll;
const long double eps = (long double)1e-9;
const long double pi = acosl((long double)-1.0);
const int alph = 26;

mt19937 mrand(random_device{}());

template <typename T, typename U> inline llong umin(const T &a, const U &b) {
  return a < b ? a : b;
}
template <typename T, typename U> inline llong umax(const T &a, const U &b) {
  return a > b ? a : b;
}

static char buff[(int)2e6 + 17]; // reads std::string
const int maxn = (int)1e5 + 17;

int n, k;
llong ans = 0;
llong a[maxn];

bool read() {
  if (scanf("%d %d", &n, &k) != 2)
    return false;

  forn(i, n) scanf("%lld", a + i);

  return true;
}

void calc(int l, int r) {
  if (l == r) {
    ans += a[l] >= 0;
    return;
  }

  int m = (l + r) / 2;
  calc(l, m);
  calc(m + 1, r);
  vector<llong> ps;
  llong cur = 0;

  for (int i = m; i >= l; --i) {
    cur += a[i];
    ps.push_back(cur);
  }

  cur = 0;
  sort(ps.begin(), ps.end());

  for (int i = m + 1; i <= r; ++i) {
    cur += a[i];
    int cnt = lower_bound(ps.begin(), ps.end(), -cur) - ps.begin();
    ans += size(ps) - cnt;
  }
}

void solve() {
  forn(i, n) a[i] -= k;

  calc(0, n - 1);
  printf("%lld\n", ans);
}

int main() {
#if SEREZHKA
  freopen("file.in", "r", stdin);
#endif

  while (read())
    solve();

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

#define forn(i, n) for (llong i = 0ll; i < (llong)n; ++i)
#define fornn(i, l, r) for (llong i = (llong)l; i < (llong)r; ++i)
#define size(x) ((int)(x.size()))

using namespace std;

typedef long long llong;
const llong inf = (llong)1e+9 + 7ll;
const llong linf = (llong)1e+18 + 7ll;
const long double eps = (long double)1e-9;
const long double pi = acosl((long double)-1.0);
const int alph = 26;

mt19937 mrand(random_device{}());

template <typename T, typename U> inline llong umin(const T &a, const U &b) {
  return a < b ? a : b;
}
template <typename T, typename U> inline llong umax(const T &a, const U &b) {
  return a > b ? a : b;
}

static char buff[(int)2e6 + 17]; // reads std::string
const int maxn = (int)2e5 + 17;

int n, k;
llong ans = 0;
llong a[maxn];

bool read() {
  if (scanf("%d %d", &n, &k) != 2)
    return false;

  forn(i, n) scanf("%lld", a + i);

  return true;
}

void calc(int l, int r) {
  if (l == r) {
    ans += a[l] >= 0;
    return;
  }

  int m = (l + r) / 2;
  calc(l, m);
  calc(m + 1, r);
  vector<llong> ps;
  llong cur = 0;

  for (int i = m; i >= l; --i) {
    cur += a[i];
    ps.push_back(cur);
  }

  cur = 0;
  sort(ps.begin(), ps.end());

  for (int i = m + 1; i <= r; ++i) {
    cur += a[i];
    int cnt = lower_bound(ps.begin(), ps.end(), -cur) - ps.begin();
    ans += size(ps) - cnt;
  }
}

void solve() {
  forn(i, n) a[i] -= k;

  calc(0, n - 1);
  printf("%lld\n", ans);
}

int main() {
#if SEREZHKA
  freopen("file.in", "r", stdin);
#endif

  while (read())
    solve();

  return 0;
}