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 C# (Mono 4.6.2.0) (judge verdict: Accepted)

using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using SB = System.Text.StringBuilder;
//using System.Threading.Tasks;
//using System.Text.RegularExpressions;
//using System.Globalization;
//using System.Diagnostics;
using static System.Console;
using System.Numerics;
using static System.Math;
using pair = Pair<int, int>;

class Program
{
    static void Main()
    {
        SetOut(new StreamWriter(OpenStandardOutput()) { AutoFlush = false });
        new Program().solve();
        Out.Flush();
    }
    readonly Scanner cin = new Scanner();
    readonly int[] dd = { 0, 1, 0, -1, 0 }; //→↓←↑
    readonly int mod = 1000000007;
    readonly int dom = 998244353;
    bool chmax<T>(ref T a, T b) where T : IComparable<T> { if (a.CompareTo(b) < 0) { a = b; return true; } return false; }
    bool chmin<T>(ref T a, T b) where T : IComparable<T> { if (b.CompareTo(a) < 0) { a = b; return true; } return false; }

    void solve()
    {
        int T = cin.nextint;
        var S = new Set<int> { IsMultiSet = true };
        var SUM = new SegTree<long>(T, (a, b) => a + b, 0);
        var CNT = new FenwickTree(T);

        long K = 0;

        var A = new pair[T];
        var ID = new int[T];

        for (int t = 0; t < T; t++)
        {
            ID[t] = t;
            if (cin.nextint == 2)
            {
                A[t] = new pair(int.MaxValue, int.MaxValue);
                continue;
            }

            A[t] = new pair(cin.nextint, cin.nextint);
        }

        Array.Sort(A, ID);

        var to = new int[T];
        var H = new HashMap<int, int>();
        for (int i = 0; i < T; i++)
        {
            to[ID[i]] = i;
            H[A[i].f] = i;
        }


        for (int t = 0; t < T; t++)
        {
            var idx = to[t];
            int a = A[idx].f;
            int b = A[idx].s;
            if (a == int.MaxValue)
            {
                var mid = S[(S.Count - 1) / 2];
                var x = H[mid];

                long ans = K;
                //WriteLine(ans);
                //S.Items.join();

                ans += mid * CNT[0, x - 1] - SUM[0, x - 1];
                ans += SUM[x + 1, T - 1] - mid * CNT[x + 1, T - 1];
                WriteLine(mid + " " + ans);
                continue;
            }

            K += b;
            S.Add(a);
            SUM.Update(idx, a);
            CNT.Add(idx, 1);

        }
    }
}

/// <OriginalAuthor>riantkb</OriginalAuthor>
class SegTree<T>
{
    readonly int n;
    int s, t;
    T[] tr;
    readonly Func<T, T, T> f;
    readonly T exnum;

    /// <summary>
    ///  セグメントツリーの構築1
    /// </summary>
    /// <param name="m">要素数</param>
    /// <param name="f">マージ関数</param>
    /// <param name="ex">単位元</param>
    public SegTree(int m, Func<T, T, T> f, T ex)
    {
        this.f = f; exnum = ex; n = 1;
        while (n < m) n <<= 1;
        tr = new T[(n << 1) - 1];
        for (int i = 0; i < tr.Length; i++) tr[i] = ex;
    }
    public SegTree(int m, Func<T, T, T> f, T ex, T ini) : this(m, f, ex)
    {
        for (int i = 0; i < m; i++) Assign(i, ini);
        All_Update();
    }
    public SegTree(int m, Func<T, T, T> f, T ex, IList<T> ini) : this(m, f, ex)
    {
        for (int i = 0; i < m; i++) Assign(i, ini[i]);
        All_Update();
    }
    public T Look(int j) => tr[j + n - 1];
    public T Peek() => tr[0];


