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 Java8 (OpenJDK 1.8.0) (judge verdict: Wrong Answer)

import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;

class Main {
	public static void main(String[] args) {
		new Main().run();
	}

	void run() {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int k = sc.nextInt();
		int[][] a = new int[n][2];
		int add = 0;
		for (int i = 0; i < n; ++i) {
			a[i][0] = sc.nextInt() - k;
			a[i][1] = i;
			if (i > 0) {
				a[i][0] += a[i - 1][0];
			}
			if (a[i][0] >= 0) {
				++add;
			}
		}
		Arrays.sort(a, new Comparator<int[]>() {
			@Override
			public int compare(int[] o1, int[] o2) {
				return Integer.compare(o1[0], o2[0]);
			}
		});

		int[] arr = new int[n];
		int p = 0;
		for (int i = 0; i < n; ++i) {
			if (i > 0) {
				if (a[i][0] != a[i - 1][0]) {
					++p;
				}
			}
			arr[a[i][1]] = p;
		}
		solve(n, arr, add);
	}

	void solve(int n, int[] arr, int add) {
		int[][] e = new int[n][2];
		for (int i = 0; i < n; ++i) {
			e[i][0] = arr[i];
			e[i][1] = i;
		}
		Arrays.sort(e, new Comparator<int[]>() {
			@Override
			public int compare(int[] o1, int[] o2) {
				if (o1[0] != o2[0]) {
					return Integer.compare(o1[0], o2[0]);
				} else {
					return Integer.compare(o1[1], o2[1]);
				}
			}
		});
		Seg seg = new Seg(n);
		int ans = 0;
		for (int i = 0; i < n; ++i) {
			ans += seg.sum(0, e[i][1]);
			seg.set(e[i][1]);
		}
		System.out.println(ans + add);
	}

	class Seg {
		int n;
		int[] v;

		public Seg(int n) {
			this.n = 1;
			while (this.n < n) {
				this.n *= 2;
			}
			v = new int[2 * this.n - 1];
		}

		void set(int k) {
			k += n - 1;
			v[k] = 1;
			while (k > 0) {
				k = (k - 1) / 2;
				v[k] = v[2 * k + 1] + v[2 * k + 2];
			}
		}

		int sum(int a, int b) {
			return sum(a, b, 0, n, 0);
		}

		// [a,b),[l,r)
		int sum(int a, int b, int l, int r, int k) {
			if (a >= r || b <= l) {
				return 0;
			} else if (a <= l && r <= b) {
				return v[k];
			} else {
				int vl = sum(a, b, l, (l + r) / 2, 2 * k + 1);
				int vr = sum(a, b, (l + r) / 2, r, 2 * k + 2);
				return vl + vr;
			}
		}
	}

	static void tr(Object... objects) {
		System.out.println(Arrays.deepToString(objects));
	}
}