結果

問題 No.649 ここでちょっとQK!
ユーザー hatoohatoo
提出日時 2018-02-10 14:38:44
言語 Rust
(1.77.0 + proconio)
結果
AC  
実行時間 132 ms / 3,000 ms
コード長 5,920 bytes
コンパイル時間 14,342 ms
コンパイル使用メモリ 379,500 KB
実行使用メモリ 16,000 KB
最終ジャッジ日時 2024-10-13 11:02:15
合計ジャッジ時間 18,359 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 1 ms
5,248 KB
testcase_02 AC 1 ms
5,248 KB
testcase_03 AC 42 ms
12,832 KB
testcase_04 AC 93 ms
16,000 KB
testcase_05 AC 90 ms
16,000 KB
testcase_06 AC 70 ms
14,292 KB
testcase_07 AC 1 ms
5,248 KB
testcase_08 AC 1 ms
5,248 KB
testcase_09 AC 1 ms
5,248 KB
testcase_10 AC 1 ms
5,248 KB
testcase_11 AC 1 ms
5,248 KB
testcase_12 AC 62 ms
8,864 KB
testcase_13 AC 62 ms
8,864 KB
testcase_14 AC 60 ms
8,864 KB
testcase_15 AC 59 ms
8,872 KB
testcase_16 AC 58 ms
8,864 KB
testcase_17 AC 64 ms
9,548 KB
testcase_18 AC 72 ms
10,168 KB
testcase_19 AC 79 ms
10,972 KB
testcase_20 AC 84 ms
11,652 KB
testcase_21 AC 93 ms
12,332 KB
testcase_22 AC 100 ms
12,956 KB
testcase_23 AC 108 ms
13,512 KB
testcase_24 AC 118 ms
14,320 KB
testcase_25 AC 123 ms
14,996 KB
testcase_26 AC 132 ms
15,744 KB
testcase_27 AC 0 ms
5,248 KB
testcase_28 AC 2 ms
5,248 KB
testcase_29 AC 1 ms
5,248 KB
testcase_30 AC 49 ms
8,608 KB
testcase_31 AC 47 ms
8,604 KB
testcase_32 AC 1 ms
5,248 KB
testcase_33 AC 1 ms
5,248 KB
testcase_34 AC 1 ms
5,248 KB
testcase_35 AC 1 ms
5,248 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 bit = BIT::new(cc.len(), &0i64, |a: &mut i64, b: &i64| *a += b);

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

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

                bit.add(c + 1, &1);
            } else {
                if bit.sum(cc.len()) < k as i64 {
                    writeln!(out, "-1").unwrap();
                } else {
                    let mut l = 0;
                    let mut r = cc.len();

                    while l != r {
                        let mid = (l + r) / 2;
                        if bit.sum(mid) < k as i64 {
                            l = mid + 1;
                        } else {
                            r = mid;
                        }
                    }

                    bit.add(l, &(-1));
                    writeln!(out, "{}", cc[l as usize - 1]).unwrap();
                }
            }
        }
    });
}
0