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: Accepted)

macro_rules ! input { ( source = $ s : expr , $ ( $ r : tt ) * ) => { let mut iter = $ s . split_whitespace ( ) ; input_inner ! { iter , $ ( $ r ) * } } ; ( iter = $ iter : ident , $ ( $ r : tt ) * ) => { let s = { use std :: io :: Read ; let mut s = String :: new ( ) ; std :: io :: stdin ( ) . read_to_string ( & mut s ) . unwrap ( ) ; s } ; let mut $ iter = s . split_whitespace ( ) ; input_inner ! { $ iter , $ ( $ r ) * } } ; ( $ ( $ r : tt ) * ) => { let s = { use std :: io :: Read ; let mut s = String :: new ( ) ; std :: io :: stdin ( ) . read_to_string ( & mut s ) . unwrap ( ) ; s } ; let mut iter = s . split_whitespace ( ) ; input_inner ! { iter , $ ( $ r ) * } } ; }
macro_rules ! input_inner { ( $ iter : expr ) => { } ; ( $ iter : expr , ) => { } ; ( $ iter : expr , mut $ var : ident : $ t : tt $ ( $ r : tt ) * ) => { let mut $ var = read_value ! ( $ iter , $ t ) ; input_inner ! { $ iter $ ( $ r ) * } } ; ( $ iter : expr , mut $ var : ident $ ( $ r : tt ) * ) => { input_inner ! { $ iter , mut $ var : usize $ ( $ r ) * } } ; ( $ iter : expr , $ var : ident : $ t : tt $ ( $ r : tt ) * ) => { let $ var = read_value ! ( $ iter , $ t ) ; input_inner ! { $ iter $ ( $ r ) * } } ; ( $ iter : expr , $ var : ident $ ( $ r : tt ) * ) => { input_inner ! { $ iter , $ var : usize $ ( $ r ) * } } ; }
macro_rules ! read_value { ( $ iter : expr , ( $ ( $ t : tt ) ,* ) ) => { ( $ ( read_value ! ( $ iter , $ t ) ) ,* ) } ; ( $ iter : expr , [ $ t : tt ; $ len : expr ] ) => { ( 0 ..$ len ) . map ( | _ | read_value ! ( $ iter , $ t ) ) . collect ::< Vec < _ >> ( ) } ; ( $ iter : expr , { chars : $ base : expr } ) => { read_value ! ( $ iter , String ) . chars ( ) . map ( | c | ( c as u8 - $ base as u8 ) as usize ) . collect ::< Vec < usize >> ( ) } ; ( $ iter : expr , { char : $ base : expr } ) => { read_value ! ( $ iter , { chars : $ base } ) [ 0 ] } ; ( $ iter : expr , chars ) => { read_value ! ( $ iter , String ) . chars ( ) . collect ::< Vec < char >> ( ) } ; ( $ iter : expr , char ) => { read_value ! ( $ iter , chars ) [ 0 ] } ; ( $ iter : expr , usize1 ) => { read_value ! ( $ iter , usize ) - 1 } ; ( $ iter : expr , $ t : ty ) => { $ iter . next ( ) . unwrap ( ) . parse ::<$ t > ( ) . unwrap ( ) } ; }
pub mod at {
    // use std;
    #[inline]
    fn rmb(k: usize) -> usize {
        (k as isize & -(k as isize)) as usize
    }
    pub trait Monoid {
        type T: Clone + PartialEq;
        fn mempty(&self) -> Self::T;
        fn mappend(&self, x: &Self::T, y: &Self::T) -> Self::T;
    }
    pub struct BinaryIndexedTree<M: Monoid> {
        bit: Vec<M::T>,
        m: M,
    }
    impl<M: Monoid> BinaryIndexedTree<M> {
        pub fn new(n: usize, m: M) -> BinaryIndexedTree<M> {
            let bit = vec![m.mempty(); n + 1];
            BinaryIndexedTree { bit: bit, m: m }
        }
        #[doc = " 1-indexed"]
        pub fn query(&self, k: usize) -> M::T {
            let mut res = self.m.mempty();
            let mut k = k;
            while k > 0 {
                res = self.m.mappend(&res, &self.bit[k]);
                k -= rmb(k);
            }
            res
        }
        #[doc = " 1-indexed"]
        pub fn update(&mut self, k: usize, x: M::T) {
            let mut k = k;
            while k < self.bit.len() {
                self.bit[k] = self.m.mappend(&self.bit[k], &x);
                k += rmb(k);
            }
        }
    }
    pub struct AnyMonoid<T: Clone, F: Fn(&T, &T) -> T> {
        id: T,
        op: F,
    }
    impl<T: Clone, F: Fn(&T, &T) -> T> AnyMonoid<T, F> {
        pub fn new(id: T, op: F) -> AnyMonoid<T, F> {
            AnyMonoid { id: id, op: op }
        }
    }
    impl<T: Clone + PartialEq, F: Fn(&T, &T) -> T> Monoid for AnyMonoid<T, F> {
        type T = T;
        fn mempty(&self) -> T {
            self.id.clone()
        }
        fn mappend(&self, x: &T, y: &T) -> T {
            (self.op)(x, y)
        }
    }
    pub trait Bisect: Copy {
        fn halve(self, other: Self) -> Self;
        fn section_end(self, other: Self) -> bool;
    }
    impl Bisect for usize {
        fn halve(self, other: Self) -> Self {
            if self > other {
                other + (self - other) / 2
            } else {
                self + (other - self) / 2
            }
        }
        fn section_end(self, other: Self) -> bool {
            (if self > other {
                self - other
            } else {
                other - self
            }) <= 1
        }
    }
    impl Bisect for i64 {
        fn halve(self, other: Self) -> Self {
            (self + other) / 2
        }
        fn section_end(self, other: Self) -> bool {
            (self - other).abs() <= 1
        }
    }
    impl Bisect for f64 {
        fn halve(self, other: Self) -> Self {
            (self + other) / 2.
        }
        fn section_end(self, other: Self) -> bool {
            (self - other).abs() <= 1e-8
        }
    }
    pub fn binary_search<T: Bisect, F: Fn(T) -> bool>(f: F, ok: T, err: T) -> T {
        let mut ok = ok;
        let mut err = err;
        while !ok.section_end(err) {
            let m = ok.halve(err);
            if f(m) {
                ok = m;
            } else {
                err = m;
            }
        }
        ok
    }
    pub fn lower_bound<T: Bisect + Ord>(v: &[T], x: T) -> usize {
        binary_search(|i| v[i as usize] >= x, v.len() as i64, -1) as usize
    }
}
use at::*;
use std::collections::BTreeSet;
fn main() {
    input! { n, k: i64, a: [i64; n] };
    let mut s = vec![0; n + 1];
    for i in 0..n {
        s[i + 1] = s[i] + a[i];
    }
    for i in 0..n + 1 {
        s[i] -= k * i as i64;
    }
    let v = s
        .iter()
        .cloned()
        .collect::<BTreeSet<_>>()
        .into_iter()
        .collect::<Vec<_>>();
    let mut bit = BinaryIndexedTree::new(n + 1, AnyMonoid::new(0i64, |&x, &y| x + y));
    let mut ans = 0;
    for i in 0..n {
        let x = lower_bound(&v, s[i]);
        bit.update(x + 1, 1);
        let y = lower_bound(&v, s[i + 1]);
        ans += bit.query(y + 1);
    }
    println!("{}", ans);
}