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: Wrong Answer)

#![allow(unused_imports)]
use std::io;
use std::cmp::*;
use std::collections::*;
#[allow(dead_code)]
const M: i64 = 1e9 as i64 + 7;

fn read_line() -> String {
    let stdin = io::stdin();
    let mut buf = String::new();
    stdin.read_line(&mut buf).unwrap();
    buf
}
#[allow(dead_code)]
fn read<T>() -> T where T: std::str::FromStr, T::Err : std::fmt::Debug {
    read_line().trim().parse().unwrap()
}
#[allow(dead_code)]
fn readv<T>() -> Vec<T> where T: std::str::FromStr, T::Err : std::fmt::Debug {
    read_line().split(' ').map(|a| a.trim().parse().unwrap()).collect()
}

struct SegNode {
    lc: Option<Box<SegNode>>,
    rc: Option<Box<SegNode>>,
    beg: usize,
    lst: usize,
    value: i64,
}
impl SegNode {
    fn new(b: usize, e: usize) -> SegNode {
        let piv = (b + e) / 2;
        let (lc, rc) = if b == piv {
            (None, None)
        } else {
            (Some(Box::new(SegNode::new(b, piv))), Some(Box::new(SegNode::new(piv, e))))
        };
        SegNode {
            lc: lc,
            rc: rc,
            beg: b,
            lst: e - 1,
            value: 0,
        }
    }
    fn get(&self, l: usize, r: usize) -> i64 {
        if l <= self.beg && self.lst <= r{
            #[cfg(local)] println!("{}: {:?}", "(self.beg, self.lst, l, r)", (self.beg, self.lst, l, r, self.value));
            self.value
        } else if r < self.beg || self.lst < l {
            0
        } else if let (&Some(ref lc), &Some(ref rc)) = (&self.lc, &self.rc) {
            lc.get(l, r) + rc.get(l, r)
        } else {
            //panic
            -1
        }
    }
    fn add(&mut self, l: usize, r: usize, num: i64) {
        if l <= self.beg && self.lst <= r{
            self.value += num;
            #[cfg(local)] println!("{}: {:?}", "num", (self.beg, self.lst, l, r, num, self.value));
        } else if r < self.beg || self.lst < l {
        } else if let (&mut Some(ref mut lc), &mut Some(ref mut rc)) = (&mut self.lc, &mut self.rc) {
            self.value += num * 1;
            lc.add(l, r, num);
            rc.add(l, r, num);
        }
    }
}
struct SegTree {
    root: SegNode
}
impl SegTree {
    fn new(size: usize) -> SegTree {
        SegTree {
            root: SegNode::new(0, size),
        }
    }
    fn get(&self, l: usize, r: usize) -> i64 {
        self.root.get(l, r)
    }
    fn add(&mut self, l: usize, r: usize, num: i64) {
        self.root.add(l, r, num);
    }
}

fn main() {
    let input = readv::<i64>();
    let (n, k) = (input[0] as usize, input[1]);
    let mut aa: VecDeque<_> = (0..n).map(|_| read::<i64>() - k).collect();
    aa.push_front(0);
    // let aa: Vec<_> = (0..n).map(|_| read::<i64>() - k).collect();
    let mut cnt = 0;
    let accs: Vec<_> = aa.into_iter().map(|a| { cnt += a; cnt }).collect();
    #[cfg(local)] println!("{}: {:?}", "accs", accs);
    let map = accs.iter().collect::<BTreeSet<_>>()
        .into_iter().enumerate().map(|(i, num)| (num, i)).collect::<HashMap<_, _>>();
    let seq = accs.iter().map(|i| map[i]).collect::<VecDeque<_>>();
    // seq.push_front(0);
    #[cfg(local)] println!("{}: {:?}", "seq", seq);
    let mut ans = 0i64;
    let mut st = SegTree::new(map.len());
    for i in seq.into_iter().rev() {
        ans += st.get(i, n - 1);
        st.add(i, i, 1);
        #[cfg(local)] println!("{}: {:?}", "(i, ans)", (i, ans));
    }
    println!("{}", ans);
}