    /// <summary>
    /// 最下層に値の代入
    /// </summary>
    /// <param name="j">代入するインデックス</param>
    /// <param name="x">代入する値</param>
    public void Assign(int j, T x) => tr[j + n - 1] = x;
    public void Update(int j, T x)
    {
        Assign(j, x);
        Update(j);
    }
    public void Update(int j)
    {
        int i = j + n - 1;
        while (i > 0) { i = i - 1 >> 1; tr[i] = f(tr[i << 1 | 1], tr[i + 1 << 1]); }
    }
    public void All_Update() { for (int i = n - 2; i >= 0; i--) tr[i] = f(tr[i << 1 | 1], tr[i + 1 << 1]); }
    /// <summary>
    /// return node[s, t] 区間クエリ
    /// </summary>
    /// <param name="s">左端 0-indexed</param>
    /// <param name="t">右端 0-indexed</param>
    /// <returns>node[s, t]</returns>
    public T this[int s, int t] => Run(s, t);
    T Run(int s, int t) { t++; this.s = s; this.t = t; return Query(0, 0, n); }
    T Query(int k, int l, int r) => r <= s || t <= l ? exnum : s <= l && r <= t ? tr[k]
                              : f(Query(k << 1 | 1, l, l + r >> 1), Query(k + 1 << 1, l + r >> 1, r));
}

class HashMap<TKey, TValue> : Dictionary<TKey, TValue>
{
    public new TValue this[TKey key]
    {
        get
        {
            TValue value;
            return TryGetValue(key, out value) ? value : default(TValue);
        }
        set
        {
            base[key] = value;
        }
    }
}

/// <OriginalAuthor>camypaper</OriginalAuthor>
class Set<T>
{
    Node root;
    readonly IComparer<T> comparer;
    readonly Node nil;
    readonly T nil_key;
    public bool IsMultiSet { get; set; }
    public Set(IComparer<T> comparer, T nil_key)
    {
        this.nil_key = nil_key;
        nil = new Node(nil_key);
        root = nil;
        this.comparer = comparer;
    }
    public Set(Comparison<T> comaprison, T nil_key = default(T)) : this(Comparer<T>.Create(comaprison), nil_key) { }
    public Set(T nil_key = default(T)) : this(Comparer<T>.Default, nil_key) { }
    public bool Add(T v)
    {
        key = v;
        return insert(ref root);
    }
    public bool Remove(T v)
    {
        key = v;
        return remove(ref root);
    }
    public T this[int index] { get { return find(root, index); } }
    public int Count { get { return root.Count; } }
    public void RemoveAt(int k)
    {
        if (k < 0 || k >= root.Count) throw new ArgumentOutOfRangeException();
        removeAt(ref root, k);
    }
    public T[] Items
    {
        get
        {
            var ret = new T[root.Count];
            var k = 0;
            walk(root, ret, ref k);
            return ret;
        }
    }
    void walk(Node t, T[] a, ref int k)
    {
        if (t.Count == 0) return;
        walk(t.lst, a, ref k);
        a[k++] = t.Key;
        walk(t.rst, a, ref k);
    }

    T key;
    bool insert(ref Node t)
    {
        if (t.Count == 0) { t = new Node(key); t.lst = t.rst = nil; t.Update(); return true; }
        var cmp = comparer.Compare(t.Key, key);
        bool res;
        if (cmp > 0)
            res = insert(ref t.lst);
        else if (cmp == 0)
        {
            if (IsMultiSet) res = insert(ref t.lst);
            else return false;
        }
        else res = insert(ref t.rst);
        balance(ref t);
        return res;
    }
    bool remove(ref Node t)
    {
        if (t.Count == 0) return false;
        var cmp = comparer.Compare(key, t.Key);
        bool ret;
        if (cmp < 0) ret = remove(ref t.lst);
        else if (cmp > 0) ret = remove(ref t.rst);
        else
        {
            ret = true;
            var k = t.lst.Count;
            if (k == 0) { t = t.rst; return true; }
            if (t.rst.Count == 0) { t = t.lst; return true; }


            t.Key = find(t.lst, k - 1);
            removeAt(ref t.lst, k - 1);
        }
        balance(ref t);
        return ret;
    }
    void removeAt(ref Node t, int k)
    {
        var cnt = t.lst.Count;
        if (cnt < k) removeAt(ref t.rst, k - cnt - 1);
        else if (cnt > k) removeAt(ref t.lst, k);
        else
        {
            if (cnt == 0) { t = t.rst; return; }
            if (t.rst.Count == 0) { t = t.lst; return; }

            t.Key = find(t.lst, k - 1);
            removeAt(ref t.lst, k - 1);
        }
        balance(ref t);
    }
    void balance(ref Node t)
    {
        var balance = t.lst.Height - t.rst.Height;
        if (balance == -2)
        {
            if (t.rst.lst.Height - t.rst.rst.Height > 0) { rotR(ref t.rst); }
            rotL(ref t);
        }
        else if (balance == 2)
        {
            if (t.lst.lst.Height - t.lst.rst.Height < 0) rotL(ref t.lst);
            rotR(ref t);
        }
        else t.Update();
    }

