結果

問題 No.440 2次元チワワ問題
ユーザー koba-e964koba-e964
提出日時 2021-09-29 09:53:16
言語 Rust
(1.77.0)
結果
RE  
実行時間 -
コード長 3,839 bytes
コンパイル時間 1,597 ms
コンパイル使用メモリ 162,404 KB
実行使用メモリ 34,960 KB
最終ジャッジ日時 2023-09-23 07:51:54
合計ジャッジ時間 9,439 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 RE -
testcase_02 RE -
testcase_03 AC 1 ms
4,380 KB
testcase_04 RE -
testcase_05 RE -
testcase_06 RE -
testcase_07 RE -
testcase_08 RE -
testcase_09 RE -
testcase_10 RE -
testcase_11 RE -
testcase_12 RE -
testcase_13 RE -
testcase_14 RE -
testcase_15 RE -
testcase_16 AC 5 ms
4,380 KB
testcase_17 AC 306 ms
34,948 KB
testcase_18 AC 302 ms
34,956 KB
testcase_19 AC 303 ms
34,908 KB
testcase_20 AC 295 ms
34,944 KB
testcase_21 AC 934 ms
34,948 KB
testcase_22 AC 950 ms
34,912 KB
testcase_23 AC 936 ms
34,960 KB
testcase_24 AC 936 ms
34,908 KB
testcase_25 AC 903 ms
34,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#[allow(unused_imports)]
use std::cmp::*;
#[allow(unused_imports)]
use std::collections::*;
use std::io::{Write, BufWriter};
// https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8
macro_rules! input {
    ($($r:tt)*) => {
        let stdin = std::io::stdin();
        let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin.lock()));
        let mut next = move || -> String{
            bytes.by_ref().map(|r|r.unwrap() as char)
                .skip_while(|c|c.is_whitespace())
                .take_while(|c|!c.is_whitespace())
                .collect()
        };
        input_inner!{next, $($r)*}
    };
}

macro_rules! input_inner {
    ($next:expr) => {};
    ($next:expr,) => {};
    ($next:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($next, $t);
        input_inner!{$next $($r)*}
    };
}

macro_rules! read_value {
    ($next:expr, ( $($t:tt),* )) => { ($(read_value!($next, $t)),*) };
    ($next:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
    };
    ($next:expr, chars) => {
        read_value!($next, String).chars().collect::<Vec<char>>()
    };
    ($next:expr, usize1) => (read_value!($next, usize) - 1);
    ($next:expr, [ $t:tt ]) => {{
        let len = read_value!($next, usize);
        read_value!($next, [$t; len])
    }};
    ($next:expr, $t:ty) => ($next().parse::<$t>().expect("Parse error"));
}

trait Change { fn chmax(&mut self, x: Self); fn chmin(&mut self, x: Self); }
impl<T: PartialOrd> Change for T {
    fn chmax(&mut self, x: T) { if *self < x { *self = x; } }
    fn chmin(&mut self, x: T) { if *self > x { *self = x; } }
}

struct Data {
    cww: Vec<i64>,
    w: Vec<i64>,
    c: Vec<i64>,
    cw: Vec<i64>,
}

impl Data {
    fn with(s: &[char]) -> Self {
        let n = s.len();
        let mut cww = vec![0; n + 1];
        let mut c = vec![0; n + 1];
        let mut w = vec![0; n + 1];
        let mut cw = vec![0; n + 1];
        for i in 0..n {
            cww[i + 1] = cww[i];
            c[i + 1] = c[i];
            w[i + 1] = w[i];
            cw[i + 1] = cw[i];
            if s[i] == 'c' {
                c[i + 1] += 1;
            } else {
                w[i + 1] += 1;
                cw[i + 1] += c[i];
                cww[i + 1] += cw[i];
            }
        }
        Data {
            cww: cww,
            w: w,
            c: c,
            cw: cw,
        }
    }
    // Counts cww.
    fn query(&self, a: usize, b: usize) -> i64 {
        let mut ans = self.cww[b] - self.cww[a];
        let rw = self.w[b] - self.w[a];
        ans -= self.c[a] * rw * (rw - 1) / 2;
        ans -= self.cw[a] * rw;
        ans
    }
}

fn main() {
    let out = std::io::stdout();
    let mut out = BufWriter::new(out.lock());
    macro_rules! puts {($($format:tt)*) => (let _ = write!(out,$($format)*););}
    input! {
        h: usize, w: usize,
        s: [chars; h],
        q: usize,
        abcd: [(usize1, usize1, usize, usize); q],
    }
    let mut row = vec![];
    let mut row_rev = vec![];
    for i in 0..h {
        row.push(Data::with(&s[i]));
        let mut tmp = s[i].to_vec();
        tmp.reverse();
        row_rev.push(Data::with(&tmp));
    }
    let mut col = vec![];
    let mut col_rev = vec![];
    for i in 0..w {
        let mut tmp = vec!['+'; h];
        for j in 0..h {
            tmp[j] = s[j][i];
        }
        col.push(Data::with(&tmp));
        tmp.reverse();
        col_rev.push(Data::with(&tmp));
    }
    for (a, b, c, d) in abcd {
        let mut ans = 0;
        for i in a..c {
            ans += row[i].query(b, d);
            ans += row_rev[i].query(w - d, w - b);
        }
        for i in b..d {
            ans += col[i].query(a, c);
            ans += col_rev[i].query(w - c, w - a);
        }
        puts!("{}\n", ans);
    }
}
0