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++14 (GCC 5.4.1) (judge verdict: Accepted)

#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vll>;
using vvvll = vector<vvll>;

#define REP(i, n, m) for(ll i=n; i<(ll)m; ++i)
#define IREP(i, n, m) for(ll i=n-1; i>=m; --i)
#define rep(i, n) REP(i, 0, n)
#define irep(i, n) IREP(i, n, 0)
#define all(v) v.begin(), v.end()
#define vprint(v) for(auto e:v){cout<<e<<" ";};cout<<endl;
#define vvprint(vv) for(auto v:vv)vprint(v);


class SSTree{
public:
    ll size;
    vll v;
    vll lv;

    SSTree(ll n){
        this->size = n;
        ll m = 1;
        while(n>m) m *= 2;
        this->v = vll(2*m-1, 0);
        this->lv = vll(2*m-1, 0);
    }

    ll get(ll left, ll right, ll level=0, ll idx=0){
        if(left>right) return 0;
        ll step = (this->v.size()+1) >> (level+1);
        ll lb = (idx-(1<<level)+1) * step;
        ll ub = lb + step - 1;
        if(lb==left && ub==right){
            vll s;
            ll tmp = idx;
            do{ s.push_back(tmp=(tmp-1)/2); } while(tmp>0);
            for(auto it=s.rbegin(); it!=s.rend(); ++it){
                for(int i=1; i<=2; ++i) v[*it*2+i] += lv[*it]/2;
                for(int i=1; i<=2; ++i) lv[*it*2+i] += lv[*it]/2;
                lv[*it] = 0;
            }
            return v[idx];
        }
        else return get(left, min((lb+ub)/2, right), level+1, idx*2+1) + get(max((lb+ub)/2+1, left), right, level+1, idx*2+2);
    }

    void update(ll left, ll right, ll e, ll level=0, ll idx=0){
        if(left>right) return;
        ll step = (this->v.size()+1) >> (level+1);
        ll lb = (idx-(1<<level)+1) * step;
        ll ub = lb + step - 1;
        if(lb==left && ub==right){
            this->v[idx] += e*step;
            this->lv[idx] += e*step;
        }
        else{
            ll mid = (lb+ub) / 2;
            update(left, min(mid, right), e, level+1, idx*2+1);
            update(max(mid+1, left), right, e, level+1, idx*2+2);
        }
    }
};


int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << setprecision(20);

    ll Q;
    cin >> Q;

    vll as(Q), bs(Q), qs(Q);
    vector<pair<ll, ll>> abs;

    rep(i, Q){
        cin >> qs[i];
        if(qs[i]==1){
            cin >> as[i] >> bs[i];
            abs.push_back({as[i], bs[i]});
        }
    }
    sort(all(abs), [](auto l, auto r){ return l.first<r.first; });
    ll last = -1, count = 0;
    unordered_map<ll, ll> m;
    vll xs;
    rep(i, abs.size()){
        if(last!=abs[i].first){
            m[abs[i].first] = count;
            xs.push_back(abs[i].first);
            ++count;
        }
        last = abs[i].first;
    }

    ll n = m.size();
    SSTree t1 = SSTree(n+1);
    SSTree t2 = SSTree(n+1);

    rep(i, Q){
        if(qs[i]==1){
            ll idx = m[as[i]];
            t1.update(0, idx, -1);
            t1.update(idx+1, n, 1);
            t2.update(0, idx, bs[i]+as[i]);
            t2.update(idx+1, n, bs[i]-as[i]);
        }
        else{
            ll left = 0, right = n;
            while(left<right){
                ll mid = (left+right) / 2;
                ll a = t1.get(mid, mid);
                if(a<0) left = mid+1;
                else right = mid;
            }
            ll x = xs[left-1];
            ll y = t1.get(left, left)*x + t2.get(left, left);
            cout << x << " " << y << endl;
        }
    }
}