結果

問題 No.649 ここでちょっとQK!
ユーザー hatoohatoo
提出日時 2018-02-10 15:04:11
言語 Rust
(1.77.0)
結果
AC  
実行時間 1,560 ms / 3,000 ms
コード長 5,681 bytes
コンパイル時間 1,296 ms
コンパイル使用メモリ 172,416 KB
実行使用メモリ 15,108 KB
最終ジャッジ日時 2024-04-21 13:31:10
合計ジャッジ時間 11,815 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 0 ms
5,376 KB
testcase_02 AC 0 ms
5,376 KB
testcase_03 AC 39 ms
12,800 KB
testcase_04 AC 1,560 ms
15,108 KB
testcase_05 AC 88 ms
15,100 KB
testcase_06 AC 775 ms
15,104 KB
testcase_07 AC 0 ms
5,376 KB
testcase_08 AC 1 ms
5,376 KB
testcase_09 AC 1 ms
5,376 KB
testcase_10 AC 0 ms
5,376 KB
testcase_11 AC 1 ms
5,376 KB
testcase_12 AC 224 ms
8,480 KB
testcase_13 AC 225 ms
8,480 KB
testcase_14 AC 224 ms
8,484 KB
testcase_15 AC 218 ms
8,484 KB
testcase_16 AC 208 ms
8,484 KB
testcase_17 AC 247 ms
9,088 KB
testcase_18 AC 288 ms
9,784 KB
testcase_19 AC 340 ms
10,460 KB
testcase_20 AC 368 ms
11,136 KB
testcase_21 AC 414 ms
11,692 KB
testcase_22 AC 472 ms
12,564 KB
testcase_23 AC 534 ms
13,004 KB
testcase_24 AC 595 ms
13,708 KB
testcase_25 AC 658 ms
14,248 KB
testcase_26 AC 714 ms
14,912 KB
testcase_27 AC 1 ms
5,376 KB
testcase_28 AC 1 ms
5,376 KB
testcase_29 AC 1 ms
5,376 KB
testcase_30 AC 213 ms
8,476 KB
testcase_31 AC 192 ms
8,476 KB
testcase_32 AC 1 ms
5,376 KB
testcase_33 AC 1 ms
5,376 KB
testcase_34 AC 1 ms
5,376 KB
testcase_35 AC 0 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

/**
 *  _           _                 __                            _   _ _   _                                 _                    _                  _
 * | |         | |               / /                           | | (_) | (_)                               | |                  (_)                | |
 * | |__   __ _| |_ ___   ___   / /__ ___  _ __ ___  _ __   ___| |_ _| |_ ___   _____ ______ _ __ _   _ ___| |_ ______ ___ _ __  _ _ __  _ __   ___| |_ ___
 * | '_ \ / _` | __/ _ \ / _ \ / / __/ _ \| '_ ` _ \| '_ \ / _ \ __| | __| \ \ / / _ \______| '__| | | / __| __|______/ __| '_ \| | '_ \| '_ \ / _ \ __/ __|
 * | | | | (_| | || (_) | (_) / / (_| (_) | | | | | | |_) |  __/ |_| | |_| |\ V /  __/      | |  | |_| \__ \ |_       \__ \ | | | | |_) | |_) |  __/ |_\__ \
 * |_| |_|\__,_|\__\___/ \___/_/ \___\___/|_| |_| |_| .__/ \___|\__|_|\__|_| \_/ \___|      |_|   \__,_|___/\__|      |___/_| |_|_| .__/| .__/ \___|\__|___/
 *                                                  | |                                                                           | |   | |
 *                                                  |_|                                                                           |_|   |_|
 *
 * https://github.com/hatoo/competitive-rust-snippets
 */
