Problem p03703 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define ALL(x) (x).begin(), (x).end()

using namespace std;

using ll = long long;
using ld = long double;

template <typename T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template <typename T> T &chmax(T &a, const T &b) { return a = max(a, b); }
template <typename T> int len(const T &x) { return x.size(); }

template <typename T> vector<T> table(int n, T v) { return vector<T>(n, v); }

template <class... Args> auto table(int n, Args... args) {
  auto val = table(args...);
  return vector<decltype(val)>(n, move(val));
}

struct yes_no : numpunct<char> {
  string_type do_truename() const { return "YES"; }
  string_type do_falsename() const { return "NO"; }
};

template <typename T> class FenwickTree {
  const int n;
  vector<T> data;

public:
  FenwickTree(int n) : n(n), data(n, 0) {}
  void add(int i, T value) {
    for (; i < n; i |= i + 1)
      data[i] += value;
  }
  T sum(int i) const {
    T res = 0;
    for (; i >= 0; i = (i & (i + 1)) - 1)
      res += data[i];
    return res;
  }
  T sum(int l, int r) const { return sum(r - 1) - sum(l - 1); }
};

void solve(ll N, ll K, vector<ll> a) {
  for (ll &i : a)
    i -= K;
  FenwickTree<ll> bit(K);
  ll sum = 0;
  vector<pair<ll, int>> vec;
  vec.emplace_back(sum, 0);
  REP(i, N) {
    sum += a[i];
    vec.emplace_back(sum, i + 1);
  }
  sort(ALL(vec));
  ll res = 0;
  for (auto p : vec) {
    int i = p.second;
    res += bit.sum(i);
    bit.add(i, 1);
  }
  cout << res << endl;
}

int main() {
  locale loc(locale(), new yes_no);
  cout << boolalpha << setprecision(12) << fixed;
  cout.imbue(loc);
  ll K;
  ll N;
  scanf("%lld", &N);
  vector<ll> a(N - 1 + 1);
  scanf("%lld", &K);
  for (int i = 0; i <= N - 1; i++) {
    scanf("%lld", &a[i]);
  }
  solve(N, K, a);
  return 0;
}


Fixed submission:
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define ALL(x) (x).begin(), (x).end()

using namespace std;

using ll = long long;
using ld = long double;

template <typename T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template <typename T> T &chmax(T &a, const T &b) { return a = max(a, b); }
template <typename T> int len(const T &x) { return x.size(); }

template <typename T> vector<T> table(int n, T v) { return vector<T>(n, v); }

template <class... Args> auto table(int n, Args... args) {
  auto val = table(args...);
  return vector<decltype(val)>(n, move(val));
}

struct yes_no : numpunct<char> {
  string_type do_truename() const { return "YES"; }
  string_type do_falsename() const { return "NO"; }
};

template <typename T> class FenwickTree {
  const int n;
  vector<T> data;

public:
  FenwickTree(int n) : n(n), data(n, 0) {}
  void add(int i, T value) {
    for (; i < n; i |= i + 1)
      data[i] += value;
  }
  T sum(int i) const {
    T res = 0;
    for (; i >= 0; i = (i & (i + 1)) - 1)
      res += data[i];
    return res;
  }
  T sum(int l, int r) const { return sum(r - 1) - sum(l - 1); }
};

void solve(ll N, ll K, vector<ll> a) {
  for (ll &i : a)
    i -= K;
  FenwickTree<ll> bit(N + 1);
  ll sum = 0;
  vector<pair<ll, int>> vec;
  vec.emplace_back(sum, 0);
  REP(i, N) {
    sum += a[i];
    vec.emplace_back(sum, i + 1);
  }
  sort(ALL(vec));
  ll res = 0;
  for (auto p : vec) {
    int i = p.second;
    res += bit.sum(i);
    bit.add(i, 1);
  }
  cout << res << endl;
}

int main() {
  locale loc(locale(), new yes_no);
  cout << boolalpha << setprecision(12) << fixed;
  cout.imbue(loc);
  ll K;
  ll N;
  scanf("%lld", &N);
  vector<ll> a(N - 1 + 1);
  scanf("%lld", &K);
  for (int i = 0; i <= N - 1; i++) {
    scanf("%lld", &a[i]);
  }
  solve(N, K, a);
  return 0;
}