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, PartialEq, Eq)]
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();

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

    let num_update = indices.len();
    let mut xs = vec![0; num_update];
    let mut xs_bit = BIT::new(num_update);
    let mut bit = BIT::new(num_update);
    let mut count_update = 0;

    let mut x_sum = 0;
    let mut y_sum = 0;

    for i in 0..n {
        match qs[i] {
            Query::Add(a, b) => {
                x_sum += a;
                y_sum += b;
                let idx: usize = *indices.get(&i).unwrap();
                xs[idx] = a;
                xs_bit.add(idx, a);
                bit.add(idx, 1);
                count_update += 1;
            },
            Query::Eval => {
                if count_update == 1 {
                    let x_min = xs_bit.sum(0..num_update);
                    println!("{} {}", x_min, y_sum);
                } else if count_update % 2 == 0 {
                    let g = count_update as i64 / 2;
                    let mut left = 0;
                    let mut right = num_update;
                    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 sum_head = xs_bit.sum(0..idx + 1);
                    let sum_tail = x_sum - sum_head;
                    let y = y_sum + sum_tail - sum_head;
                    println!("{} {}", x_min, y);
                } else {
                    let g = count_update 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 sum_head = xs_bit.sum(0..idx + 1);
                    let sum_tail = x_sum - sum_head - x_min;
                    let y = y_sum + sum_tail - sum_head;
                    println!("{} {}", x_min, 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()
    }
}