AtCoder Beginner Contest 458 D

https://atcoder.jp/contests/abc458/tasks/abc458_d

半分に分けて、奇数個だから左側が一つ多くなるように保ちます。

// Chalkboard Median
#![allow(non_snake_case)]

use std::collections::BinaryHeap;


//////////////////// library ////////////////////

fn read<T: std::str::FromStr>() -> T {
    let mut line = String::new();
    std::io::stdin().read_line(&mut line).ok();
    line.trim().parse().ok().unwrap()
}

fn read_vec<T: std::str::FromStr>() -> Vec<T> {
    read::<String>().split_whitespace()
            .map(|e| e.parse().ok().unwrap()).collect()
}


//////////////////// process ////////////////////

fn read_input() -> (i32, usize) {
    let X: i32 = read();
    let Q: usize = read();
    (X, Q)
}

fn read_query() -> (i32, i32) {
    let v: Vec<i32> = read_vec();
    let (A, B) = (v[0], v[1]);
    (A, B)
}

fn F(X: i32, Q: usize) {
    let mut heap1 = BinaryHeap::new();
    let mut heap2 = BinaryHeap::new();
    heap1.push(X);
    for i in 0..Q {
        let (A, B) = read_query();
        let &mid = heap1.peek().unwrap();
        for x in [A, B] {
            if x < mid {
                heap1.push(x)
            }
            else {
                heap2.push(-x)
            }
        }
        
        while heap2.len() != i + 1 {
            if heap2.len() < i + 1 {
                let x = heap1.pop().unwrap();
                heap2.push(-x)
            }
            else {
                let x = heap2.pop().unwrap();
                heap1.push(-x)
            }
        }
        println!("{}", heap1.peek().unwrap())
    }
}

fn main() {
    let (X, Q) = read_input();
    F(X, Q)
}