Problem:
Score : 400 points

Problem Statement

One day, Snuke was given a permutation of length N, a_1, a_2, ..., a_N, from his friend.

Find the following:

Constraints

1 ≦ N ≦ 200,000

(a_1, a_2, ..., a_N) is a permutation of (1, 2, ..., N).

Input

The input is given from Standard Input in the following format:

N
a_1 a_2 ... a_N

Output

Print the answer.

Note that the answer may not fit into a 32-bit integer.

Sample Input 1

3
2 1 3

Sample Output 1

9

Sample Input 2

4
1 3 2 4

Sample Output 2

19

Sample Input 3

8
5 4 8 1 2 6 7 3

Sample Output 3

85

Solution in LuaJIT (2.0.4) (judge verdict: Accepted)

local mfl, mce, mmi = math.floor, math.ceil, math.min
local SegTree = {}
SegTree.create = function(self, ary, func, emptyvalue)
  self.n, self.func, self.emptyvalue = #ary, func, emptyvalue
  local stagenum, mul = 1, 1
  self.cnt, self.stage, self.size = {1}, {{}}, {}
  while mul < self.n do
    mul, stagenum = mul * 2, stagenum + 1
    self.cnt[stagenum], self.stage[stagenum] = mul, {}
  end
  for i = 1, stagenum do
    self.size[i] = self.cnt[stagenum + 1 - i]
  end
  self.stagenum = stagenum
  for i = 1, self.n do
    self.stage[stagenum][i] = ary[i]
  end
  for i = self.n + 1, mul do
    self.stage[stagenum][i] = emptyvalue
  end
  for i = stagenum - 1, 1, -1 do
    for j = 1, self.cnt[i] do
      self.stage[i][j] = func(self.stage[i + 1][j * 2 - 1], self.stage[i + 1][j * 2])
    end
  end
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 = self.emptyvalue
  local tasks = {{start_stage, left, right}}
  while 0 < #tasks do
    local task = tasks[#tasks]
    table.remove(tasks)
    local stage, l, r = task[1], task[2], task[3]
    if (l - 1) % self.size[stage] ~= 0 then
      local newr = mce((l - 1) / self.size[stage]) * self.size[stage]
      table.insert(tasks, {stage + 1, l, mmi(r, newr)})
      l = newr + 1
    end
    if self.size[stage] <= r + 1 - l then
      local pos = mce(l / self.size[stage])
      ret = self.func(ret, self.stage[stage][pos])
      l = l + self.size[stage]
    end
    if l <= r then
      table.insert(tasks, {stage + 1, l, r})
    end
  end
  return ret
end

SegTree.new = function(ary, func, emptyvalue)
  local obj = {}
  setmetatable(obj, {__index = SegTree})
  obj:create(ary, func, emptyvalue)
  return obj
end

local n = io.read("*n", "*l")
local str = io.read()
local t = {}
local idx = 1
for v in str:gmatch("%d+") do
  table.insert(t, {idx, tonumber(v)})
  idx = idx + 1
end
local segtree = SegTree.new(t, function(a, b) return a[2] < b[2] and a or b end, {0, n + 1})
local tasks = {{1, n}}
local ret1, ret2 = 0, 0
local big = 1000000000
while 0 < #tasks do
  local l, r = tasks[#tasks][1], tasks[#tasks][2]
  table.remove(tasks)
  local p = segtree:getRange(l, r)
  local val = p[2] * (p[1] + 1 - l) * (r + 1 - p[1])
  ret2 = ret2 + val
  ret1, ret2 = ret1 + mfl(ret2 / big), ret2 % big
  if p[1] ~= l then
    table.insert(tasks, {l, p[1] - 1})
  end
  if p[1] ~= r then
    table.insert(tasks, {p[1] + 1, r})
  end
end
ret2 = 0 < ret1 and string.format("%09d", ret2) or ret2
ret1 = 0 < ret1 and ret1 or ""
print(ret1 .. ret2)