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 Rust (1.15.1) (judge verdict: Time Limit Exceeded)

#[allow(unused_imports)]
use std::cmp::{max, min};
#[allow(unused_imports)]
use std::collections::{HashMap, HashSet};

mod util {
    use std::io::stdin;
    use std::str::FromStr;
    use std::fmt::Debug;

    #[allow(dead_code)]
    pub fn line() -> String {
        let mut line: String = String::new();
        stdin().read_line(&mut line).unwrap();
        line.trim().to_string()
    }

    #[allow(dead_code)]
    pub fn get<T: FromStr>() -> T
        where <T as FromStr>::Err: Debug
    {
        let mut line: String = String::new();
        stdin().read_line(&mut line).unwrap();
        line.trim().parse().unwrap()
    }

    #[allow(dead_code)]
    pub fn gets<T: FromStr>() -> Vec<T>
        where <T as FromStr>::Err: Debug
    {
        let mut line: String = String::new();
        stdin().read_line(&mut line).unwrap();
        line.split_whitespace()
            .map(|t| t.parse().unwrap())
            .collect()
    }
}

fn main() {
    let nk: Vec<i64> = util::gets();

    let c: Vec<i64> = (0..nk[0])
        .map(|_| util::get::<i64>() - nk[1])
        .fold((Vec::new(), 0), |(mut v, acc), a| {
            v.push(acc + a);
            (v, acc + a)
        })
        .0;

    let d: Vec<usize> = {
        let uniq: HashSet<_> = c.iter().cloned().collect();
        let mut uniq_sorted: Vec<_> = uniq.into_iter().collect();
        uniq_sorted.sort();

        c.iter()
            .map(|x| uniq_sorted.binary_search(x).unwrap())
            .collect()
    };

    let n = d.len();
    let mut bit = vec![HashMap::new(); n + 1];

    for (i, &x) in (1..).zip(d.iter()) {
        let mut i = i as isize;
        while i as usize <= n {
            let update = bit[i as usize].get(&x).unwrap_or(&0) + 1;
            bit[i as usize].insert(x, update);
            i += i & -i;
        }
    }

    let mut ans = c.iter().filter(|&&x| x >= 0).count();

    for (i, x) in (0..).zip(d.iter()) {
        let mut i = i as isize;
        while i > 0 {
            for (k, &v) in &bit[i as usize] {
                if k <= x {
                    ans += v;
                }
            }
            i -= i & -i;
        }
    }

    println!("{}", ans);
}