#[allow(unused_imports)]
use std::cmp::{max, min, Ordering};
#[allow(unused_imports)]
use std::collections::{BTreeMap, BTreeSet, BinaryHeap, HashMap, HashSet, VecDeque};
#[allow(unused_imports)]
use std::iter::FromIterator;
#[allow(unused_imports)]
use std::io::{stdin, stdout, BufWriter, Write};
mod util {
    use std::io::{stdin, stdout, BufWriter, StdoutLock};
    use std::str::FromStr;
    use std::fmt::Debug;
    #[allow(dead_code)]
    pub fn line() -> String {
        let mut line: String = String::new();
        stdin().read_line(&mut line).unwrap();
        line.trim().to_string()
    }
    #[allow(dead_code)]
    pub fn gets<T: FromStr>() -> Vec<T>
    where
        <T as FromStr>::Err: Debug,
    {
        let mut line: String = String::new();
        stdin().read_line(&mut line).unwrap();
        line.split_whitespace()
            .map(|t| t.parse().unwrap())
            .collect()
    }
    #[allow(dead_code)]
    pub fn with_bufwriter<F: FnOnce(BufWriter<StdoutLock>) -> ()>(f: F) {
        let out = stdout();
        let writer = BufWriter::new(out.lock());
        f(writer)
    }
}
#[allow(unused_macros)]
macro_rules ! get { ( $ t : ty ) => { { let mut line : String = String :: new ( ) ; stdin ( ) . read_line ( & mut line ) . unwrap ( ) ; line . trim ( ) . parse ::<$ t > ( ) . unwrap ( ) } } ; ( $ ( $ t : ty ) ,* ) => { { let mut line : String = String :: new ( ) ; stdin ( ) . read_line ( & mut line ) . unwrap ( ) ; let mut iter = line . split_whitespace ( ) ; ( $ ( iter . next ( ) . unwrap ( ) . parse ::<$ t > ( ) . unwrap ( ) , ) * ) } } ; ( $ t : ty ; $ n : expr ) => { ( 0 ..$ n ) . map ( | _ | get ! ( $ t ) ) . collect ::< Vec < _ >> ( ) } ; ( $ ( $ t : ty ) ,*; $ n : expr ) => { ( 0 ..$ n ) . map ( | _ | get ! ( $ ( $ t ) ,* ) ) . collect ::< Vec < _ >> ( ) } ; ( $ t : ty ;; ) => { { let mut line : String = String :: new ( ) ; stdin ( ) . read_line ( & mut line ) . unwrap ( ) ; line . split_whitespace ( ) . map ( | t | t . parse ::<$ t > ( ) . unwrap ( ) ) . collect ::< Vec < _ >> ( ) } } ; }
#[allow(unused_macros)]
macro_rules ! debug { ( $ ( $ a : expr ) ,* ) => { println ! ( concat ! ( $ ( stringify ! ( $ a ) , " = {:?}, " ) ,* ) , $ ( $ a ) ,* ) ; } }

#[allow(dead_code)]
/// Generic Binary Indexed Tree
pub struct BIT<T: Clone, F: Fn(&mut T, &T) -> ()> {
    buf: Vec<T>,
    zero: T,
    f: F,
}
impl<T: Clone, F: Fn(&mut T, &T) -> ()> BIT<T, F> {
    #[allow(dead_code)]
    pub fn new(n: usize, zero: &T, f: F) -> BIT<T, F> {
        BIT {
            buf: vec![zero.clone(); n + 1],
            zero: zero.clone(),
            f: f,
        }
    }
    #[allow(dead_code)]
    pub fn sum(&self, i: usize) -> T {
        let mut i = i;
        let mut s = self.zero.clone();
        while i > 0 {
            (self.f)(&mut s, &self.buf[i]);
            i &= i - 1;
        }
        s
    }
    #[allow(dead_code)]
    pub fn add(&mut self, i: usize, x: &T) {
        let mut i = i as i64;
        while i < self.buf.len() as i64 {
            let t = &mut self.buf[i as usize];
            (self.f)(t, x);
            i += i & -i;
        }
    }
}

#[allow(dead_code)]
fn main() {
    let (q, k) = get!(usize, usize);

    let inputs = get!(String; q);

    let mut cc: Vec<u64> = inputs
        .iter()
        .filter_map(|s| {
            let mut split = s.split_whitespace();
            if split.next() == Some("1") {
                split.next().unwrap().parse().ok()
            } else {
                None
            }
        })
        .collect();

    cc.sort();
    cc.dedup();

    let mut buf: Vec<u32> = Vec::with_capacity(q);

    util::with_bufwriter(|mut out| {
        for line in inputs {
            let mut split = line.split_whitespace();

            if split.next() == Some("1") {
                let x: u64 = split.next().unwrap().parse().unwrap();
                let y = cc.binary_search(&x).unwrap();

                let i = match buf.binary_search(&(y as u32)) {
                    Ok(i) => i,
                    Err(i) => i,
                };

                buf.insert(i, y as u32);
            } else {
                if let Some(ans) = buf.get(k - 1).cloned() {
                    writeln!(out, "{}", cc[ans as usize]).unwrap();
                    buf.remove(k - 1);
                } else {
                    writeln!(out, "-1").unwrap();
                }
            }
        }
    });
}
0