/* * */ fn main() { input! { h: usize, w: usize, a: [[u32; w]; h], } let t = a .iter() .flatten() .fold(0u32, |acc, &x| acc.overflowing_add(x).0); let ans = a .iter() .map(|ai| { (ai.iter().fold(0u32, |acc, &x| acc.overflowing_add(x).0)) .overflowing_add(t) .0 .to_string() }) .collect::>() .join("\n"); println!("{}", ans); } mod kyopro_lib { #![allow(unused_imports, dead_code)] pub mod io { use std::cell::RefCell; use std::collections::VecDeque; use std::io::{BufRead, Read}; // ─── TokenReader トレイト ───────────────────────────────────────────────────── pub trait TokenReader { fn next_token(&mut self) -> String; } // ─── Scanner (非インタラクティブ) ───────────────────────────────────────────── pub struct Scanner { tokens: VecDeque, } impl Scanner { fn new() -> Self { let mut s = String::new(); std::io::stdin().lock().read_to_string(&mut s).unwrap(); Scanner { tokens: s.split_whitespace().map(String::from).collect(), } } } impl TokenReader for Scanner { fn next_token(&mut self) -> String { self.tokens.pop_front().expect("input exhausted") } } thread_local! { #[doc(hidden)] pub static __SCANNER: RefCell = RefCell::new(Scanner::new()); } // ─── InteractiveScanner ─────────────────────────────────────────────────────── /// インタラクティブ問題用スキャナ。トークンを1行ずつ遅延読み込みする。 /// stdin のロックを保持し続けることで毎回のロック取得コストを避ける。 pub struct InteractiveScanner { reader: std::io::BufReader>, tokens: VecDeque, } impl InteractiveScanner { fn new() -> Self { InteractiveScanner { reader: std::io::BufReader::new(std::io::stdin().lock()), tokens: VecDeque::new(), } } } impl TokenReader for InteractiveScanner { fn next_token(&mut self) -> String { while self.tokens.is_empty() { let mut line = String::new(); let n = self.reader.read_line(&mut line).unwrap(); if n == 0 { panic!("input exhausted (EOF)"); } self.tokens .extend(line.split_whitespace().map(String::from)); } self.tokens.pop_front().expect("input exhausted") } } thread_local! { #[doc(hidden)] pub static __INTERACTIVE_SCANNER: RefCell = RefCell::new(InteractiveScanner::new()); } // ─── Readable トレイト ──────────────────────────────────────────────────────── pub trait Readable { type Value; fn read(scanner: &mut R) -> Self::Value; } macro_rules! impl_readable_from_str { ($($t:ty),+ $(,)?) => {$( impl Readable for $t { type Value = $t; fn read(scanner: &mut R) -> $t { scanner.next_token().parse().expect("parse error") } } )+}; } impl_readable_from_str!( i8, i16, i32, i64, i128, isize, u8, u16, u32, u64, u128, usize, f32, f64, String, char, bool, ); // ─── 特殊型マーカー ─────────────────────────────────────────────────────────── /// 1-indexed で読んで 0-indexed `usize` に変換。 pub enum Usize1 {} impl Readable for Usize1 { type Value = usize; fn read(scanner: &mut R) -> usize { usize::read(scanner).wrapping_sub(1) } } /// 1-indexed で読んで 0-indexed `isize` に変換。 pub enum Isize1 {} impl Readable for Isize1 { type Value = isize; fn read(scanner: &mut R) -> isize { isize::read(scanner) - 1 } } /// トークンを `Vec` として読む。 pub enum Chars {} impl Readable for Chars { type Value = Vec; fn read(scanner: &mut R) -> Vec { String::read(scanner).chars().collect() } } /// トークンを `Vec` として読む。 pub enum Bytes {} impl Readable for Bytes { type Value = Vec; fn read(scanner: &mut R) -> Vec { String::read(scanner).into_bytes() } } // ─── タプル実装 ─────────────────────────────────────────────────────────────── macro_rules! impl_readable_tuple { ($($T:ident),+) => { impl<$($T: Readable),+> Readable for ($($T,)+) { type Value = ($($T::Value,)+); fn read(scanner: &mut R) -> Self::Value { ($($T::read(scanner),)+) } } }; } impl_readable_tuple!(A, B); impl_readable_tuple!(A, B, C); impl_readable_tuple!(A, B, C, D); impl_readable_tuple!(A, B, C, D, E); impl_readable_tuple!(A, B, C, D, E, F); impl_readable_tuple!(A, B, C, D, E, F, G); impl_readable_tuple!(A, B, C, D, E, F, G, H); // ─── マクロ ─────────────────────────────────────────────────────────────────── // // `#[macro_export]` + `kyopro_lib::` を使う。 // expander が `kyopro_lib::` → `kyopro_lib::` に書き換え、 // `pub use super::MACRO;` を mod kyopro_lib に追加するため // インライン展開後も正しく解決される。 // // `&mut *__s.borrow_mut()` の `*` は RefMut を Scanner に deref するため必要。 // Readable::read が TokenReader でジェネリックになったことで、 // &mut RefMut では TokenReader を満たせないため。 /// proconio 互換の入力マクロ。非インタラクティブ問題用(全入力を一括読み込み)。 /// /// # 使い方 /// ```rust /// use kyopro_lib::{input, io::{Usize1, Chars}}; /// /// input! { /// n: usize, /// m: usize, /// a: [i64; n], // → Vec /// b: [Usize1; m], // → Vec(0-indexed) /// s: Chars, // → Vec /// t: (usize, i64), // → タプル /// } /// ``` #[macro_export] macro_rules! input { (@parse) => {}; (@parse ,) => {}; (@parse mut $name:ident : [[$t:ty ; $m:expr] ; $n:expr], $($rest:tt)*) => { let mut $name: ::std::vec::Vec<::std::vec::Vec<_>> = (0..$n) .map(|_| { (0..$m) .map(|_| kyopro_lib::io::__SCANNER.with(|__s| { <$t as kyopro_lib::io::Readable>::read(&mut *__s.borrow_mut()) })) .collect() }) .collect(); kyopro_lib::input!(@parse $($rest)*); }; (@parse $name:ident : [[$t:ty ; $m:expr] ; $n:expr], $($rest:tt)*) => { let $name: ::std::vec::Vec<::std::vec::Vec<_>> = (0..$n) .map(|_| { (0..$m) .map(|_| kyopro_lib::io::__SCANNER.with(|__s| { <$t as kyopro_lib::io::Readable>::read(&mut *__s.borrow_mut()) })) .collect() }) .collect(); kyopro_lib::input!(@parse $($rest)*); }; (@parse mut $name:ident : [$t:ty ; $n:expr], $($rest:tt)*) => { let mut $name: ::std::vec::Vec<_> = (0..$n) .map(|_| kyopro_lib::io::__SCANNER.with(|__s| { <$t as kyopro_lib::io::Readable>::read(&mut *__s.borrow_mut()) })) .collect(); kyopro_lib::input!(@parse $($rest)*); }; (@parse $name:ident : [$t:ty ; $n:expr], $($rest:tt)*) => { let $name: ::std::vec::Vec<_> = (0..$n) .map(|_| kyopro_lib::io::__SCANNER.with(|__s| { <$t as kyopro_lib::io::Readable>::read(&mut *__s.borrow_mut()) })) .collect(); kyopro_lib::input!(@parse $($rest)*); }; (@parse mut $name:ident : $t:ty, $($rest:tt)*) => { let mut $name = kyopro_lib::io::__SCANNER.with(|__s| { <$t as kyopro_lib::io::Readable>::read(&mut *__s.borrow_mut()) }); kyopro_lib::input!(@parse $($rest)*); }; (@parse $name:ident : $t:ty, $($rest:tt)*) => { let $name = kyopro_lib::io::__SCANNER.with(|__s| { <$t as kyopro_lib::io::Readable>::read(&mut *__s.borrow_mut()) }); kyopro_lib::input!(@parse $($rest)*); }; ($($tt:tt)*) => { kyopro_lib::input!(@parse $($tt)*,) }; } pub use input; /// インタラクティブ問題用入力マクロ。`input!` と同じ構文で、1行ずつ遅延読み込みする。 /// /// 出力後は必ず `flush!()` を呼ぶこと。 /// /// # 使い方 /// ```rust /// use kyopro_lib::{iinput, flush, io::Usize1}; /// /// println!("{}", query); /// flush!(); /// iinput! { response: i64 } /// ``` #[macro_export] macro_rules! iinput { (@parse) => {}; (@parse ,) => {}; (@parse mut $name:ident : [[$t:ty ; $m:expr] ; $n:expr], $($rest:tt)*) => { let mut $name: ::std::vec::Vec<::std::vec::Vec<_>> = (0..$n) .map(|_| { (0..$m) .map(|_| kyopro_lib::io::__INTERACTIVE_SCANNER.with(|__s| { <$t as kyopro_lib::io::Readable>::read(&mut *__s.borrow_mut()) })) .collect() }) .collect(); kyopro_lib::iinput!(@parse $($rest)*); }; (@parse $name:ident : [[$t:ty ; $m:expr] ; $n:expr], $($rest:tt)*) => { let $name: ::std::vec::Vec<::std::vec::Vec<_>> = (0..$n) .map(|_| { (0..$m) .map(|_| kyopro_lib::io::__INTERACTIVE_SCANNER.with(|__s| { <$t as kyopro_lib::io::Readable>::read(&mut *__s.borrow_mut()) })) .collect() }) .collect(); kyopro_lib::iinput!(@parse $($rest)*); }; (@parse mut $name:ident : [$t:ty ; $n:expr], $($rest:tt)*) => { let mut $name: ::std::vec::Vec<_> = (0..$n) .map(|_| kyopro_lib::io::__INTERACTIVE_SCANNER.with(|__s| { <$t as kyopro_lib::io::Readable>::read(&mut *__s.borrow_mut()) })) .collect(); kyopro_lib::iinput!(@parse $($rest)*); }; (@parse $name:ident : [$t:ty ; $n:expr], $($rest:tt)*) => { let $name: ::std::vec::Vec<_> = (0..$n) .map(|_| kyopro_lib::io::__INTERACTIVE_SCANNER.with(|__s| { <$t as kyopro_lib::io::Readable>::read(&mut *__s.borrow_mut()) })) .collect(); kyopro_lib::iinput!(@parse $($rest)*); }; (@parse mut $name:ident : $t:ty, $($rest:tt)*) => { let mut $name = kyopro_lib::io::__INTERACTIVE_SCANNER.with(|__s| { <$t as kyopro_lib::io::Readable>::read(&mut *__s.borrow_mut()) }); kyopro_lib::iinput!(@parse $($rest)*); }; (@parse $name:ident : $t:ty, $($rest:tt)*) => { let $name = kyopro_lib::io::__INTERACTIVE_SCANNER.with(|__s| { <$t as kyopro_lib::io::Readable>::read(&mut *__s.borrow_mut()) }); kyopro_lib::iinput!(@parse $($rest)*); }; ($($tt:tt)*) => { kyopro_lib::iinput!(@parse $($tt)*,) }; } pub use iinput; /// 型を指定して1トークン読む(非インタラクティブ用)。 #[macro_export] macro_rules! read { ($t:ty) => { kyopro_lib::io::__SCANNER.with(|__s| <$t as kyopro_lib::io::Readable>::read(&mut *__s.borrow_mut())) }; } pub use read; /// 型を指定して1トークン読む(インタラクティブ用)。 #[macro_export] macro_rules! iread { ($t:ty) => { kyopro_lib::io::__INTERACTIVE_SCANNER .with(|__s| <$t as kyopro_lib::io::Readable>::read(&mut *__s.borrow_mut())) }; } pub use iread; // ─── インタラクティブ用出力バッファ ────────────────────────────────────────── thread_local! { #[doc(hidden)] pub static __STDOUT_BUF: RefCell>> = RefCell::new(std::io::BufWriter::with_capacity( 1 << 20, std::io::stdout().lock(), )); } /// インタラクティブ問題用の `print!`。`flush!()` するまでバッファに貯める。 #[macro_export] macro_rules! iprint { ($($arg:tt)*) => {{ use ::std::io::Write as _; kyopro_lib::io::__STDOUT_BUF.with(|__w| { ::std::write!(__w.borrow_mut(), $($arg)*).unwrap(); }); }}; } pub use iprint; /// インタラクティブ問題用の `println!`。`flush!()` するまでバッファに貯める。 #[macro_export] macro_rules! iprintln { () => { kyopro_lib::iprint!("\n") }; ($($arg:tt)*) => {{ use ::std::io::Write as _; kyopro_lib::io::__STDOUT_BUF.with(|__w| { ::std::writeln!(__w.borrow_mut(), $($arg)*).unwrap(); }); }}; } pub use iprintln; /// バッファを flush してジャッジに送信する(インタラクティブ問題用)。 /// `BufWriter::flush` が内部の `StdoutLock` も flush するので 1 回で十分。 #[macro_export] macro_rules! flush { () => {{ use ::std::io::Write as _; kyopro_lib::io::__STDOUT_BUF.with(|__w| { __w.borrow_mut().flush().unwrap(); }); }}; } pub use flush; /// 高速出力用 `BufWriter` をセットアップする。 /// /// ```rust /// fastout!(out, { /// use std::io::Write; /// writeln!(out, "{}", answer).unwrap(); /// }); /// ``` #[macro_export] macro_rules! fastout { ($out:ident, $body:block) => { let __stdout = ::std::io::stdout(); let mut $out = ::std::io::BufWriter::new(__stdout.lock()); $body }; } pub use fastout; } pub use crate::input; pub use crate::iinput; pub use crate::read; pub use crate::iread; pub use crate::iprint; pub use crate::iprintln; pub use crate::flush; pub use crate::fastout; }