結果

問題 No.953 席
ユーザー koba-e964koba-e964
提出日時 2019-12-17 02:06:56
言語 Rust
(1.77.0 + proconio)
結果
RE  
実行時間 -
コード長 5,516 bytes
コンパイル時間 17,340 ms
コンパイル使用メモリ 378,524 KB
実行使用メモリ 35,712 KB
最終ジャッジ日時 2024-07-02 21:06:24
合計ジャッジ時間 21,955 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 1 ms
5,248 KB
testcase_02 RE -
testcase_03 RE -
testcase_04 RE -
testcase_05 RE -
testcase_06 RE -
testcase_07 AC 260 ms
25,728 KB
testcase_08 AC 318 ms
33,408 KB
testcase_09 AC 208 ms
22,912 KB
testcase_10 AC 74 ms
9,856 KB
testcase_11 AC 224 ms
25,600 KB
testcase_12 RE -
testcase_13 RE -
testcase_14 RE -
testcase_15 RE -
testcase_16 RE -
testcase_17 RE -
testcase_18 RE -
testcase_19 RE -
testcase_20 RE -
testcase_21 RE -
testcase_22 RE -
testcase_23 RE -
testcase_24 RE -
testcase_25 AC 323 ms
35,456 KB
testcase_26 AC 136 ms
35,712 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#[allow(unused_imports)]
use std::cmp::*;
#[allow(unused_imports)]
use std::collections::*;
use std::io::{Write, BufWriter};
// https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8
macro_rules! input {
    ($($r:tt)*) => {
        let stdin = std::io::stdin();
        let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin.lock()));
        let mut next = move || -> String{
            bytes
                .by_ref()
                .map(|r|r.unwrap() as char)
                .skip_while(|c|c.is_whitespace())
                .take_while(|c|!c.is_whitespace())
                .collect()
        };
        input_inner!{next, $($r)*}
    };
}

macro_rules! input_inner {
    ($next:expr) => {};
    ($next:expr, ) => {};

    ($next:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($next, $t);
        input_inner!{$next $($r)*}
    };
}

macro_rules! read_value {
    ($next:expr, ( $($t:tt),* )) => {
        ( $(read_value!($next, $t)),* )
    };

    ($next:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
    };

    ($next:expr, chars) => {
        read_value!($next, String).chars().collect::<Vec<char>>()
    };

    ($next:expr, usize1) => {
        read_value!($next, usize) - 1
    };

    ($next:expr, [ $t:tt ]) => {{
        let len = read_value!($next, usize);
        (0..len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
    }};

    ($next:expr, $t:ty) => {
        $next().parse::<$t>().expect("Parse error")
    };
}

#[allow(unused)]
macro_rules! debug {
    ($($format:tt)*) => (write!(std::io::stderr(), $($format)*).unwrap());
}
#[allow(unused)]
macro_rules! debugln {
    ($($format:tt)*) => (writeln!(std::io::stderr(), $($format)*).unwrap());
}

// 0: nearest, 1: the second nearest
fn pri(v: usize, _n: usize, k1: usize, k2: usize) -> usize {
    if v == k1 {
        return 0;
    }
    if (v < k1) != (k2 < k1) {
        return ((v as i32 - k1 as i32).abs() * 2) as usize;
    }
    ((v as i32 - k1 as i32).abs() * 2 - 1) as usize
}

fn assign(r: &mut BTreeSet<(usize, usize)>,
          r2: &mut BTreeSet<(usize, usize)>,
          k1: usize, k2: usize,
          n: usize) -> usize {
    if r2.is_empty() {
        let &mi = r.iter().next().unwrap();
        r.remove(&mi);
        return mi.1;
    }
    let &mi = r2.iter().next().unwrap();
    r2.remove(&mi);
    r.remove(&mi);
    // neighbors
    let mut indices = vec![];
    if mi.1 + 1 < n {
        indices.push(mi.1 + 1);
    }
    if mi.1 > 0 {
        indices.push(mi.1 - 1);
    }
    for idx in indices {
        let e = (pri(idx, n, k1, k2), idx);
        r2.remove(&e);
    }
    mi.1
}

fn fix(r: &BTreeSet<(usize, usize)>,
       r2: &mut BTreeSet<(usize, usize)>,
       v: usize, n: usize, k1: usize, k2: usize) {
    let mut ok = true;
    if v >= 1 {
        ok &= r.contains(&(pri(v - 1, 0, k1, k2), v - 1));
    }
    if v + 1 < n {
        ok &= r.contains(&(pri(v + 1, 0, k1, k2), v + 1));
    }
    ok &= r.contains(&(pri(v, 0, k1, k2), v));
    let e = (pri(v, 0, k1, k2), v);
    if ok && !r2.contains(&e) {
        r2.insert(e);
    }
    if !ok && r2.contains(&e) {
        r2.remove(&e);
    }
}

fn solve() {
    let out = std::io::stdout();
    let mut out = BufWriter::new(out.lock());
    macro_rules! puts {
        ($($format:tt)*) => (write!(out,$($format)*).unwrap());
    }
    input! {
        n: usize, k1: usize1, k2: usize1,
        q: usize,
        ab: [(i64, i64); q],
    }
    let mut ev: BTreeMap<_, (Vec<usize>, Vec<usize>)> = BTreeMap::new();
    for i in 0..q {
        let (a, b) = ab[i];
        let e = ev.entry(a).or_insert((vec![], vec![]));
        e.0.push(i);
        let e = ev.entry(a + b).or_insert((vec![], vec![]));
        e.1.push(i);
    }
    let mut r2 = BTreeSet::new();
    let mut r = BTreeSet::new();
    for i in 0..n {
        r2.insert((pri(i, n, k1, k2), i));
        r.insert((pri(i, n, k1, k2), i));
    }
    let mut ans = vec![n + 1; q];
    let mut waiting = VecDeque::new();
    for (_time, (come, leave)) in ev {
        /*
        eprintln!("time = {}, ans = {:?}", time, ans);
        eprintln!("r = {:?}, r2 = {:?}", r, r2);
         */
        for c in come {
            waiting.push_back(c);
        }
        for idx in leave {
            let pos = ans[idx % q];
            assert!(pos < n);
            // eprintln!("leaving: {} {}", idx, pos);
            r.insert((pri(pos, n, k1, k2), pos));
            let mut indices = vec![pos];
            if pos + 1 < n {
                indices.push(pos + 1);
            }
            if pos > 0 {
                indices.push(pos - 1);
            }
            for idx in indices {
                fix(&r, &mut r2, idx, n, k1, k2);
            }
            //eprintln!("r,r2={:?}, {:?}", r, r2);
        }
        while !r.is_empty() && !waiting.is_empty() {
            if let Some(v) = waiting.pop_front() {
                //eprintln!("assigning {}, {:?}, {:?}", v, r, r2);
                let pos = assign(&mut r, &mut r2, k1, k2, n);
                ans[v] = pos;
                //eprintln!("assigning {} -> {}, {:?}, {:?}", v, pos, r, r2);
            }
        }
    }
    for i in 0..q {
        puts!("{}\n", ans[i] + 1);
    }
}

fn main() {
    // In order to avoid potential stack overflow, spawn a new thread.
    let stack_size = 104_857_600; // 100 MB
    let thd = std::thread::Builder::new().stack_size(stack_size);
    thd.spawn(|| solve()).unwrap().join().unwrap();
}
0