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# (Mono 4.6.2.0) (judge verdict: Wrong Answer)

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
class TEST{
	static void Main(){
		Sol mySol =new Sol();
		mySol.Solve();
	}
}

class Sol{
	public void Solve(){
		
		
		long[] sum = new long[N+1];
		for(int i=0;i<N;i++){
			sum[i+1] = A[i] + sum[i];
		}
		
		HashSet<long> H = new HashSet<long>();
		for(int i=0;i<=N;i++){
			H.Add(sum[i] - i * K);
		}
		
		var L = H.ToList();
		L.Sort();
		
		var D = new Dictionary<long,int>();
		for(int i=0;i<L.Count;i++) D.Add(L[i],i);
		
		int NN = L.Count;
		int[] cnt = new int[NN];
		for(int i=1;i<=N;i++){
			cnt[D[sum[i] - i * K]]++;
		}
		BIT sumCnt = new BIT(NN+1);
		for(int i=0;i<NN;i++){
//Console.WriteLine("   " + i);
			sumCnt.Add(i+1,cnt[i]);
		}
		
		
		long ans = 0;
		
		for(int i=0;i<N;i++){
			long offset = sum[i] - i * K;
			int l = 0, r = NN;
			while(r - l > 1){
				int c = (l+r) / 2;
				if(L[c] < offset){
					l = c;
				} else {
					r = c;
				}
			}
			ans += (N-i) - sumCnt.Sum(l+1);
			sumCnt.Add(D[offset]+1,-1);
		}
		Console.WriteLine(ans);
	}
	int N;
	long K;
	long[] A;
	public Sol(){
		var d = ria();
		N = d[0]; K = d[1];
		A = new long[N];
		for(int i=0;i<N;i++) A[i] = rl();
	}

	static String rs(){return Console.ReadLine();}
	static int ri(){return int.Parse(Console.ReadLine());}
	static long rl(){return long.Parse(Console.ReadLine());}
	static double rd(){return double.Parse(Console.ReadLine());}
	static String[] rsa(char sep=' '){return Console.ReadLine().Split(sep);}
	static int[] ria(char sep=' '){return Array.ConvertAll(Console.ReadLine().Split(sep),e=>int.Parse(e));}
	static long[] rla(char sep=' '){return Array.ConvertAll(Console.ReadLine().Split(sep),e=>long.Parse(e));}
	static double[] rda(char sep=' '){return Array.ConvertAll(Console.ReadLine().Split(sep),e=>double.Parse(e));}
}
class BIT{
	int MM;
	int n;
	long[] bit;
	public BIT(int n_){
		n=n_;
		MM=1<<18;
		while(MM<n*2)MM<<=1;
		bit=new long[MM+1];
	}
	//1-origin!!
	public void Add(int i,int x){
		while(i<=n){
			bit[i]+=x;
			i+=(i&-i);
		}
	}
	
	// i=>sum(a_1,...,a_i)
	// i=0 -> return 0;
	public long Sum(int i){
		long s=0;
		while(i>0){
			s+=bit[i];
			i-= (i&-i);
		}
		return s;
	}
}