結果

問題 No.1170 Never Want to Walk
ユーザー phsplsphspls
提出日時 2022-12-30 23:38:53
言語 Rust
(1.83.0 + proconio)
結果
AC  
実行時間 270 ms / 2,000 ms
コード長 3,119 bytes
コンパイル時間 11,449 ms
コンパイル使用メモリ 376,088 KB
実行使用メモリ 14,720 KB
最終ジャッジ日時 2024-11-25 22:00:50
合計ジャッジ時間 16,913 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 37
権限があれば一括ダウンロードができます
コンパイルメッセージ
warning: value assigned to `maxidx` is never read
  --> src/main.rs:94:13
   |
94 |     let mut maxidx = 0usize;
   |             ^^^^^^
   |
   = help: maybe it is overwritten before being read?
   = note: `#[warn(unused_assignments)]` on by default

warning: field `n` is never read
  --> src/main.rs:31:5
   |
30 | struct UnionFind {
   |        --------- field in this struct
31 |     n: usize,
   |     ^
   |
   = note: `#[warn(dead_code)]` on by default

warning: method `chain_cnt` is never used
  --> src/main.rs:74:8
   |
37 | impl UnionFind {
   | -------------- method in this implementation
...
74 |     fn chain_cnt(&mut self, i: usize) -> usize {
   |        ^^^^^^^^^

ソースコード

diff #

fn lower_bound(target: usize, vals: &Vec<usize>) -> usize {
    let mut upper = vals.len();
    let mut lower = 0usize;
    while upper > lower {
        let middle = (upper + lower) / 2;
        if vals[middle] < target {
            lower = middle+1;
        } else {
            upper = middle;
        }
    }
    upper
}

fn upper_bound(target: usize, vals: &Vec<usize>) -> usize {
    let mut upper = vals.len();
    let mut lower = 0usize;
    while upper > lower {
        let middle = (upper + lower) / 2;
        if vals[middle] <= target {
            lower = middle+1;
        } else {
            upper = middle;
        }
    }
    upper
}

struct UnionFind {
    n: usize,
    parents: Vec<usize>,
    depths: Vec<usize>,
    chains: Vec<usize>,
}

impl UnionFind {
    fn new(n: usize) -> Self {
        UnionFind {
            n: n,
            parents: (0..n).collect(),
            depths: vec![0; n],
            chains: vec![1; n],
        }
    }

    fn equiv(&mut self, a: usize, b: usize) -> bool {
        self.find(a) == self.find(b)
    }
    
    fn unite(&mut self, a: usize, b: usize) {
        if self.equiv(a, b) { return; }
        let x = self.parents[a];
        let y = self.parents[b];
        if self.depths[x] > self.depths[y] {
            self.parents[x] = self.parents[y];
            self.chains[y] += self.chains[x];
        } else {
            self.parents[y] = self.parents[x];
            self.chains[x] += self.chains[y];
            if self.depths[x] == self.depths[y] {
                self.depths[x] += 1;
            }
        }
    }

    fn find(&mut self, a: usize) -> usize {
        if self.parents[a] == a { return a; }
        let p = self.find(self.parents[a]);
        self.parents[a] = p;
        p
    }

    fn chain_cnt(&mut self, i: usize) -> usize {
        let idx = self.find(i);
        self.chains[idx]
    }
}

fn main() {
    let mut nab = String::new();
    std::io::stdin().read_line(&mut nab).ok();
    let nab: Vec<usize> = nab.trim().split_whitespace().map(|s| s.parse().unwrap()).collect();
    let n = nab[0];
    let a = nab[1];
    let b = nab[2];
    let mut x = String::new();
    std::io::stdin().read_line(&mut x).ok();
    let x: Vec<usize> = x.trim().split_whitespace().map(|s| s.parse().unwrap()).collect();

    let mut uf = UnionFind::new(n);
    let mut size = vec![1usize; n];
    let mut maxval = 0usize;
    let mut maxidx = 0usize;
    for i in 0..n {
        maxval = maxval.max(x[i]+a);
        let lidx = lower_bound(maxval, &x);
        let ridx = upper_bound(x[i]+b, &x);
        maxidx = ridx-1;
        maxval = maxval.max(x[maxidx]);
        for j in lidx..ridx {
            let pi = uf.find(i);
            let pj = uf.find(j);
            if pi == pj { continue; }
            uf.unite(pi, pj);
            let p = uf.find(pi);
            if p == pi {
                size[pi] += size[pj];
                size[pj] = 0;
            } else {
                size[pj] += size[pi];
                size[pi] = 0;
            }
        }
    }
    for i in 0..n {
        println!("{}", size[uf.find(i)]);
    }
}
0