Problem:
Score : 600 points

Problem Statement

There is a function f(x), which is initially a constant function f(x) = 0.

We will ask you to process Q queries in order. There are two kinds of queries, update queries and evaluation queries, as follows:

An update query 1 a b: Given two integers a and b, let g(x) = f(x) + |x - a| + b and replace f(x) with g(x).

An evaluation query 2: Print x that minimizes f(x), and the minimum value of f(x). If there are multiple such values of x, choose the minimum such value.

We can show that the values to be output in an evaluation query are always integers, so we ask you to print those values as integers without decimal points.

Constraints

All values in input are integers.

1 \leq Q \leq 2 \times 10^5

-10^9 \leq a, b \leq 10^9

The first query is an update query.

Input

Input is given from Standard Input in the following format:

Q
Query_1
:
Query_Q

See Sample Input 1 for an example.

Output

For each evaluation query, print a line containing the response, in the order in which the queries are given.

The response to each evaluation query should be the minimum value of x that minimizes f(x), and the minimum value of f(x), in this order, with space in between.

Sample Input 1

4
1 4 2
2
1 1 -8
2

Sample Output 1

4 2
1 -3

In the first evaluation query, f(x) = |x - 4| + 2, which attains the minimum value of 2 at x = 4.

In the second evaluation query, f(x) = |x - 1| + |x - 4| - 6, which attains the minimum value of -3 when 1 \leq x \leq 4. Among the multiple values of x that minimize f(x), we ask you to print the minimum, that is, 1.

Sample Input 2

4
1 -1000000000 1000000000
1 -1000000000 1000000000
1 -1000000000 1000000000
2

Sample Output 2

-1000000000 3000000000

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
    for j = 1, self.cnt[i] do
      self.stage[i][j] = self.func(self.stage[i + 1][j * 2 - 1], self.stage[i + 1][j * 2])
    end
  end
end
SegTree.create = function(self, n, func)
  self.func = func
  local stagenum, mul = 1, 1
  self.cnt, self.stage, self.size = {1}, {{}}, {}
  while mul < 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, mul do self.stage[stagenum][i] = {0, 0LL} end
  self:updateAll()
end
SegTree.setValue = function(self, idx, value)
  self.stage[self.stagenum][idx] = value
  for i = self.stagenum - 1, 1, -1 do
    local dst = mce(idx / 2)
    local rem = dst * 4 - 1 - idx
    self.stage[i][dst] = self.func(self.stage[i + 1][idx], self.stage[i + 1][rem])
    idx = dst
  end
end
SegTree.new = function(n, func)
  local obj = {}
  setmetatable(obj, {__index = SegTree})
  obj:create(n, func)
  return obj
end

SegTree.lower_bound = function(self, val)
  local ret, retpos = {0, 0LL}, 0
  local t1, t2, t3 = {1}, {1}, {self.size[1]}
  local retval = 0
  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 sz <= r + 1 - l then
      local tmp = self.func(ret, self.stage[stage][mce(l / sz)])
      if tmp[1] < val then
        ret, retpos = tmp, l + sz - 1
        retval = tmp[2]
        if sz ~= 1 then table.insert(t1, stage + 1) table.insert(t2, l + sz) table.insert(t3, r) end
      else
        if sz ~= 1 then table.insert(t1, stage + 1) table.insert(t2, l) table.insert(t3, l + sz - 2) end
      end
    else
      table.insert(t1, stage + 1) table.insert(t2, l) table.insert(t3, r)
    end
  end
  return retpos + 1, retval
end

local q = io.read("*n")
local qlist = {}
local alist = {}
for i = 1, q do
  local tp = io.read("*n")
  if tp == 1 then
    local a, b = io.read("*n", "*n")
    qlist[i] = {a, b}
    table.insert(alist, a)
  else
    qlist[i] = false
  end
end
table.sort(alist)
local amap = {}
local asum = {}
local addcnt = {}
for i = 1, #alist do
  if not amap[alist[i]] then
    amap[alist[i]] = i
  end
  if i == 1 then asum[i] = alist[i]
  else asum[i] = asum[i - 1] + alist[i]
  end
  addcnt[alist[i]] = 0
end
local tree = SegTree.new(#alist, function(x, y) return {x[1] + y[1], x[2] + y[2]} end)
local acnt = 0
local bsum = 0
for i = 1, q do
  local query = qlist[i]
  if query then
    local a = query[1]
    local tmp = addcnt[a]
    local addpos = amap[query[1]] + tmp
    addcnt[a] = tmp + 1
    tree:setValue(addpos, {1, 0LL + a})
    bsum = bsum + query[2]
    acnt = acnt + 1
  else
    local half = mfl((acnt + 1) / 2)
    local calcpos, preval = tree:lower_bound(half)
    local src = alist[calcpos]
    preval = preval + src
    local curtot = tree.stage[1][1][2]
    local result = bsum + half * src - preval
      + (curtot - preval) - (acnt - half) * src
    print(src .. " " .. tostring(result):gsub("LL", ""))
  end
end