Language: C++

#include <algorithm>
#include <iostream>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<ll, int> P;

template<typename Monoid>
struct SegmentTree {
    using F = function<Monoid(Monoid, Monoid)>;

    int sz;
    vector<Monoid> seg;

    const F f;
    const Monoid M1;

    SegmentTree(int n, const F f, const Monoid &M1) : f(f), M1(M1) {
        sz = 1;
        while(sz < n) sz <<= 1;
        seg.assign(2 * sz, M1);
    }

    void set(int k, const Monoid &x) {
        seg[k + sz] = x;
    }

    void build() {
        for(int k = sz - 1; k > 0; k--) {
            seg[k] = f(seg[2 * k + 0], seg[2 * k + 1]);
        }
    }

    void update(int k, const Monoid &x) {
        k += sz;
        seg[k] = x;
        while(k >>= 1) {
            seg[k] = f(seg[2 * k + 0], seg[2 * k + 1]);
        }
    }

    Monoid query(int a, int b) {
        Monoid L = M1, R = M1;
        for(a += sz, b += sz; a < b; a >>= 1, b >>= 1) {
            if(a & 1) L = f(L, seg[a++]);
            if(b & 1) R = f(seg[--b], R);
        }
        return f(L, R);
    }

    Monoid operator[](const int &k) const {
        return seg[k + sz];
    }
};

int main()
{
    ll n, k;
    cin >> n >> k;
    ll a[200004];
    ll s[200004];
    P p[200004];
    ll ans = 0;
    for(int i = 0; i < n; i++){
        cin >> a[i];
        a[i] -= k;
        s[i] = a[i];
        if(i > 0) s[i] += s[i - 1];
        if(s[i] >= 0) ans++;
        p[i] = P(s[i], i);
    }
    sort(p, p + n);
    int nth[200004];
    for(int i = 0; i < n; i++) nth[p[i].second] = i;
    SegmentTree<ll> seg(n, [](ll a, ll b){return a + b;}, (ll)0);
    for(int i = 0; i < n; i++) seg.set(i, (ll)0);
    seg.build();
    for(int i = n - 1; i >= 0; i--){
        int insert = lower_bound(p, p + n, P(s[i], -1)) - p;
        ans += seg.query(insert, n);
        seg.update(nth[i], 1);
    }
    cout << ans << endl;
}

Compiler output:
a.cc:11:15: error: 'function' does not name a type; did you mean 'union'?
   11 |     using F = function<Monoid(Monoid, Monoid)>;
      |               ^~~~~~~~
      |               union
a.cc:16:11: error: 'F' does not name a type
   16 |     const F f;
      |           ^
a.cc:19:30: error: 'F' does not name a type
   19 |     SegmentTree(int n, const F f, const Monoid &M1) : f(f), M1(M1) {
      |                              ^
a.cc: In constructor 'SegmentTree<Monoid>::SegmentTree(int, int, const Monoid&)':
a.cc:19:55: error: class 'SegmentTree<Monoid>' does not have any field named 'f'
   19 |     SegmentTree(int n, const F f, const Monoid &M1) : f(f), M1(M1) {
      |                                                       ^
a.cc: In function 'int main()':
a.cc:76:28: error: invalid user-defined conversion from 'main()::<lambda(ll, ll)>' to 'int' [-fpermissive]
   76 |     SegmentTree<ll> seg(n, [](ll a, ll b){return a + b;}, (ll)0);
      |                            ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
a.cc:76:28: note: candidate is: 'constexpr main()::<lambda(ll, ll)>::operator long long int (*)(ll, ll)() const' (near match)
   76 |     SegmentTree<ll> seg(n, [](ll a, ll b){return a + b;}, (ll)0);
      |                            ^
a.cc:76:28: note:   no known conversion from 'long long int (*)(ll, ll)' {aka 'long long int (*)(long long int, long long int)'} to 'int'
a.cc:19:32: note:   initializing argument 2 of 'SegmentTree<Monoid>::SegmentTree(int, int, const Monoid&) [with Monoid = long long int]'
   19 |     SegmentTree(int n, const F f, const Monoid &M1) : f(f), M1(M1) {
      |                        ~~~~~~~~^
a.cc: In instantiation of 'void SegmentTree<Monoid>::build() [with Monoid = long long int]':
a.cc:78:14:   required from here
   78 |     seg.build();
      |     ~~~~~~~~~^~
a.cc:31:23: error: 'f' was not declared in this scope
   31 |             seg[k] = f(seg[2 * k + 0], seg[2 * k + 1]);
      |                      ~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
a.cc: In instantiation of 'Monoid SegmentTree<Monoid>::query(int, int) [with Monoid = long long int]':
a.cc:81:25:   required from here
   81 |         ans += seg.query(insert, n);
      |                ~~~~~~~~~^~~~~~~~~~~
a.cc:46:28: error: 'f' was not declared in this scope
   46 |             if(a & 1) L = f(L, seg[a++]);
      |                           ~^~~~~~~~~~~~~
a.cc:47:28: error: 'f' was not declared in this scope
   47 |             if(b & 1) R = f(seg[--b], R);
      |                           ~^~~~~~~~~~~~~
a.cc:49:17: error: 'f' was not declared in this scope
   49 |         return f(L, R);
      |                ~^~~~~~
a.cc: In instantiation of 'void SegmentTree<Monoid>::update(int, const Monoid&) [with Monoid = long long int]':
a.cc:82:19:   required from here
   82 |         seg.update(nth[i], 1);
      |         ~~~~~~~~~~^~~~~~~~~~~
a.cc:39:23: error: 'f' was not declared in this scope
   39 |             seg[k] = f(seg[2 * k + 0], seg[2 * k + 1]);
      |                      ~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~