結果

問題 No.1623 三角形の制作
ユーザー ngtkanangtkana
提出日時 2021-07-23 21:42:24
言語 Rust
(1.77.0 + proconio)
結果
AC  
実行時間 92 ms / 2,000 ms
コード長 12,556 bytes
コンパイル時間 12,568 ms
コンパイル使用メモリ 390,344 KB
実行使用メモリ 21,064 KB
最終ジャッジ日時 2024-07-18 17:20:48
合計ジャッジ時間 14,661 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 62 ms
12,732 KB
testcase_03 AC 59 ms
12,056 KB
testcase_04 AC 59 ms
12,192 KB
testcase_05 AC 92 ms
20,076 KB
testcase_06 AC 31 ms
5,376 KB
testcase_07 AC 60 ms
12,232 KB
testcase_08 AC 34 ms
5,724 KB
testcase_09 AC 59 ms
11,564 KB
testcase_10 AC 81 ms
17,492 KB
testcase_11 AC 67 ms
13,796 KB
testcase_12 AC 26 ms
9,552 KB
testcase_13 AC 31 ms
9,436 KB
testcase_14 AC 29 ms
9,252 KB
testcase_15 AC 68 ms
20,808 KB
testcase_16 AC 68 ms
20,812 KB
testcase_17 AC 66 ms
20,928 KB
testcase_18 AC 71 ms
21,064 KB
testcase_19 AC 38 ms
12,552 KB
testcase_20 AC 1 ms
5,376 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
warning: unused imports: `Leaf`, `Tuple`, `VecLen`
   --> src/main.rs:114:27
    |
114 |             multi_token::{Leaf, Parser, ParserTuple, RawTuple, Tuple, VecLen},
    |                           ^^^^                                 ^^^^^  ^^^^^^
    |
    = note: `#[warn(unused_imports)]` on by default

warning: unused import: `with_str`
   --> src/main.rs:352:35
    |
352 |     pub use self::i::{with_stdin, with_str};
    |                                   ^^^^^^^^

warning: unused imports: `ParserTuple`, `Parser`, `RawTuple`, `Token`, `Usize1`
   --> src/main.rs:354:28
    |
354 |         pub use super::i::{Parser, ParserTuple, RawTuple, Token, Usize1};
    |                            ^^^^^^  ^^^^^^^^^^^  ^^^^^^^^  ^^^^^  ^^^^^^

ソースコード

diff #

#[allow(unused_imports)]
#[cfg(feature = "dbg")]
use dbg::lg;

fn main() {
    let mut buf = ngtio::with_stdin();
    let n = buf.usize();
    let mut a = count(&buf.vec::<usize>(n));
    accum::add(&mut a);
    a.insert(0, 0);
    let b = count(&buf.vec::<usize>(n));
    let c = count(&buf.vec::<usize>(n));
    let mut ans = 0;
    for j in 0..b.len() {
        for k in 0..c.len() {
            ans += b[j] * c[k] * (a[(j + k).min(a.len() - 1)] - a[j.max(k).min(a.len() - 1)]);
        }
    }
    println!("{}", ans);
}
fn count(a: &[usize]) -> Vec<usize> {
    let mut count = vec![0; a.iter().max().unwrap() + 1];
    a.iter().for_each(|&x| count[x] += 1);
    count
}