    T find(Node t, int k)
    {
        if (k < 0 || k > root.Count) throw new ArgumentOutOfRangeException();
        for (; ; )
        {
            if (k == t.lst.Count) return t.Key;
            else if (k < t.lst.Count) t = t.lst;
            else { k -= t.lst.Count + 1; t = t.rst; }
        }
    }
    public int LowerBound(T v)
    {
        var k = 0;
        var t = root;
        for (; ; )
        {
            if (t.Count == 0) return k;
            if (comparer.Compare(v, t.Key) <= 0) t = t.lst;
            else { k += t.lst.Count + 1; t = t.rst; }
        }
    }
    public int UpperBound(T v)
    {
        var k = 0;
        var t = root;
        for (; ; )
        {
            if (t.Count == 0) return k;
            if (comparer.Compare(t.Key, v) <= 0) { k += t.lst.Count + 1; t = t.rst; }
            else t = t.lst;
        }
    }
    public T LowerBoundValue(T v)
    {
        var t = root;
        T ret = nil_key;
        for (; ; )
        {
            if (t.Count == 0) return ret;
            if (comparer.Compare(v, t.Key) <= 0)
            {
                ret = t.Key;
                t = t.lst;
            }
            else
            {
                t = t.rst;
            }
        }
    }
    public T UpperBoundValue(T v)
    {
        var t = root;
        T ret = nil_key;
        for (; ; )
        {
            if (t.Count == 0) return ret;
            if (comparer.Compare(t.Key, v) <= 0)
            {
                t = t.rst;
            }
            else
            {
                ret = t.Key;
                t = t.lst;
            }

        }
    }
    void rotR(ref Node t)
    {
        var l = t.lst;
        t.lst = l.rst;
        l.rst = t;
        t.Update();
        l.Update();
        t = l;
    }
    void rotL(ref Node t)
    {
        var r = t.rst;
        t.rst = r.lst;
        r.lst = t;
        t.Update();
        r.Update();
        t = r;
    }

    class Node
    {
        public Node(T key)
        {
            Key = key;
        }
        public int Count { get; private set; }
        public sbyte Height { get; private set; }
        public T Key { get; set; }
        public Node lst, rst;
        public void Update()
        {
            Count = 1 + lst.Count + rst.Count;
            Height = (sbyte)(1 + Math.Max(lst.Height, rst.Height));
        }
        public override string ToString()
        {
            return string.Format("Count = {0}, Key = {1}", Count, Key);
        }
    }
}

/// <OriginalAuthor>camypaper</OriginalAuthor>
class FenwickTree
{
    readonly int n;
    readonly long[] bit;
    readonly int max = 1;
    public FenwickTree(int size)
    {
        n = size; bit = new long[n + 1];
        while ((max << 1) <= n) max <<= 1;
    }
    /// <summary>sum[a, b] 0-indexed</summary>
    public long this[int i, int j] { get { i++; j++; return i <= j ? this[j] - this[i - 1] : 0; } }
    /// <summary>sum[1, i] 1-indexed</summary>
    public long this[int i] { get { long s = 0; for (; i > 0; i -= i & -i) s += bit[i]; return s; } }
    public int LowerBound(long w)
    {
        if (w <= 0) return 0;
        int x = 0;
        for (int k = max; k > 0; k >>= 1)
            if (x + k <= n && bit[x + k] < w)
            {
                w -= bit[x + k];
                x += k;
            }
        return x + 1;
    }
    /// <summary>add v to bit[i] 0-indexed</summary>
    public void Add(int i, long v)
    {
        i++;
        //if (i == 0) System.Diagnostics.Debug.Fail("BIT is 1 indexed");
        for (; i <= n; i += i & -i) bit[i] += v;
    }
    public long[] Items
    {
        get
        {
            var ret = new long[n + 1];
            for (int i = 0; i < ret.Length; i++)
                ret[i] = this[i, i];
            return ret;
        }
    }
}

