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 Kotlin (1.0.0) (judge verdict: Runtime Error)

import java.util.*
import java.util.NoSuchElementException
import java.io.IOException
import java.io.PrintWriter
import java.util.PriorityQueue
import java.util.Arrays
import java.util.ArrayList


var sc = FastScanner()
var pw = PrintWriter(System.out)
var st = SegTree<Int>(1 shl 17, 0, fun(l:Int, r:Int) = l + r)

fun main(args: Array<String>) {
    var N = sc.nextInt()
    var K = sc.nextInt()
    var A = Array<Int>(N, {i -> sc.nextInt() - K})

    var sm : MutableList<Long> = mutableListOf()
    var set : HashSet<Long> = hashSetOf()
    for (i in 0 until N) {
        var x:Long = A[i].toLong()
        if(0 < i) x += A[i]
        sm.add(x)
        set.add(x)
    }
    var dic : MutableMap<Long,Int> = mutableMapOf()
    set.forEachIndexed({
        index, l -> dic[l] = index
    })

    var ans:Long = 0
    for(i in 0 until N) {
        var idx:Int = dic[sm[i]]!!
        if(0 < idx) ans  += st.get(0, idx - 1)
        var x = st.get(idx, idx)
        st.update(idx, x + 1)
        if(0 <= A[i]) ans++
    }

    println(ans)
}

class SegTree<T>(_N :Int, _def:T, _comp:(l:T, r:T)->T) {
    var N = _N
    var vec:MutableList<T> = mutableListOf()

    val def:T = _def
    var comp = _comp

    init { for(i in 0 until 2*N) vec.add(def) }

    fun get(_l:Int, _r:Int) :T{
        if(_l > _r) return def
        var l = _l + N
        var r = _r + N + 1
        var ret : T = def
        while(l < r) {
            if(l and 1 != 0) ret = comp(ret, vec[l++])
            if(r and 1 != 0) ret = comp(ret, vec[--r])
            l /= 2
            r /= 2
        }
        return ret
    }

    fun update(_i:Int, v: T) {
        var i = _i + N
        vec[i] = v
        while(i  > 1) {
            i = i shr 1
            vec[i] = comp(vec[i * 2], vec[i * 2 + 1])
        }
    }

}














class FastScanner {
    private val `in` = System.`in`
    private val buffer = ByteArray(1024)
    private var ptr = 0
    private var bufferLength = 0

    private fun hasNextByte(): Boolean {
        if (ptr < bufferLength) {
            return true
        } else {
            ptr = 0
            try {
                bufferLength = `in`.read(buffer)
            } catch (e: IOException) {
                e.printStackTrace()
            }

            if (bufferLength <= 0) {
                return false
            }
        }
        return true
    }

    private fun readByte(): Int {
        if (hasNextByte())
            return buffer[ptr++].toInt()
        else
            return -1
    }

    private fun isPrintableChar(c: Int): Boolean {
        return 33 <= c && c <= 126
    }

    private fun skipUnprintable() {
        while (hasNextByte() && !isPrintableChar(buffer[ptr].toInt())) ptr++
    }

    internal operator fun hasNext(): Boolean {
        skipUnprintable()
        return hasNextByte()
    }

    operator fun next(): String {
        if (!hasNext()) throw NoSuchElementException()
        val sb = StringBuilder()
        var b = readByte()
        while (isPrintableChar(b)) {
            sb.appendCodePoint(b)
            b = readByte()
        }
        return sb.toString()
    }

    internal fun nextLong(): Long {
        if (!hasNext()) throw NoSuchElementException()
        var n: Long = 0
        var minus = false
        var b = readByte()
        if (b == '-'.toInt()) {
            minus = true
            b = readByte()
        }
        if (b < '0'.toInt() || '9'.toInt() < b) {
            throw NumberFormatException()
        }
        while (true) {
            if ('0'.toInt() <= b && b <= '9'.toInt()) {
                n *= 10
                n += (b - '0'.toInt()).toLong()
            } else if (b == -1 || !isPrintableChar(b)) {
                return if (minus) -n else n
            } else {
                throw NumberFormatException()
            }
            b = readByte()
        }
    }

    internal fun nextDouble(): Double {
        return java.lang.Double.parseDouble(next())
    }

    internal fun nextDoubleArray(n: Int): DoubleArray {
        val array = DoubleArray(n)
        for (i in 0..n - 1) {
            array[i] = nextDouble()
        }
        return array
    }

    internal fun nextDoubleMap(n: Int, m: Int): Array<DoubleArray?> {
        val map = arrayOfNulls<DoubleArray?>(n)
        for (i in 0..n - 1) {
            map[i] = nextDoubleArray(m)
        }
        return map
    }

    fun nextInt(): Int {
        return nextLong().toInt()
    }

    fun nextIntArray(n: Int): IntArray {
        val array = IntArray(n)
        for (i in 0..n - 1) {
            array[i] = nextInt()
        }
        return array
    }
}