結果

問題 No.3806 Crossword Key with Cropping (Easy)
コンテスト
ユーザー surumeika1987
提出日時 2026-11-25 12:26:35
言語 Rust
(1.99.1 + proconio + num + itertools + ACL)
コンパイル:
/usr/bin/rustc_custom
実行:
./target/release/main
結果
AC  
実行時間 739 ms / 2,000 ms
+ 817µs
コード長 3,550 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 7,149 ms
コンパイル使用メモリ 180,176 KB
実行使用メモリ 9,924 KB
最終ジャッジ日時 2026-11-25 12:27:05
合計ジャッジ時間 29,036 ms
ジャッジサーバーID
(参考情報)
judge4_0 / judge5_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 53
権限があれば一括ダウンロードができます
コンパイルメッセージ
warning: unnecessary parentheses around `if` condition
  --> src/main.rs:36:16
   |
36 |             if (y1 != y2) {
   |                ^        ^
   |
   = note: `#[warn(unused_parens)]` (part of `#[warn(unused)]`) on by default
help: remove these parentheses
   |
36 -             if (y1 != y2) {
36 +             if y1 != y2 {
   |

warning: unnecessary parentheses around `if` condition
  --> src/main.rs:47:16
   |
47 |             if (x1 != x2) {
   |                ^        ^
   |
help: remove these parentheses
   |
47 -             if (x1 != x2) {
47 +             if x1 != x2 {
   |

warning: unused variable: `h`
   --> src/main.rs:104:11
    |
104 | fn is_key(h: u32, w: u32, v: &Vec<bool>, y1: u32, x1: u32, y2: u32, x2: u32, y3: u32, x3: u32) -> bool {
    |           ^ help: if this is intentional, prefix it with an underscore: `_h`
    |
    = note: `#[warn(unused_variables)]` (part of `#[warn(unused)]`) on by default

warning: unused variable: `h`
   --> src/main.rs:120:14
    |
120 | fn range_sum(h: u32, w: u32, v: &Vec<u32>, y1: u32, x1: u32, y2: u32, x2: u32) -> u32{
    |              ^ help: if this is intentional, prefix it with an underscore: `_h`

ソースコード

diff #
raw source code

use std::io::stdin;

fn main() {
    let (h, w) = input_size();
    let board = input_board(h, w);

    let q = input_num();
    
    let mut board_key_sum: Vec<u32> = vec![0; ((w + 2) * (h + 2)) as usize];
    
    for i in 1..h+1 {
        for j in 1..w+1 {
            let current = if is_key(h, w, &board, 1, 1, h, w, i, j) {
                1
            } else {
                0
            };
            let up = board_key_sum[((i - 1) * (w + 2) + j) as usize];
            let left = board_key_sum[(i * (w + 2) + (j - 1)) as usize];
            let d = board_key_sum[((i - 1) * (w + 2) + (j - 1)) as usize];
            board_key_sum[(i * (w + 2) + j) as usize] = up + left - d + current;
        }
    }

    for _ in 0..q {
        let (y1, x1, y2, x2) = input_query();
        let mut count = 0;
        if 2 <= y2 - y1 && 2 <= x2 - x1 {
            count += range_sum(h, w, &board_key_sum, y1 + 1, x1 + 1, y2 - 1, x2 - 1);
        }
        // 上下
        for i in x1..x2 + 1 {
            if is_key(h, w, &board, y1, x1, y2, x2, y1, i) {
                count += 1;
            }
            if (y1 != y2) {
                if is_key(h, w, &board, y1, x1, y2, x2, y2, i) {
                    count += 1;
                }
            }
        }
        // 左右
        for i in y1 + 1..y2 {
            if is_key(h, w, &board, y1, x1, y2, x2, i, x1) {
                count += 1;
            }
            if (x1 != x2) {
                if is_key(h, w, &board, y1, x1, y2, x2, i, x2) {
                    count += 1;
                }
            }
        }
        
        println!("{}", count);
    }
}

fn input_size() -> (u32, u32) {
    let mut a = String::new();
    stdin().read_line(&mut a).unwrap();

    let v: Vec<u32> = a
        .split_whitespace()
        .map(|e| e.parse().unwrap())
        .collect();

    (v[0], v[1])
}

fn input_board(h: u32, w: u32) -> Vec<bool> {
    let mut board = vec![false; ((w + 2) * (h + 2)) as usize];

    for y in 0..h {
        let mut a = String::new();
        stdin().read_line(&mut a).unwrap();

        for (x, c) in a.trim().chars().enumerate() {
            board[(y + 1) as usize * (w + 2) as usize + (x + 1)] = c == '.';
        }
    }

    board
}

fn input_num() -> u32 {
    let mut a = String::new();
    stdin().read_line(&mut a).unwrap();

    a.trim().parse().unwrap()
}

fn input_query() -> (u32, u32, u32, u32) {
    let mut a = String::new();
    stdin().read_line(&mut a).unwrap();

    let v: Vec<u32> = a
        .split_whitespace()
        .map(|e| e.parse().unwrap())
        .collect();

    (v[0], v[1], v[2], v[3])
}

fn is_key(h: u32, w: u32, v: &Vec<bool>, y1: u32, x1: u32, y2: u32, x2: u32, y3: u32, x3: u32) -> bool {
    if !v[(y3 * (w + 2) + x3) as usize] {
        return false;
    }
    let up      = y1 < y3 && v[((y3 - 1) * (w + 2) + x3) as usize];
    let down    = y3 < y2 && v[((y3 + 1) * (w + 2) + x3) as usize];
    let left    = x1 < x3 && v[(y3 * (w + 2) + (x3 - 1)) as usize];
    let right   = x3 < x2 && v[(y3 * (w + 2) + (x3 + 1)) as usize];
    
    let vertical = !up && down;
    let horizontal = !left && right;
    let dot = !(up || down || left || right);
    
    vertical || horizontal || dot
}

fn range_sum(h: u32, w: u32, v: &Vec<u32>, y1: u32, x1: u32, y2: u32, x2: u32) -> u32{
    let c = v[(y2 * (w + 2) + x2) as usize];
    let up = v[((y1 - 1) * (w + 2) + x2) as usize];
    let left = v[(y2 * (w + 2) + (x1 - 1)) as usize];
    let d = v[((y1 - 1) * (w + 2) + (x1 - 1)) as usize];
    c + d - up - left
}
0