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 Haskell (GHC 7.10.3) (judge verdict: Accepted)

{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE UnboxedTuples #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE ViewPatterns #-}
{-# LANGUAGE TupleSections #-}
{-# LANGUAGE MultiWayIf #-}

import Control.Monad
import Control.Monad.ST
import Control.Monad.State
import Control.Monad.Primitive
import Control.Applicative
import Control.Arrow
import Data.List
import Data.Array
import Data.Array.IO
import Data.Array.ST
import Data.IORef
import Data.STRef
import Data.Maybe
import Data.Bits
import Data.Ord
import Data.Ratio
import Data.Int
import Data.Char
import qualified Data.IntMap as IM
import qualified Data.ByteString.Char8 as BS
import qualified Data.Set as Set
import qualified Data.Vector as V
import qualified Data.Vector.Mutable as VM
import qualified Data.Vector.Unboxed as VU
import qualified Data.Vector.Unboxed.Mutable as VUM
import qualified Data.Vector.Generic as VG
import qualified Data.Vector.Generic.Mutable as VGM
import qualified Data.Sequence as Sq
import Data.Tuple
import Data.Function
import Text.Printf
import Debug.Trace
import Numeric

modifyArray arr i f = readArray arr i >>= \x -> writeArray arr i (f x)

listToTuple2 [a,b] = (a,b)
listToTuple3 [a,b,c] = (a,b,c)
listToTuple4 [a,b,c,d] = (a,b,c,d)
listToTuple5 [a,b,c,d,e] = (a,b,c,d,e)

readIntBS = fst . fromJust . BS.readInt

for = flip map

infixr 1 ?
(?) :: Bool -> (a,a) -> a
(?) b t = (if b then fst else snd) t


main = do
  [n,k] <- map read . words <$> getLine :: IO [Int]
  xs <- scanl (+) 0 . map ((+ (-k)) . readIntBS) . BS.lines <$> BS.getContents :: IO [Int]

  let
    ys = IM.fromList . (flip zip) [1..] . map head . group . sort $ xs
    zs = map (\i -> ys IM.! i) xs

  seg <- newFenwickTree (length zs) 0 (+) :: IO (IOUFenwickTree Int)

  print =<< sum <$> (forM zs $ \z -> queryFenwickTree seg z >>= \q -> modifyFenwickTree seg z (+1) >> return q)
    

type MFenwickTree v s a = (v s a, a, (a -> a -> a), Int) -- (フェニック木, 単位元, 二項演算)
type STFenwickTree s a = MFenwickTree VM.MVector s a
type IOFenwickTree a = STFenwickTree RealWorld a
type STUFenwickTree s a = MFenwickTree VUM.MVector s a
type IOUFenwickTree a = STUFenwickTree RealWorld a

newFenwickTree :: (Applicative m, PrimMonad m, VGM.MVector v a) => Int -> a -> (a -> a -> a) -> m (MFenwickTree v (PrimState m) a)
newFenwickTree n e f = (,e,f,n) <$> VGM.replicate (n+1) e

modifyFenwickTree :: (Applicative m, PrimMonad m, VGM.MVector v a) => MFenwickTree v (PrimState m) a -> Int -> (a -> a) -> m ()
modifyFenwickTree (tree,e,f,size) i g = do
  aux i
  where
    aux j = do
      when (j <= size) $ do
        VGM.write tree j =<< g <$> VGM.read tree j
        aux (j + (j .&. (-j)))

queryFenwickTree :: (Applicative m, PrimMonad m, VGM.MVector v a) => MFenwickTree v (PrimState m) a -> Int -> m a
queryFenwickTree (tree,e,f,size) i = do
  aux i
  where
    aux j
      | j <= 0 = return e
      | otherwise = f <$> VGM.read tree j <*> aux (j - (j .&. (-j)))

lowerBoundFenwickTree :: (Num a, Ord a, Eq a, Applicative m, PrimMonad m, VGM.MVector v a) => MFenwickTree v (PrimState m) a -> a -> m Int
lowerBoundFenwickTree (tree,e,f,size) x = do
  aux k 0 x
  where
    k = (2 ^ (floor $ logBase 2 (fromIntegral size)))
    aux i j w
      | i == 0 = return (j+1)
      | otherwise = do
          if i+j <= size then do
            a <- VGM.read tree (i+j)
            if a < w then aux (i `div` 2) (i+j) (w-a) else aux (i `div` 2) j w
            else
            aux (i `div` 2) j w