結果

問題 No.3807 Crossword Key with Cropping (Hard)
コンテスト
ユーザー 👑 cologne
提出日時 2026-12-02 18:13:47
言語 Rust
(1.99.1 + proconio + num + itertools + ACL)
コンパイル:
/usr/bin/rustc_custom
実行:
./target/release/main
結果
AC  
実行時間 358 ms / 2,000 ms
+ 592µs
コード長 4,933 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 11,009 ms
コンパイル使用メモリ 176,824 KB
実行使用メモリ 26,496 KB
最終ジャッジ日時 2026-12-02 18:14:18
合計ジャッジ時間 19,204 ms
ジャッジサーバーID
(参考情報)
judge1_0 / judge2_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 45
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

use fio::*;

fn main() {
    let [h, w] = read_tuple::<usize, 2>();
    let mut s = vec![];
    for _ in 0..h {
        s.push(read_line().chars().collect::<Vec<_>>());
    }
    let mut dp = vec![vec![vec![vec![vec![vec![0; w + 1]; h + 1]; 2]; 2]; 2]; 2];

    for lb in 0..=1 {
        for rb in 0..=1 {
            for ub in 0..=1 {
                for db in 0..=1 {
                    for r in 0..h {
                        for c in 0..w {
                            let lc = if lb == 1 || c == 0 { '#' } else { s[r][c - 1] };
                            let rc = if rb == 1 || c == w - 1 {
                                '#'
                            } else {
                                s[r][c + 1]
                            };
                            let uc = if ub == 1 || r == 0 { '#' } else { s[r - 1][c] };
                            let dc = if db == 1 || r == h - 1 {
                                '#'
                            } else {
                                s[r + 1][c]
                            };

                            let mc = s[r][c];

                            let hor = mc == '.' && lc == '#' && rc == '.';
                            let ver = mc == '.' && uc == '#' && dc == '.';
                            let sin = mc == '.' && lc == '#' && rc == '#' && uc == '#' && dc == '#';
                            let dp = &mut dp[lb][rb][ub][db];
                            if hor || ver || sin {
                                dp[r + 1][c + 1] = 1;
                            }
                            dp[r + 1][c + 1] += dp[r][c + 1] + dp[r + 1][c] - dp[r][c];
                        }
                    }
                }
            }
        }
    }

    let q = read::<usize>();

    let getsum = |a: &Vec<Vec<i32>>, x1: usize, y1: usize, x2: usize, y2: usize| -> i32 {
        a[x2][y2] - a[x1][y2] - a[x2][y1] + a[x1][y1]
    };

    for _ in 0..q {
        let [x1, y1, x2, y2] = read_tuple::<usize, 4>();
        let [x1, y1, x2, y2] = [x1 - 1, y1 - 1, x2, y2];

        let udx1x2 = if x1 + 1 == x2 {
            vec![(1, 1, x1, x2)]
        } else {
            vec![
                (1, 0, x1, x1 + 1),
                (0, 0, x1 + 1, x2 - 1),
                (0, 1, x2 - 1, x2),
            ]
        };
        let lry1y2 = if y1 + 1 == y2 {
            vec![(1, 1, y1, y2)]
        } else {
            vec![
                (1, 0, y1, y1 + 1),
                (0, 0, y1 + 1, y2 - 1),
                (0, 1, y2 - 1, y2),
            ]
        };

        let mut ans = 0;
        for (u, d, x1, x2) in udx1x2 {
            for (l, r, y1, y2) in lry1y2.iter().copied() {
                ans += getsum(&dp[l][r][u][d], x1, y1, x2, y2);
            }
        }

        println!("{ans}")
    }
}

mod fio {
    use std::{
        cell::RefCell,
        convert::TryInto,
        fmt::Debug,
        io::{BufRead, BufWriter, StdinLock, StdoutLock, stdin, stdout},
        str::FromStr,
    };
    thread_local! {
        pub static STDIN: RefCell<StdinLock<'static>> = RefCell::new(stdin().lock());
        pub static STDOUT: RefCell<BufWriter<StdoutLock<'static>>> = RefCell::new(BufWriter::new(stdout().lock()));
    }

    #[allow(dead_code)]
    pub fn read<T: FromStr>() -> T
    where
        <T as FromStr>::Err: Debug,
    {
        read_line().parse().unwrap()
    }

    /// If there is a performance concern, use direct
    /// `read_line().spilt_ascii_whitespace().map(|x| x.parse().unwrap())`
    #[allow(dead_code)]
    pub fn read_vec<T: FromStr>() -> Vec<T>
    where
        <T as FromStr>::Err: Debug,
    {
        read_line()
            .split_ascii_whitespace()
            .map(|x| x.parse().unwrap())
            .collect()
    }

    /// If there is a performance concern, use direct
    /// `read_line().spilt_ascii_whitespace().map(|x| x.parse().unwrap())`
    #[allow(dead_code)]
    pub fn read_tuple<T, const N: usize>() -> [T; N]
    where
        T: FromStr + Debug,
        <T as FromStr>::Err: Debug,
    {
        read_vec::<T>().try_into().unwrap()
    }

    /// whitespace at the end of the line is ignored
    pub fn read_line() -> String {
        let mut s = String::new();
        STDIN.with(|cell| {
            cell.borrow_mut().read_line(&mut s).unwrap();
        });
        String::from_str(s.trim_end()).unwrap()
    }
}

#[macro_export]
macro_rules! print {
    ($($t:tt)*) => {
        fio::STDOUT.with(|cell|{
            use std::io::Write;
            write!(cell.borrow_mut(), $($t)*).unwrap()
        })};
}

#[macro_export]
macro_rules! println {
    ($($t:tt)*) => {
        fio::STDOUT.with(|cell| {
            use std::io::Write;
            writeln!(cell.borrow_mut(), $($t)*).unwrap()
        })
    };
}

#[macro_export]
macro_rules! flush {
    () => {
        fio::STDOUT.with(|cell| {
            use std::io::Write;
            cell.borrow_mut().flush().unwrap()
        });
    };
}
0