class RangeAddFenwickTree
{
    readonly int n;
    FenwickTree a, b;
    public RangeAddFenwickTree(int n)
    {
        this.n = n;
        a = new FenwickTree(n);
        b = new FenwickTree(n);
    }
    /// <summary>Add V to[i, j] 0-indexed</summary>
    public void Add(int i, int j, long v)
    {
        //a.Add(i, -(i - 1) * v); a.Add(j + 1, j * v);
        //b.Add(i, v); b.Add(j + 1, -v);
        a.Add(i, -i * v); a.Add(j + 1, (j + 1) * v);
        b.Add(i, v); b.Add(j + 1, -v);
    }
    /// <summary>Sum [1, i] 1-indexed</summary>
    public long this[int i] => a[i] + b[i] * i;
    /// <summary>Sum [i, j] 0-indexed</summary>
    public long this[int i, int j] { get { i++; j++; return i <= j ? this[j] - this[i - 1] : 0; } }
    public long[] Items
    {
        get
        {
            var ret = new long[n + 1];
            for (int i = 0; i < ret.Length; i++)
                ret[i] = this[i, i];
            return ret;
        }
    }
}


static class Ex
{
    public static void join<T>(this IEnumerable<T> values, string sep = " ") => WriteLine(string.Join(sep, values));
    public static string concat<T>(this IEnumerable<T> values) => string.Concat(values);
    public static string reverse(this string s) { var t = s.ToCharArray(); Array.Reverse(t); return t.concat(); }

    public static int lower_bound<T>(this IList<T> arr, T val) where T : IComparable<T>
    {
        int low = 0, high = arr.Count;
        int mid;
        while (low < high)
        {
            mid = ((high - low) >> 1) + low;
            if (arr[mid].CompareTo(val) < 0) low = mid + 1;
            else high = mid;
        }
        return low;
    }
    public static int upper_bound<T>(this IList<T> arr, T val) where T : IComparable<T>
    {
        int low = 0, high = arr.Count;
        int mid;
        while (low < high)
        {
            mid = ((high - low) >> 1) + low;
            if (arr[mid].CompareTo(val) <= 0) low = mid + 1;
            else high = mid;
        }
        return low;
    }
}

class Pair<T, U> : IComparable<Pair<T, U>> where T : IComparable<T> where U : IComparable<U>
{
    public T f; public U s;
    public Pair(T f, U s) { this.f = f; this.s = s; }
    public int CompareTo(Pair<T, U> a) => f.CompareTo(a.f) != 0 ? f.CompareTo(a.f) : s.CompareTo(a.s);
    public override string ToString() => $"{f} {s}";
}

class Scanner
{
    string[] s; int i;
    readonly char[] cs = new char[] { ' ' };
    public Scanner() { s = new string[0]; i = 0; }
    public string[] scan => ReadLine().Split();
    public int[] scanint => Array.ConvertAll(scan, int.Parse);
    public long[] scanlong => Array.ConvertAll(scan, long.Parse);
    public double[] scandouble => Array.ConvertAll(scan, double.Parse);
    public string next
    {
        get
        {
            if (i < s.Length) return s[i++];
            string st = ReadLine();
            while (st == "") st = ReadLine();
            s = st.Split(cs, StringSplitOptions.RemoveEmptyEntries);
            i = 0;
            return next;
        }
    }
    public int nextint => int.Parse(next);
    public long nextlong => long.Parse(next);
    public double nextdouble => double.Parse(next);
}