結果

問題 No.2786 RMQ on Grid Path
ユーザー Yukino DX.Yukino DX.
提出日時 2024-06-19 21:31:27
言語 Rust
(1.77.0)
結果
AC  
実行時間 2,381 ms / 6,000 ms
コード長 6,000 bytes
コンパイル時間 15,476 ms
コンパイル使用メモリ 377,868 KB
実行使用メモリ 40,124 KB
最終ジャッジ日時 2024-06-19 21:32:27
合計ジャッジ時間 57,661 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 1 ms
5,248 KB
testcase_02 AC 3 ms
5,376 KB
testcase_03 AC 3 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 3 ms
5,376 KB
testcase_06 AC 3 ms
5,376 KB
testcase_07 AC 3 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 3 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 3 ms
5,376 KB
testcase_12 AC 2,337 ms
40,080 KB
testcase_13 AC 2,329 ms
40,124 KB
testcase_14 AC 2,337 ms
40,032 KB
testcase_15 AC 2,328 ms
40,084 KB
testcase_16 AC 2,381 ms
39,980 KB
testcase_17 AC 2,359 ms
40,088 KB
testcase_18 AC 2,339 ms
40,092 KB
testcase_19 AC 2,334 ms
40,084 KB
testcase_20 AC 2,379 ms
40,064 KB
testcase_21 AC 2,380 ms
40,080 KB
testcase_22 AC 2,055 ms
39,624 KB
testcase_23 AC 1,983 ms
39,624 KB
testcase_24 AC 965 ms
34,488 KB
testcase_25 AC 977 ms
34,648 KB
testcase_26 AC 920 ms
34,372 KB
testcase_27 AC 678 ms
16,512 KB
testcase_28 AC 601 ms
17,580 KB
testcase_29 AC 1,857 ms
33,808 KB
testcase_30 AC 606 ms
16,372 KB
testcase_31 AC 67 ms
5,376 KB
testcase_32 AC 466 ms
32,916 KB
testcase_33 AC 276 ms
15,984 KB
testcase_34 AC 961 ms
37,700 KB
testcase_35 AC 969 ms
37,704 KB
testcase_36 AC 958 ms
37,700 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

use proconio::{input, marker::Usize1};

fn main() {
    input! {
        h:usize,
        w:usize,
        a:[[usize;w];h],
        q:usize,
        query:[(Usize1,Usize1,Usize1,Usize1);q],
    }

    let mut poss = vec![vec![]; h * w + 1];
    for i in 0..h {
        for j in 0..w {
            poss[a[i][j]].push(i * w + j);
        }
    }

    let mut rs = vec![(0, h * w); q];
    while rs.iter().any(|&(l, r)| r - l > 1) {
        let mut ms = rs
            .iter()
            .enumerate()
            .map(|(i, &(l, r))| ((l + r) / 2, i))
            .collect::<Vec<_>>();
        ms.sort();

        let mut i = 0;
        let mut j = 0;
        let mut dsu = UnionFindTree::new(h * w);
        while i < q {
            while j <= ms[i].0 {
                for &pos in poss[j].iter() {
                    for Pos { x: nx, y: ny } in Pos::new(pos / w, pos % w).neigh4(h, w) {
                        if a[nx][ny] <= j {
                            dsu.unite(pos, nx * w + ny);
                        }
                    }
                }

                j += 1;
            }

            let s = query[ms[i].1].0 * w + query[ms[i].1].1;
            let g = query[ms[i].1].2 * w + query[ms[i].1].3;
            if dsu.same(s, g) {
                rs[ms[i].1].1 = ms[i].0;
            } else {
                rs[ms[i].1].0 = ms[i].0;
            }

            i += 1;
        }
    }

    let ans = rs.into_iter().map(|(_, r)| r).collect::<Vec<_>>();
    for i in 0..q {
        println!("{}", ans[i]);
    }
}

use grid_mgr::*;
mod grid_mgr {
    //[R,D,L,U]
    pub const DIR4: [(usize, usize); 4] = [(0, 1), (1, 0), (0, !0), (!0, 0)];
    pub const DIR8: [(usize, usize); 8] = [
        (1, 0),
        (1, !0),
        (0, !0),
        (!0, !0),
        (!0, 0),
        (!0, 1),
        (0, 1),
        (1, 1),
    ];

