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 LuaJIT (2.0.4) (judge verdict: Accepted)

local mfl, mce, mmi = math.floor, math.ceil, math.min
local SegTree = {}
SegTree.updateAll = function(self)
  for i = self.stagenum - 1, 1, -1 do
    local c = #self.stage[i]
    for j = 1, c - 1 do
      self.stage[i][j] = self.stage[i + 1][j * 2 - 1] + self.stage[i + 1][j * 2]
    end
    if #self.stage[i + 1] < c * 2 then
      self.stage[i][c] = self.stage[i + 1][c * 2 - 1]
    else
      self.stage[i][c] = self.stage[i + 1][c * 2 - 1] + self.stage[i + 1][c * 2]
    end
  end
end
SegTree.create = function(self, n)
  local datasize = n
  local datacount = {}
  while 1 < datasize do
    table.insert(datacount, 1, datasize)
    datasize = mce(datasize / 2)
  end
  table.insert(datacount, 1, 1)
  self.stagenum = #datacount
  self.stage, self.size = {}, {}
  local mul = 1
  for i = 1, self.stagenum do
    self.stage[i] = {}
    for j = 1, datacount[i] do self.stage[i][j] = 0 end
    mul = mul * 2
  end
  for i = 1, self.stagenum do
    mul = mul / 2
    self.size[i] = mul
  end
  self:updateAll()
end
SegTree.getRange = function(self, left, right)
  if left == right then return self.stage[self.stagenum][left] end
  local start_stage = 1
  while right - left + 1 < self.size[start_stage] do
    start_stage = start_stage + 1
  end
  local ret = 0
  local t1, t2, t3 = {start_stage}, {left}, {right}
  while 0 < #t1 do
    local stage, l, r = t1[#t1], t2[#t1], t3[#t1]
    table.remove(t1) table.remove(t2) table.remove(t3)
    local sz = self.size[stage]
    if (l - 1) % sz ~= 0 then
      local newr = mmi(r, mce((l - 1) / sz) * sz)
      table.insert(t1, stage + 1) table.insert(t2, l) table.insert(t3, newr)
      l = newr + 1
    end
    if sz <= r + 1 - l then
      ret = ret + self.stage[stage][mce(l / sz)]
      l = l + sz
    end
    if l <= r then
      table.insert(t1, stage + 1) table.insert(t2, l) table.insert(t3, r)
    end
  end
  return ret
end
SegTree.decValue = function(self, idx)
  self.stage[self.stagenum][idx] = self.stage[self.stagenum][idx] - 1
  for i = self.stagenum - 1, 1, -1 do
    local dst = mce(idx / 2)
    if idx % 2 == 0 then
      self.stage[i][dst] = self.stage[i + 1][idx - 1] + self.stage[i + 1][idx]
    else
      if #self.stage[i + 1] == idx then
        self.stage[i][dst] = self.stage[i + 1][idx]
      else
        self.stage[i][dst] = self.stage[i + 1][idx] + self.stage[i + 1][idx + 1]
      end
    end
    idx = dst
  end
end
SegTree.inc = function(self, idx)
  self.stage[self.stagenum][idx] = 1 + self.stage[self.stagenum][idx]
end
SegTree.new = function(ary)
  local obj = {}
  setmetatable(obj, {__index = SegTree})
  obj:create(ary, func)
  return obj
end


local n, k = io.read("*n", "*n")
local sum, tmp, conv = {}, {}, {}
sum[1] = io.read("*n") - k
for i = 2, n do
  sum[i] = sum[i - 1] + io.read("*n") - k
end
sum[n + 1] = 0
for i = 1, n + 1 do tmp[i] = sum[i] end
table.sort(tmp)
local sumTypeCount = 1
local sumTypeList = {}
conv[tmp[1]] = 1
sumTypeList[1] = tmp[1]
for i = 2, n + 1 do
  if tmp[i] ~= sumTypeList[sumTypeCount] then
    sumTypeCount = sumTypeCount + 1
    conv[tmp[i]] = sumTypeCount
    sumTypeList[sumTypeCount] = tmp[i]
  end
end
local segtree = SegTree.new(sumTypeCount)
for i = 1, n do
  segtree:inc(conv[sum[i]])
end
segtree:updateAll()
local bias = 0
local cnt = 0
for i = 1, n do
  cnt = cnt + segtree:getRange(conv[bias], sumTypeCount)
  segtree:decValue(conv[sum[i]])
  bias = sum[i]
end
print(cnt)