// accum {{{
#[allow(dead_code)]
mod accum {
    use std::{
        cmp::Ord,
        ops::{
            AddAssign, BitAndAssign, BitOrAssign, BitXorAssign, DivAssign, MulAssign, SubAssign,
        },
    };
    pub fn add<T: Copy + AddAssign>(a: &mut [T]) {
        for_each_mut(a, |&mut x, y| *y += x);
    }
    pub fn add_inv<T: Copy + SubAssign>(a: &mut [T]) {
        rfor_each_mut(a, |&mut x, y| *y -= x);
    }
    pub fn mul<T: Copy + MulAssign>(a: &mut [T]) {
        for_each_mut(a, |&mut x, y| *y *= x);
    }
    pub fn mul_inv<T: Copy + DivAssign>(a: &mut [T]) {
        rfor_each_mut(a, |&mut x, y| *y /= x);
    }
    // -- ord
    pub fn min<T: Copy + Ord>(a: &mut [T]) {
        for_each_mut(a, |&mut x, y| *y = x.min(*y));
    }
    pub fn max<T: Copy + Ord>(a: &mut [T]) {
        for_each_mut(a, |&mut x, y| *y = x.max(*y));
    }
    // --  bit
    pub fn xor<T: Copy + BitXorAssign>(a: &mut [T]) {
        for_each_mut(a, |&mut x, y| *y ^= x);
    }
    pub fn xor_inv<T: Copy + BitXorAssign>(a: &mut [T]) {
        rfor_each_mut(a, |&mut x, y| *y ^= x);
    }
    pub fn or<T: Copy + BitOrAssign>(a: &mut [T]) {
        for_each_mut(a, |&mut x, y| *y |= x);
    }
    pub fn and<T: Copy + BitAndAssign>(a: &mut [T]) {
        for_each_mut(a, |&mut x, y| *y &= x);
    }
    // -- for_each
    pub fn for_each<T>(a: &[T], mut f: impl FnMut(&T, &T)) {
        if !a.is_empty() {
            for i in 1..a.len() {
                let (left, right) = a.split_at(i);
                f(left.last().unwrap(), right.first().unwrap());
            }
        }
    }
    pub fn rfor_each<T>(a: &[T], mut f: impl FnMut(&T, &T)) {
        if !a.is_empty() {
            for i in (1..a.len()).rev() {
                let (left, right) = a.split_at(i);
                f(left.last().unwrap(), right.first().unwrap());
            }
        }
    }
    pub fn for_each_mut<T>(a: &mut [T], mut f: impl FnMut(&mut T, &mut T)) {
        if !a.is_empty() {
            for i in 1..a.len() {
                let (left, right) = a.split_at_mut(i);
                f(left.last_mut().unwrap(), right.first_mut().unwrap());
            }
        }
    }
    pub fn rfor_each_mut<T>(a: &mut [T], mut f: impl FnMut(&mut T, &mut T)) {
        if !a.is_empty() {
            for i in (1..a.len()).rev() {
                let (left, right) = a.split_at_mut(i);
                f(left.last_mut().unwrap(), right.first_mut().unwrap());
            }
        }
    }
}
// }}}
// template {{{
#[cfg(not(feature = "dbg"))]
#[allow(unused_macros)]
#[macro_export]
macro_rules! lg {
    ($($expr:expr),*) => {};
}
#[allow(dead_code)]
mod ngtio {
    mod i {
        pub use self::{
            multi_token::{Leaf, Parser, ParserTuple, RawTuple, Tuple, VecLen},
            token::{Token, Usize1},
        };
        use std::{
            io::{self, BufRead},
            iter,
        };
        pub fn with_stdin() -> Tokenizer<io::BufReader<io::Stdin>> {
            io::BufReader::new(io::stdin()).tokenizer()
        }
        pub fn with_str(src: &str) -> Tokenizer<&[u8]> {
            src.as_bytes().tokenizer()
        }
        pub struct Tokenizer<S: BufRead> {
            queue: Vec<String>, // FIXME: String のみにすると速そうです。
            scanner: S,
        }
        macro_rules! prim_method {
            ($name:ident: $T:ty) => {
                pub fn $name(&mut self) -> $T {
                    <$T>::leaf().parse(self)
                }
            };
            ($name:ident) => {
                prim_method!($name: $name);
            };
        }
        macro_rules! prim_methods {
            ($name:ident: $T:ty; $($rest:tt)*) => {
                prim_method!($name:$T);
                prim_methods!($($rest)*);
            };
            ($name:ident; $($rest:tt)*) => {
                prim_method!($name);
                prim_methods!($($rest)*);
            };
            () => ()
        }
        impl<S: BufRead> Tokenizer<S> {
            pub fn token(&mut self) -> String {
                self.load();
                self.queue.pop().expect("入力が終了したのですが。")
            }
            pub fn new(scanner: S) -> Self {
                Self {
                    queue: Vec::new(),
                    scanner,
                }
            }
            fn load(&mut self) {
                while self.queue.is_empty() {
                    let mut s = String::new();
                    let length = self.scanner.read_line(&mut s).unwrap(); // 入力が UTF-8 でないときにエラーだそうです。
                    if length == 0 {
                        break;
                    }
                    self.queue = s.split_whitespace().rev().map(str::to_owned).collect();
                }
            }
            pub fn skip_line(&mut self) {
                assert!(
                    self.queue.is_empty(),
                    "行の途中で呼ばないでいただきたいです。現在のトークンキュー: {:?}",
                    &self.queue
                );
                self.load();
            }
            pub fn end(&mut self) {
                self.load();
                assert!(self.queue.is_empty(), "入力はまだあります!");
            }
            pub fn parse<T: Token>(&mut self) -> T::Output {
                T::parse(&self.token())
            }
            pub fn parse_collect<T: Token, B>(&mut self, n: usize) -> B
            where
                B: iter::FromIterator<T::Output>,
            {
                iter::repeat_with(|| self.parse::<T>()).take(n).collect()
            }
            pub fn tuple<T: RawTuple>(&mut self) -> <T::LeafTuple as Parser>::Output {
                T::leaf_tuple().parse(self)
            }
            pub fn vec<T: Token>(&mut self, len: usize) -> Vec<T::Output> {
                T::leaf().vec(len).parse(self)
            }
            pub fn vec_tuple<T: RawTuple>(
                &mut self,
                len: usize,
            ) -> Vec<<T::LeafTuple as Parser>::Output> {
                T::leaf_tuple().vec(len).parse(self)
            }
            pub fn vec2<T: Token>(&mut self, height: usize, width: usize) -> Vec<Vec<T::Output>> {
                T::leaf().vec(width).vec(height).parse(self)
            }
            pub fn vec2_tuple<T>(
                &mut self,
                height: usize,
                width: usize,
            ) -> Vec<Vec<<T::LeafTuple as Parser>::Output>>
            where
                T: RawTuple,
            {
                T::leaf_tuple().vec(width).vec(height).parse(self)
            }
            prim_methods! {
                u8; u16; u32; u64; u128; usize;
                i8; i16; i32; i64; i128; isize;
                f32; f64;
                char; string: String;
            }
        }
        mod token {
            use super::multi_token::Leaf;
            use std::{any, fmt, marker, str};
            pub trait Token: Sized {
                type Output;
                fn parse(s: &str) -> Self::Output;
                fn leaf() -> Leaf<Self> {
                    Leaf(marker::PhantomData)
                }
            }
            impl<T> Token for T
            where
                T: str::FromStr,
                <Self as str::FromStr>::Err: fmt::Debug,
            {
                type Output = Self;
                fn parse(s: &str) -> Self::Output {
                    s.parse().unwrap_or_else(|_| {
                        panic!("Parse error!: ({}: {})", s, any::type_name::<Self>(),)
                    })
                }
            }
            pub struct Usize1 {}
            impl Token for Usize1 {
                type Output = usize;
                fn parse(s: &str) -> Self::Output {
                    usize::parse(s)
                        .checked_sub(1)
                        .expect("Parse error! (Zero substruction error of Usize1)")
                }
            }
        }
        mod multi_token {
            use super::{Token, Tokenizer};
            use std::{io::BufRead, iter, marker};
            pub trait Parser: Sized {
                type Output;
                fn parse<S: BufRead>(&self, server: &mut Tokenizer<S>) -> Self::Output;
                fn vec(self, len: usize) -> VecLen<Self> {
                    VecLen { len, elem: self }
                }
            }
            pub struct Leaf<T>(pub(super) marker::PhantomData<T>);
            impl<T: Token> Parser for Leaf<T> {
                type Output = T::Output;
                fn parse<S: BufRead>(&self, server: &mut Tokenizer<S>) -> T::Output {
                    server.parse::<T>()
                }
            }
            pub struct VecLen<T> {
                pub len: usize,
                pub elem: T,
            }
            impl<T: Parser> Parser for VecLen<T> {
                type Output = Vec<T::Output>;
                fn parse<S: BufRead>(&self, server: &mut Tokenizer<S>) -> Self::Output {
                    iter::repeat_with(|| self.elem.parse(server))
                        .take(self.len)
                        .collect()
                }
            }
            pub trait RawTuple {
                type LeafTuple: Parser;
                fn leaf_tuple() -> Self::LeafTuple;
            }
            pub trait ParserTuple {
                type Tuple: Parser;
                fn tuple(self) -> Self::Tuple;
            }
            pub struct Tuple<T>(pub T);
            macro_rules! impl_tuple {
                ($($t:ident: $T:ident),*) => {
                    impl<$($T),*> Parser for Tuple<($($T,)*)>
                        where
                            $($T: Parser,)*
                            {
                                type Output = ($($T::Output,)*);
#[allow(unused_variables)]
                                fn parse<S: BufRead >(&self, server: &mut Tokenizer<S>) -> Self::Output {
                                    match self {
                                        Tuple(($($t,)*)) => {
                                            ($($t.parse(server),)*)
                                        }
                                    }
                                }
                            }
                    impl<$($T: Token),*> RawTuple for ($($T,)*) {
                        type LeafTuple = Tuple<($(Leaf<$T>,)*)>;
                        fn leaf_tuple() -> Self::LeafTuple {
                            Tuple(($($T::leaf(),)*))
                        }
                    }
                    impl<$($T: Parser),*> ParserTuple for ($($T,)*) {
                        type Tuple = Tuple<($($T,)*)>;
                        fn tuple(self) -> Self::Tuple {
                            Tuple(self)
                        }
                    }
                };
            }
            impl_tuple!();
            impl_tuple!(t1: T1);
            impl_tuple!(t1: T1, t2: T2);
            impl_tuple!(t1: T1, t2: T2, t3: T3);
            impl_tuple!(t1: T1, t2: T2, t3: T3, t4: T4);
            impl_tuple!(t1: T1, t2: T2, t3: T3, t4: T4, t5: T5);
            impl_tuple!(t1: T1, t2: T2, t3: T3, t4: T4, t5: T5, t6: T6);
            impl_tuple!(t1: T1, t2: T2, t3: T3, t4: T4, t5: T5, t6: T6, t7: T7);
            impl_tuple!(
                t1: T1,
                t2: T2,
                t3: T3,
                t4: T4,
                t5: T5,
                t6: T6,
                t7: T7,
                t8: T8
            );
        }
        trait Scanner: BufRead + Sized {
            fn tokenizer(self) -> Tokenizer<Self> {
                Tokenizer::new(self)
            }
        }
        impl<R: BufRead> Scanner for R {}
    }
    pub use self::i::{with_stdin, with_str};
    mod prelude {
        pub use super::i::{Parser, ParserTuple, RawTuple, Token, Usize1};
    }
}
// }}}
0