結果

問題 No.1170 Never Want to Walk
ユーザー phsplsphspls
提出日時 2022-12-30 23:33:38
言語 Rust
(1.77.0)
結果
TLE  
実行時間 -
コード長 3,025 bytes
コンパイル時間 1,290 ms
コンパイル使用メモリ 147,544 KB
実行使用メモリ 11,744 KB
最終ジャッジ日時 2023-08-17 04:20:28
合計ジャッジ時間 11,084 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 1 ms
4,384 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 2 ms
4,384 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 2 ms
4,380 KB
testcase_16 AC 3 ms
4,380 KB
testcase_17 AC 2 ms
4,376 KB
testcase_18 AC 2 ms
4,380 KB
testcase_19 AC 2 ms
4,380 KB
testcase_20 AC 2 ms
4,380 KB
testcase_21 AC 2 ms
4,380 KB
testcase_22 AC 7 ms
4,380 KB
testcase_23 AC 4 ms
4,380 KB
testcase_24 AC 6 ms
4,376 KB
testcase_25 AC 7 ms
4,380 KB
testcase_26 AC 6 ms
4,380 KB
testcase_27 AC 305 ms
11,484 KB
testcase_28 AC 299 ms
11,420 KB
testcase_29 AC 306 ms
11,744 KB
testcase_30 AC 310 ms
11,712 KB
testcase_31 AC 301 ms
11,440 KB
testcase_32 TLE -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
権限があれば一括ダウンロードができます
コンパイルメッセージ
warning: field `n` is never read
  --> Main.rs:31:5
   |
30 | struct UnionFind {
   |        --------- field in this struct
31 |     n: usize,
   |     ^
   |
   = note: `#[warn(dead_code)]` on by default

warning: associated function `chain_cnt` is never used
  --> Main.rs:74:8
   |
74 |     fn chain_cnt(&mut self, i: usize) -> usize {
   |        ^^^^^^^^^

warning: 2 warnings emitted

ソースコード

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;
    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);
        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