結果
| 問題 | 
                            No.2786 RMQ on Grid Path
                             | 
                    
| コンテスト | |
| ユーザー | 
                             | 
                    
| 提出日時 | 2024-06-26 18:22:29 | 
| 言語 | Rust  (1.83.0 + proconio)  | 
                    
| 結果 | 
                             
                                AC
                                 
                             
                            
                         | 
                    
| 実行時間 | 2,891 ms / 6,000 ms | 
| コード長 | 5,957 bytes | 
| コンパイル時間 | 16,634 ms | 
| コンパイル使用メモリ | 377,820 KB | 
| 実行使用メモリ | 40,224 KB | 
| 最終ジャッジ日時 | 2024-06-26 18:24:01 | 
| 合計ジャッジ時間 | 67,027 ms | 
| 
                            ジャッジサーバーID (参考情報)  | 
                        judge2 / judge1 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 2 | 
| other | AC * 35 | 
ソースコード
use proconio::{input, marker::Usize1};
fn main() {
    input! {
        h:usize,
        w:usize,
        a:[[usize;w];h],
        q:usize,
        sg:[(Usize1,Usize1,Usize1,Usize1);q],
    }
    let mut poss = vec![vec![]; h * w + 1];
    for i in 0..h * w {
        poss[a[i / w][i % w]].push(i);
    }
    let mut ranges = vec![(0, h * w); q];
    while ranges.iter().any(|&(ng, ok)| ok - ng > 1) {
        let mut ms = (0..q)
            .map(|i| ((ranges[i].0 + ranges[i].1) / 2, i))
            .collect::<Vec<_>>();
        ms.sort();
        let mut uf = UnionFindTree::new(h * w);
        let mut j = 0;
        for i in 0..=h * w {
            for &p in poss[i].iter() {
                for Pos { x: nx, y: ny } in Pos::new(p / w, p % w).neigh4(h, w) {
                    if a[nx][ny] > i {
                        continue;
                    }
                    let np = nx * w + ny;
                    uf.unite(p, np);
                }
            }
            while j < q && ms[j].0 == i {
                if uf.same(
                    sg[ms[j].1].0 * w + sg[ms[j].1].1,
                    sg[ms[j].1].2 * w + sg[ms[j].1].3,
                ) {
                    ranges[ms[j].1].1 = i;
                } else {
                    ranges[ms[j].1].0 = i;
                }
                j += 1;
            }
        }
    }
    let ans = ranges.iter().map(|(_, ok)| *ok).collect::<Vec<_>>();
    for i in 0..q {
        println!("{}", ans[i]);
    }
}
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()
        }
    }
}
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!(),
            }
        }
    }
}