結果

問題 No.649 ここでちょっとQK!
ユーザー hatoohatoo
提出日時 2018-02-10 14:38:44
言語 Rust
(1.77.0)
結果
AC  
実行時間 109 ms / 3,000 ms
コード長 5,920 bytes
コンパイル時間 1,503 ms
コンパイル使用メモリ 180,372 KB
実行使用メモリ 15,992 KB
最終ジャッジ日時 2024-04-21 12:33:46
合計ジャッジ時間 4,523 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 35 ms
12,788 KB
testcase_04 AC 80 ms
15,992 KB
testcase_05 AC 82 ms
15,980 KB
testcase_06 AC 63 ms
14,368 KB
testcase_07 AC 1 ms
5,376 KB
testcase_08 AC 1 ms
5,376 KB
testcase_09 AC 1 ms
5,376 KB
testcase_10 AC 1 ms
5,376 KB
testcase_11 AC 1 ms
5,376 KB
testcase_12 AC 55 ms
8,864 KB
testcase_13 AC 52 ms
8,864 KB
testcase_14 AC 53 ms
8,864 KB
testcase_15 AC 52 ms
8,868 KB
testcase_16 AC 50 ms
8,868 KB
testcase_17 AC 54 ms
9,544 KB
testcase_18 AC 60 ms
10,164 KB
testcase_19 AC 69 ms
10,968 KB
testcase_20 AC 72 ms
11,524 KB
testcase_21 AC 79 ms
12,328 KB
testcase_22 AC 83 ms
12,948 KB
testcase_23 AC 90 ms
13,636 KB
testcase_24 AC 98 ms
14,316 KB
testcase_25 AC 104 ms
15,000 KB
testcase_26 AC 109 ms
15,736 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 44 ms
8,608 KB
testcase_31 AC 41 ms
8,604 KB
testcase_32 AC 1 ms
5,376 KB
testcase_33 AC 1 ms
5,376 KB
testcase_34 AC 0 ms
5,376 KB
testcase_35 AC 1 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 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