Problem:
Score : 600 points

Problem Statement

You are given an integer sequence of length N, a = {a_1, a_2, …, a_N}, and an integer K.

a has N(N+1)/2 non-empty contiguous subsequences, {a_l, a_{l+1}, …, a_r} (1 ≤ l ≤ r ≤ N). Among them, how many have an arithmetic mean that is greater than or equal to K?

Constraints

All input values are integers.

1 ≤ N ≤ 2 \times 10^5

1 ≤ K ≤ 10^9

1 ≤ a_i ≤ 10^9

Input

Input is given from Standard Input in the following format:

N K
a_1
a_2
:
a_N

Output

Print the number of the non-empty contiguous subsequences with an arithmetic mean that is greater than or equal to K.

Sample Input 1

3 6
7
5
7

Sample Output 1

5

All the non-empty contiguous subsequences of a are listed below:

{a_1} = {7}

{a_1, a_2} = {7, 5}

{a_1, a_2, a_3} = {7, 5, 7}

{a_2} = {5}

{a_2, a_3} = {5, 7}

{a_3} = {7}

Their means are 7, 6, 19/3, 5, 6 and 7, respectively, and five among them are 6 or greater. Note that {a_1} and {a_3} are indistinguishable by the values of their elements, but we count them individually.

Sample Input 2

1 2
1

Sample Output 2

0

Sample Input 3

7 26
10
20
30
40
30
20
10

Sample Output 3

13

Solution in C++ (GCC 9.2.1) (judge verdict: Accepted)

// Standard Stuff
#include <bits/stdc++.h>
using namespace std;

// Macros
#define F  first
#define S  second
#define PB push_back

// Rename Datatypes
using ll  = long long;
using ld  = long double;
using pii = pair<int,int>;

// Common Functions
ll fastpow(ll a, ll ex){ ll res = 1; while(ex){ if(ex & 1) res *= a; a *= a; ex >>= 1; } return res; }
ll fastpow(ll a, ll ex, ll m){ a %= m; ll res = 1; while(ex){ if(ex & 1) res = (a*res) % m; a = (a*a) % m; ex >>= 1; } return res; }
ll modinverse(ll a, ll m){ ll m0 = m, y = 0, x = 1; while (a > 1){ ll q = a / m; a %= m; swap(a,m); x -= q*y; swap(x,y); } if(x < 0) x += m0; return x; }

// HELPERS
//  Tedious Grid Movements
vector<pii> moves_grid = { {0,1} , {0,-1} , {1,0} , {-1,0} };
inline bool move_okay(int x, int y, int n, int m) { return 0 <= x and x < n and 0 <= y and y < m; }
//  Remember:
//   * vec.assign() GOOD; vec.resize() BAD
//   * comparator uses <

// Output, Common Containers
template <typename T> ostream& operator << (ostream& os, const vector<T>& v) { for (T i : v) os << i << ' '; return os; }
template <typename T> ostream& operator << (ostream& os, const set<T>& v)    { for (T i : v) os << i << ' '; return os; }
template <typename T, typename S> ostream& operator << (ostream& os, const pair<T,S>& v) { os << "(" << v.F << "=>" << v.S << ") "; return os; }
template <typename T, typename S> ostream& operator << (ostream& os, const map<T,S>& v)  { for (auto i : v) os << i << ' '; return os; }

// Debugging
#ifdef SANYAM1101
	#define trace(args...) do{ cerr << "TRACE ON! (Line: " << __LINE__ << ")\n"; auto _stream = istringstream{string{#args}}; d_f(_stream, args); } while(0)
	void d_f(__attribute__((unused)) istringstream& _stream) {}
	template<typename Head, typename... Tails> void d_f(istringstream& _stream, Head H, Tails... T) { string _name; _stream >> _name; if(_name.back() == ',') _name.pop_back(); cerr << "\t" << _name << " <=> [" << H << "]\n"; d_f(_stream, T...); }
#else
	#define trace(args...)
#endif


// CODE STARTS HERE

// 0-indexed
template<typename T>
class FenwickTree {
	private:

		const int n;
		vector<T> bits;
		const int log_val;

	public:

		FenwickTree(int N) : n(N), bits(N+1, 0), log_val(__lg(N)) {}

		FenwickTree(vector<T>& arr) : FenwickTree(arr.size()) {
			for(int i = 0; i < n; ++i)
				add(i, arr[i]);
		}

		void add(int idx, T delta){
			for (++idx; idx <= n; idx += (idx & -idx))
				bits[idx] += delta;
		}

		T sum(int r){
			T ret = 0;
			for (++r; r > 0; r -= (r & -r))
				ret += bits[r];
			return ret;
		}

		T sum(int l, int r){
			return sum(r) - sum(l - 1);
		}

		int find_kth_by_order(T val){
			/*
			 * Returns lowest i st sum(0,i) >= val.
			 * Note that if no such value exists, it returns n, which is outside the legal positions of the array.
			 */

			int pos = 0;

			for(int i = log_val; i >= 0; --i){
				int next_pos = pos + (1 << i);

				if(next_pos <= n and bits[next_pos] < val){
					pos = next_pos;
					val -= bits[next_pos];
				}
			}

			return pos;
		}

		friend ostream& operator << (ostream& os, FenwickTree<T>& ft){
			os << "N:" << ft.n << " ==> " << ft.bits;
			return os; 
		}
};

void solve(){
	int n; ll k;
	cin >> n >> k;

	vector<int> arr(n);
	for(auto& i: arr) cin >> i;

	// count of (i,j) st \Sigma_{k = arr[i..j]} / (j - i + 1) >= k
	//                st presum(j) - presum(i - 1) >= k * j - k * (i - 1)
	//                st presum(j) - k * j >= presum(i - 1) - k * (i - 1)

	vector<ll> vec(1, 0);
	for(auto i: arr) vec.push_back(i + vec.back());
	for(int i = 0; i <= n; ++i) vec[i] -= k * i;

	// ans = count inversions in vec !

	ll cou = 0;

	// COMPRESS
	// sort
	set<ll> s(vec.begin(), vec.end());
	//find ordering
	map<ll,int> m;
	int no = 0; for(auto element: s) m[element] = no++;
	// transform vec
	for(auto& i: vec) i = m[i];

	FenwickTree<int> fen{no};
	fen.add(vec.front(), 1);

	for(int i = 1; i <= n; ++i){
		cou += fen.sum(vec[i]);
		fen.add(vec[i], 1);
	}

	cout << cou << "\n";
}

signed main (void){
	// Fast_IO
	ios::sync_with_stdio(false);
	cin.tie(nullptr);

	// Set Fixed Notation (default sucks), High Precision for Floating-Point Output
	cout << fixed << setprecision(20);

	// Multiple TCs
	int tt = 1;
	// cin >> tt;
	while(tt--) solve();
}