結果

問題 No.1950 片道きゃっちぼーる
ユーザー koba-e964koba-e964
提出日時 2022-05-20 23:21:40
言語 Rust
(1.77.0 + proconio)
結果
AC  
実行時間 526 ms / 3,000 ms
コード長 5,356 bytes
コンパイル時間 12,592 ms
コンパイル使用メモリ 385,256 KB
実行使用メモリ 102,636 KB
最終ジャッジ日時 2024-09-20 09:44:58
合計ジャッジ時間 21,087 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 319 ms
100,340 KB
testcase_04 AC 272 ms
84,720 KB
testcase_05 AC 1 ms
5,376 KB
testcase_06 AC 186 ms
58,320 KB
testcase_07 AC 338 ms
102,520 KB
testcase_08 AC 501 ms
102,636 KB
testcase_09 AC 303 ms
78,532 KB
testcase_10 AC 280 ms
76,832 KB
testcase_11 AC 269 ms
77,040 KB
testcase_12 AC 258 ms
76,912 KB
testcase_13 AC 251 ms
62,312 KB
testcase_14 AC 205 ms
45,324 KB
testcase_15 AC 526 ms
82,352 KB
testcase_16 AC 213 ms
50,472 KB
testcase_17 AC 1 ms
5,376 KB
testcase_18 AC 303 ms
93,992 KB
testcase_19 AC 487 ms
81,504 KB
testcase_20 AC 266 ms
76,864 KB
testcase_21 AC 221 ms
49,868 KB
testcase_22 AC 223 ms
49,744 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

use std::cmp::*;
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 ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
    };
    ($next:expr, $t:ty) => ($next().parse::<$t>().expect("Parse error"));
}

// Strong connected components.
// Verified by: yukicoder No.470 (http://yukicoder.me/submissions/145785)
//              ABC214-H (https://atcoder.jp/contests/abc214/submissions/25082618)
struct SCC {
    n: usize,
    ncc: usize,
    g: Vec<Vec<usize>>, // graph in adjacent list
    rg: Vec<Vec<usize>>, // reverse graph
    cmp: Vec<usize>, // topological order
}

impl SCC {
    fn new(n: usize) -> Self {
        SCC {
            n: n,
            ncc: n + 1,
            g: vec![Vec::new(); n],
            rg: vec![Vec::new(); n],
            cmp: vec![0; n],
        }
    }
    fn add_edge(&mut self, from: usize, to: usize) {
        self.g[from].push(to);
        self.rg[to].push(from);
    }
    fn dfs(&self, v: usize, used: &mut [bool], vs: &mut Vec<usize>) {
        used[v] = true;
        for &w in self.g[v].iter() {
            if !used[w] {
               self.dfs(w, used, vs);
            }
        }
        vs.push(v);
    }
    fn rdfs(&self, v: usize, k: usize,
            used: &mut [bool], cmp: &mut [usize]) {
        used[v] = true;
        cmp[v] = k;
        for &w in self.rg[v].iter() {
            if !used[w] {
                self.rdfs(w, k, used, cmp);
            }
        }
    }
    fn scc(&mut self) -> usize {
        let n = self.n;
        let mut used = vec![false; n];
        let mut vs = Vec::new();
        let mut cmp = vec![0; n];
        for v in 0 .. n {
            if !used[v] { self.dfs(v, &mut used, &mut vs); }
        }
        for u in used.iter_mut() {
            *u = false;
        }
        let mut k = 0;
        for &t in vs.iter().rev() {
            if !used[t] { self.rdfs(t, k, &mut used, &mut cmp); k += 1; }
        }
        self.ncc = k;
        self.cmp = cmp;
        k
    }
    #[allow(dead_code)]
    fn top_order(&self) -> Vec<usize> {
        assert!(self.ncc <= self.n);
        self.cmp.clone()
    }
    /*
     * Returns a dag whose vertices are scc's, and whose edges are those of the original graph.
     */
    #[allow(dead_code)]
    fn dag(&self) -> Vec<Vec<usize>> {
        assert!(self.ncc <= self.n);
        let ncc = self.ncc;
        let mut ret = vec![vec![]; ncc];
        let n = self.n;
        for i in 0 .. n {
            for &to in self.g[i].iter() {
                if self.cmp[i] != self.cmp[to] {
                    assert!(self.cmp[i] < self.cmp[to]);
                    ret[self.cmp[i]].push(self.cmp[to]);
                }
            }
        }
        ret.into_iter().map(|mut v| {
            v.sort_unstable(); v.dedup();
            v
        }).collect()
    }
    #[allow(dead_code)]
    fn rdag(&self) -> Vec<Vec<usize>> {
        assert!(self.ncc <= self.n);
        let ncc = self.ncc;
        let mut ret = vec![vec![]; ncc];
        let n = self.n;
        for i in 0 .. n {
            for &to in self.g[i].iter() {
                if self.cmp[i] != self.cmp[to] {
                    assert!(self.cmp[i] < self.cmp[to]);
                    ret[self.cmp[to]].push(self.cmp[i]);
                }
            }
        }
        ret.into_iter().map(|mut v| {
            v.sort_unstable(); v.dedup();
            v
        }).collect()
    }
}

fn main() {
    let out = std::io::stdout();
    let mut out = BufWriter::new(out.lock());
    macro_rules! puts {($($format:tt)*) => (let _ = write!(out,$($format)*););}
    input! {
        n: usize,
        x: [i64; n],
        a: [i64; n],
    }
    let mut coo = vec![];
    for i in 0..n {
        coo.extend_from_slice(&[x[i], x[i] + a[i], x[i] - a[i]]);
    }
    coo.sort(); coo.dedup();
    let m = coo.len();
    let mut scc = SCC::new(m);
    for i in 0..n {
        let from = coo.binary_search(&x[i]).unwrap();
        for &to in &[x[i] - a[i], x[i] + a[i]] {
            let to = coo.binary_search(&to).unwrap();
            scc.add_edge(from, to);
        }
    }
    let ncc = scc.scc();
    let mut ma = vec![0; ncc];
    let top_ord = scc.top_order();
    for i in 0..n {
        let from = coo.binary_search(&x[i]).unwrap();
        ma[top_ord[from]] = max(ma[top_ord[from]], x[i] + a[i]);
    }
    let dag = scc.dag();
    for i in (0..ncc).rev() {
        for &j in &dag[i] {
            ma[i] = max(ma[i], ma[j]);
        }
    }
    for i in 0..n {
        let from = coo.binary_search(&x[i]).unwrap();
        puts!("{}\n", ma[top_ord[from]] - x[i]);
    }
}
0