    pub struct Pos {
        pub x: usize,
        pub y: usize,
    }

    impl Pos {
        #[allow(dead_code)]
        pub fn new(x: usize, y: usize) -> Self {
            Self { x, y }
        }

        #[allow(dead_code)]
        pub fn neigh4<'a>(&'a self, h: usize, w: usize) -> impl Iterator<Item = Pos> + 'a {
            DIR4.iter().filter_map(move |&(dx, dy)| {
                let nx = self.x.wrapping_add(dx);
                let ny = self.y.wrapping_add(dy);
                if nx < h && ny < w {
                    Some(Pos::new(nx, ny))
                } else {
                    None
                }
            })
        }

        #[allow(dead_code)]
        pub fn neigh8<'a>(&'a self, h: usize, w: usize) -> impl Iterator<Item = Pos> + 'a {
            DIR8.iter().filter_map(move |&(dx, dy)| {
                let nx = self.x.wrapping_add(dx);
                let ny = self.y.wrapping_add(dy);
                if nx < h && ny < w {
                    Some(Pos::new(nx, ny))
                } else {
                    None
                }
            })
        }

        #[allow(dead_code)]
        pub fn line<'a>(
            &'a self,
            h: usize,
            w: usize,
            dir: (usize, usize),
        ) -> impl Iterator<Item = Pos> + 'a {
            (0..).map_while(move |i| {
                let nx = self.x.wrapping_add(dir.0.wrapping_mul(i));
                let ny = self.y.wrapping_add(dir.1.wrapping_mul(i));
                if nx < h && ny < w {
                    Some(Pos::new(nx, ny))
                } else {
                    None
                }
            })
        }

        #[allow(dead_code)]
        pub fn _moveto(&self, h: usize, w: usize, dir: (usize, usize)) -> Option<Pos> {
            let nx = self.x.wrapping_add(dir.0);
            let ny = self.y.wrapping_add(dir.1);
            if nx < h && ny < w {
                Some(Pos::new(nx, ny))
            } else {
                None
            }
        }

        #[allow(dead_code)]
        pub fn moveto(&self, h: usize, w: usize, dir: char) -> Option<Pos> {
            match dir {
                'R' => self._moveto(h, w, DIR4[0]),
                'D' => self._moveto(h, w, DIR4[1]),
                'L' => self._moveto(h, w, DIR4[2]),
                'U' => self._moveto(h, w, DIR4[3]),
                _ => unreachable!(),
            }
        }
    }
}

use unionfindtree::*;
mod unionfindtree {
    pub struct UnionFindTree {
        par: Vec<usize>,
        rank: Vec<usize>,
        size: Vec<usize>,
    }

    impl UnionFindTree {
        pub fn new(n: usize) -> Self {
            Self {
                par: (0..n).collect::<Vec<_>>(),
                rank: vec![0; n],
                size: vec![1; n],
            }
        }

        #[allow(dead_code)]
        pub fn same(&mut self, v1: usize, v2: usize) -> bool {
            self.root(v1) == self.root(v2)
        }

        #[allow(dead_code)]
        pub fn size(&mut self, v: usize) -> usize {
            let v_root = self.root(v);
            self.size[v_root]
        }

        #[allow(dead_code)]
        pub fn root(&mut self, v: usize) -> usize {
            if self.par[v] != v {
                self.par[v] = self.root(self.par[v]);
            }

            self.par[v]
        }

        pub fn unite(&mut self, v1: usize, v2: usize) {
            let mut v1_root = self.root(v1);
            let mut v2_root = self.root(v2);
            if v1_root == v2_root {
                return;
            }

            if self.rank[v1_root] < self.rank[v2_root] {
                std::mem::swap(&mut v1_root, &mut v2_root);
            }

            self.par[v2_root] = v1_root;
            self.size[v1_root] += self.size[v2_root];

            if self.rank[v1_root] == self.rank[v2_root] {
                self.rank[v1_root] += 1;
            }
        }

        #[allow(dead_code)]
        pub fn nofcc(&mut self) -> usize {
            let n = self.par.len();
            (0..n)
                .map(|v| self.root(v))
                .collect::<std::collections::HashSet<usize>>()
                .len()
        }
    }
}
0