Problem p03703 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define pb push_back
#define pf push_front
typedef long long lint;
typedef complex<double> P;
#define mp make_pair
#define fi first
#define se second
typedef pair<lint, int> pint;
#define All(s) s.begin(), s.end()
#define rAll(s) s.rbegin(), s.rend()
#define REP(i, a, b) for (int i = a; i < b; i++)
#define rep(i, n) REP(i, 0, n)
template <class typ> struct BIT {
  vector<typ> x;
  BIT(int n) : x(n, 0) {}
  typ sum(int a, int b) {
    if (a == 0) {
      typ s = 0;
      for (int i = b; i >= 0; i = (i & (i + 1)) - 1)
        s += x[i];
      return s;
    } else
      return sum(0, b) - sum(0, a - 1);
  }
  void add(int ind, typ f) {
    for (int i = ind; i < x.size(); i |= i + 1)
      x[i] += f;
  }
};
lint a[100100], s[100100];
int pos[100100];
vector<pint> v;
int main() {
  int n;
  lint k, out = 0;
  cin >> n >> k;
  rep(i, n) cin >> a[i];
  // sort(a,a+n);
  s[0] = 0;
  rep(i, n) {
    s[i + 1] = s[i] + a[i];
    s[i + 1] -= k;
    v.pb(mp(s[i + 1], i + 1));
  }
  // rep(i,n+1) cout<<s[i]<<endl;
  sort(All(v));
  rep(i, n) pos[v[i].se] = i; // cout<<pos[2]<<endl;
  BIT<int> bit(n + 10);
  for (int i = n - 1; i >= 0; i--) {
    bit.add(pos[i + 1], 1);
    int t = lower_bound(All(v), mp(s[i], 0)) - v.begin();
    // cout<<t<<' '<<bit.sum(t,n+5)<<endl;
    out += bit.sum(t, n + 5);
  }
  cout << out << endl;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define pb push_back
#define pf push_front
typedef long long lint;
typedef complex<double> P;
#define mp make_pair
#define fi first
#define se second
typedef pair<lint, int> pint;
#define All(s) s.begin(), s.end()
#define rAll(s) s.rbegin(), s.rend()
#define REP(i, a, b) for (int i = a; i < b; i++)
#define rep(i, n) REP(i, 0, n)
template <class typ> struct BIT {
  vector<typ> x;
  BIT(int n) : x(n, 0) {}
  typ sum(int a, int b) {
    if (a == 0) {
      typ s = 0;
      for (int i = b; i >= 0; i = (i & (i + 1)) - 1)
        s += x[i];
      return s;
    } else
      return sum(0, b) - sum(0, a - 1);
  }
  void add(int ind, typ f) {
    for (int i = ind; i < x.size(); i |= i + 1)
      x[i] += f;
  }
};
lint a[200100], s[200100];
int pos[200100];
vector<pint> v;
int main() {
  int n;
  lint k, out = 0;
  cin >> n >> k;
  rep(i, n) cin >> a[i];
  // sort(a,a+n);
  s[0] = 0;
  rep(i, n) {
    s[i + 1] = s[i] + a[i];
    s[i + 1] -= k;
    v.pb(mp(s[i + 1], i + 1));
  }
  // rep(i,n+1) cout<<s[i]<<endl;
  sort(All(v));
  rep(i, n) pos[v[i].se] = i; // cout<<pos[2]<<endl;
  BIT<int> bit(n + 10);
  for (int i = n - 1; i >= 0; i--) {
    bit.add(pos[i + 1], 1);
    int t = lower_bound(All(v), mp(s[i], 0)) - v.begin();
    // cout<<t<<' '<<bit.sum(t,n+5)<<endl;
    out += bit.sum(t, n + 5);
  }
  cout << out << endl;
}