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 5.4.1) (judge verdict: Wrong Answer)


//#include "pch.h"
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
//#include <set>
//#include <map>
//#include <iostream>

#define ll long long
#define fri(n) for(i=0;i<(n);i++)
#define frj(n) for(j=0;j<(n);i++)
#define min(p,q) ((p)<(q)?(p):(q))
#define max(p,q) ((p)>(q)?(p):(q))
#define INF 1000000000000//10^12

static int bit[200010];
int bitlen=200000;//使う長さをここに入れる

void bit_init(void){
	int i;
	for(i=0;i<=bitlen;i++){
		bit[i]=0;
	}
}

int bitsum(int i){
	int s=0;
	while(i>0){
		s=s+bit[i];
		i=i-(i&-i);
	}
	return s;
}

//add BITのi番目の値をx増やす
void bitadd(int i,int x){
	while(i<=bitlen){
		bit[i]=bit[i]+x;
		i=i+(i&-i);
	}
}

struct num_t {
	int idnum;
	int num;
} ;

int compare_num(const void *a, const void *b) {
	struct num_t *A = (struct num_t *)a;
	struct num_t *B = (struct num_t *)b;
	return A->num - B->num;
}

int compare_idnum(const void *a, const void *b) {
	struct num_t *A = (struct num_t *)a;
	struct num_t *B = (struct num_t *)b;
	return A->idnum - B->idnum;
}

int main(void)
{
	//変数の宣言
	int n,k;
	static int a[200010];
	static struct num_t ruiseki[200010];

	//よく使う変数
	int i,j,l;
	int flag=0;
	ll int ans=0;
	int count=0;
	int temp,temp1,temp2;
	int max,min;
	int len;
	int sum=0;


	//データの読み込み
	scanf("%d %d",&n,&k);
//	scanf_s("%d %d",&n,&k);

	for(i=0;i<n;i++){
		scanf("%d",&a[i]);
//		scanf_s("%d",&a[i]);
	}

	//	printf("nは%dです\n", n);
	//	printf("データの読み込み終了\n");
	//実際の処理

	fri(n){
		a[i]=a[i]-k;
		if(i==0){
			ruiseki[i].num=a[i];
			ruiseki[i].idnum=i;
		}else{
			ruiseki[i].num=ruiseki[i-1].num+a[i];
			ruiseki[i].idnum=i;
		}
		if(ruiseki[i].num>=0) ans++;
	}

//	for(i=0;i<n;i++){
//		printf("ruiseki[%d].num=%d\n",i,ruiseki[i].num);
//	}

	qsort(ruiseki, n, sizeof(struct num_t), compare_num);

	count=1;
	for(i=0;i<n;i++){
		if(0==i){
			temp=ruiseki[0].num;
			ruiseki[0].num=1;
		}else{
			if(ruiseki[i].num==temp){
				ruiseki[i].num=count;
			}else{
				temp=ruiseki[i].num;
				count++;
				ruiseki[i].num=count;
			}
		}
	}

	qsort(ruiseki, n, sizeof(struct num_t), compare_idnum);

//	for(i=0;i<n;i++){
//		printf("ruiseki[%d].num=%d\n",i,ruiseki[i].num);
//	}

	bitlen=n;
	bit_init();

	for(i=0;i<n;i++){
		ans=ans+bitsum(ruiseki[i].num);
		bitadd(ruiseki[i].num,1);
	}

	//	printf("計算部分終了\n");

	//出力

	printf("%d",ans);

	//	printf("結果の出力終了\n");


	return 0;
}