#[allow(dead_code)] mod tree { use std; pub struct WBT { root: Link, } impl WBT { pub fn new() -> WBT { WBT { root: None, } } pub fn insert(&mut self, val: T) { let r = self.root.take(); self.root = Node::insert(r, val); } pub fn remove(&mut self, val: &T) { let r = self.root.take(); self.root = Node::remove(r, val); } pub fn search_at(&self, k: usize) -> Option<&T> { Node::search_at(&self.root, k) } pub fn search_by(&self, val: &T) -> usize { Node::search_by(&self.root, val) } } type NonNull = Box>; type Link = Option>; struct Node { val: T, size: usize, left: Link, right: Link, } #[derive(PartialEq, Eq)] enum Bias { N, L, R, } impl Node { fn get_size(t: &Link) -> usize { match t.as_ref() { None => 0, Some(t) => t.size, } } fn update(&mut self) { self.size = 1 + Node::get_size(&self.left) + Node::get_size(&self.right); } fn get_bias(t: &NonNull, p: usize) -> Bias { let l_size = Node::get_size(&t.left) + 1; let r_size = Node::get_size(&t.right) + 1; if p * l_size >= r_size && l_size <= p * r_size { Bias::N } else if l_size > p * r_size { Bias::L } else { Bias::R } } fn get_bias_lr(l: &Link, r: &Link, p: usize) -> Bias { let l_size = Node::get_size(l) + 1; let r_size = Node::get_size(r) + 1; if p * l_size >= r_size && l_size <= p * r_size { Bias::N } else if l_size > p * r_size { Bias::L } else { Bias::R } } fn left_rotate(mut u: NonNull) -> NonNull { let mut v = u.right.take().expect("left_rotate: input right child is None"); u.right = v.left.take(); u.update(); v.left = Some(u); v.update(); v } fn right_rotate(mut u: NonNull) -> NonNull { let mut v = u.left.take().expect("right_rotate: input left child is None"); u.left = v.left.take(); u.update(); v.right = Some(u); v.update(); v } fn balance(mut t: NonNull) -> NonNull { match Node::get_bias(&t, 3) { Bias::N => t, Bias::L => { let l = t.left.take().unwrap(); t.left = Some(if Node::get_bias_lr(&l.left, &l.right, 2) == Bias::R { Node::left_rotate(l) } else { l }); Node::right_rotate(t) }, Bias::R => { let r = t.right.take().unwrap(); t.right = Some(if Node::get_bias_lr(&r.left, &r.right, 2) == Bias::L { Node::right_rotate(r) } else { r }); Node::left_rotate(t) }, } } fn merge(l: Link, r: Link) -> Link { if l.is_none() { return r; } if r.is_none() { return l; } Some(match Node::get_bias_lr(&l, &r, 1) { Bias::L => { let mut l = l.unwrap(); let lr = l.right.take(); l.right = Node::merge(lr, r); l.update(); Node::balance(l) }, _ => { let mut r = r.unwrap(); let rl = r.left.take(); r.left = Node::merge(l, rl); r.update(); Node::balance(r) }, }) } fn insert(t: Link, val: T) -> Link { if t.is_none() { return Some(Box::new(Node { val: val, size: 1, left: None, right: None, })); } let mut t = t.unwrap(); if val < t.val { let l = t.left.take(); t.left = Node::insert(l, val); } else { let r = t.right.take(); t.right = Node::insert(r, val); } t.update(); Some(Node::balance(t)) } fn remove(t: Link, val: &T) -> Link { if t.is_none() { return t; } let mut t = t.unwrap(); match t.val.cmp(val) { std::cmp::Ordering::Equal => { let l = t.left.take(); let r = t.right.take(); Node::merge(l, r) }, std::cmp::Ordering::Greater => { let l = t.left.take(); t.left = Node::remove(l, val); t.update(); Some(Node::balance(t)) }, std::cmp::Ordering::Less => { let r = t.right.take(); t.right = Node::remove(r, val); t.update(); Some(Node::balance(t)) }, } } fn search_by(t: &Link, val: &T) -> usize { if t.is_none() { return 0; } let t = t.as_ref().unwrap(); match t.val.cmp(val) { std::cmp::Ordering::Equal => { let l_size = Node::get_size(&t.left); l_size }, std::cmp::Ordering::Greater => { Node::search_by(&t.left, val) }, std::cmp::Ordering::Less => { let l_size = Node::get_size(&t.left); l_size + 1 + Node::search_by(&t.right, val) }, } } fn search_at(t: &Link, k: usize) -> Option<&T> { if k >= Node::get_size(t) { return None; } let t = t.as_ref().unwrap(); let l_size = Node::get_size(&t.left); if k == l_size { Some(&t.val) } else if k < l_size { Node::search_at(&t.left, k) } else { Node::search_at(&t.right, k - l_size - 1) } } } } use std::io::{Read, Write}; fn run() { let out = std::io::stdout(); let mut out = std::io::BufWriter::new(out.lock()); let mut s = String::new(); std::io::stdin().read_to_string(&mut s).unwrap(); let mut it = s.trim().split_whitespace(); let q: usize = it.next().unwrap().parse().unwrap(); let k: usize = it.next().unwrap().parse().unwrap(); let mut set = tree::WBT::::new(); for _ in 0..q { let op: u8 = it.next().unwrap().parse().unwrap(); if op == 1 { let v: u64 = it.next().unwrap().parse().unwrap(); set.insert(v); } else { match set.search_at(k - 1) { None => writeln!(out, "-1").unwrap(), Some(&v) => { set.remove(&v); writeln!(out, "{}", v).unwrap(); }, } } } } fn main(){ run(); }