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 Rust (1.15.1) (judge verdict: Wrong Answer)

#![allow(unused_imports)]
#![allow(unused_macros)]
use std::cmp::{ min, max };

macro_rules! ewriteln {
    ($($args:expr),*) => { let _ = writeln!(&mut std::io::stderr(), $($args),*); };
}
macro_rules! trace {
    ($x:expr) => { ewriteln!(">>> {} = {:?}", stringify!($x), $x) };
    ($($xs:expr),*) => { trace!(($($xs),*)) }
}
macro_rules! put {
    ($x:expr) => { println!("{}", $x) };
    ($x:expr, $($xs:expr),*) => { print!("{} ", $x); put!($($xs),*) }
}

#[allow(dead_code)]
const M: i64 = 1_000_000_007;

use std::ops::Range;
struct BIT { size: usize, array: Vec<i64> }
impl BIT {
    fn new(n: usize) -> BIT {
        BIT  { size: n, array: vec![0; n + 1] }
    }
    fn add(&mut self, idx: usize, w: i64) {
        let mut x = idx + 1;
        while x <= self.size {
            self.array[x] += w;
            let xi = x as i64;
            x += (xi&-xi) as usize;
        }
    }
    fn sum_up(&self, idx: usize) -> i64 { // [0, idx)
        let mut sum = 0;
        let mut x = idx;
        while x > 0 {
            sum += self.array[x];
            let xi = x as i64;
            x -= (xi&-xi) as usize;
        }
        sum
    }
    fn sum(&self, range: Range<usize>) -> i64 {
        self.sum_up(range.end) - self.sum_up(range.start)
    }
    fn at(&self, idx: usize) -> i64 {
        self.sum(idx..idx+1)
    }
    fn to_vec(&self) -> Vec<i64> {
        let mut a = vec![];
        for i in 0..self.size {
            a.push(self.at(i));
        }
        a
    }
}

#[derive(Debug)]
enum Query {
    Add(i64, i64),
    Eval
}

fn main() {
    let mut sc = Scanner::new();
    let n: usize = sc.cin();
    let qs: Vec<Query> = (0..n).map(|_| {
        let t: usize = sc.cin();
        if t == 1 {
            let a: i64 = sc.cin();
            let b: i64 = sc.cin();
            Query::Add(a, b)
        } else {
            Query::Eval
        }
    }).collect();

    let mut xs = vec![];
    let mut y_sum = 0;
    for i in 0..n {
        match qs[i] {
            Query::Add(a, b) => {
                y_sum += b;
                xs.push(a);
            },
            Query::Eval => {}
        }
    }
    xs.sort();

    use std::collections::HashMap;
    let mut indices = HashMap::new();
    {
        let mut zs = vec![];
        for i in 0..n {
            match qs[i] {
                Query::Add(a, _) => {
                    zs.push((a, i));
                },
                _ => {}
            }
        }
        zs.sort();
        for j in 0..zs.len() {
            let (_, i) = zs[j];
            indices.insert(i, j);
        }
    }

    let mut xs_bit = BIT::new(xs.len());
    for i in 0..xs.len() {
        xs_bit.add(i, xs[i]);
    }

    let mut bit = BIT::new(xs.len());
    for i in 0..xs.len() {
        bit.add(i, 1);
    }

    let mut count = xs.len();

    let mut ans = vec![];

    for i in (0..n).rev() {
        // trace!(qs[i]);
        match qs[i] {
            Query::Add(_, b) => {
                y_sum -= b;
                let idx: usize = *indices.get(&i).unwrap();
                xs_bit.add(idx, -xs[idx]);
                bit.add(idx, -1);
                count -= 1;
                // trace!(xs_bit.to_vec());
                // trace!(bit.to_vec());
            },
            Query::Eval => {
                if count == 1 {
                    let x_min = xs_bit.sum(0..xs.len());
                    let y_min = y_sum;
                    ans.push((x_min, y_min));
                } else if count % 2 == 0 {
                    let g = count as i64 / 2;
                    let mut left = 0;
                    let mut right = xs.len();
                    for _ in 0..100 {
                        let mid = (left + right) / 2;
                        if bit.sum(0..mid+1) < g {
                            left = mid;
                        } else {
                            right = mid;
                        }
                    }
                    let mut idx = left;
                    for i in left..xs.len() {
                        if bit.sum(0..i+1) == g {
                            idx = i;
                        }
                    }
                    let x_min = xs[idx];
                    let y_min = xs_bit.sum(idx+1..xs.len()) - xs_bit.sum(0..idx+1) + y_sum;
                    ans.push((x_min, y_min));
                } else {
                    let g = count as i64 / 2;
                    let mut left = 0;
                    let mut right = xs.len();
                    for _ in 0..100 {
                        let mid = (left + right) / 2;
                        if bit.sum(0..mid+1) < g {
                            left = mid;
                        } else {
                            right = mid;
                        }
                    }
                    let mut idx = left;
                    // trace!(idx);
                    for i in left..xs.len() {
                        if bit.sum(0..i+1) == g {
                            idx = i;
                        }
                    }
                    let x_min = xs[idx];
                    let y_min = xs_bit.sum(idx+1..xs.len()) - xs_bit.sum(0..idx+2) + y_sum;
                    ans.push((x_min, y_min));
                }
            }
        }
    }

    ans.reverse();
    for &(x, y) in ans.iter() {
        println!("{} {}", x, y);
    }

}

use std::io::{self, Write};
use std::str::FromStr;
use std::collections::VecDeque;

struct Scanner { stdin: io::Stdin, buffer: VecDeque<String>, }
impl Scanner {
    fn new() -> Scanner { Scanner { stdin: io::stdin(), buffer: VecDeque::new() } }
    fn cin<T: FromStr>(&mut self) -> T {
        while self.buffer.len() == 0 {
            let mut line = String::new();
            let _ = self.stdin.read_line(&mut line);
            for w in line.split_whitespace() {
                self.buffer.push_back(String::from(w));
            }
        }
        self.buffer.pop_front().unwrap().parse::<T>().ok().unwrap()
    }
}