heh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#![allow(
    confusable_idents,
    uncommon_codepoints,
    mixed_script_confusables,
    incomplete_features
)]
#![feature(
    generic_const_exprs,
    maybe_uninit_uninit_array,
    inline_const,
    slice_flatten,
    iter_collect_into,
    let_chains,
    anonymous_lifetime_in_impl_trait,
    unchecked_math,
    array_windows,
    slice_take,
    test,
    slice_as_chunks,
    array_chunks,
    slice_split_once,
    byte_slice_trim_ascii
)]
extern crate test;
pub mod util;

pub use util::prelude::*;

#[derive(Copy, Clone)]
struct Piece {
    a: [u16; 3],
    b: [u16; 3],
}
pub fn run(i: &str) -> impl Display {
    let mut pieces = vec![];
    let mut i = i.as_bytes();
    while i.len() > 0 {
        pieces.push(Piece {
            a: 読む::迄::<u16>(&mut i, b',')
                .and(読む::迄(&mut i, b','))
                .and(読む::迄(&mut i, b'~')),
            b: 読む::迄::<u16>(&mut i, b',')
                .and(読む::迄(&mut i, b','))
                .and(読む::迄または完了(&mut i, b'\n')),
        });
    }
    pieces.sort_unstable_by(|a, b| a.a[2].cmp(&b.a[2]));
    let mut m: HashMap<(u16, u16), u16> = HashMap::new();
    for p in pieces.iter_mut() {
        let a = (p.a[0]..=p.b[0]).flat_map(|x| (p.a[1]..=p.b[1]).map(move |y| (x, y)));
        let k = a.clone().map(|x| *m.get(&x).unwrap_or(&0)).max().unwrap() + 1;
        for e in a {
            m.insert(e, k + p.b[2] - p.a[2]);
        }
        *p = Piece {
            a: p.a.trunc().and(k),
            b: p.b.trunc().and(k + p.b[2] - p.a[2]),
        };
    }
    (0..pieces.len())
        .map(|i| {
            let mut m: HashMap<(u16, u16), u16> = HashMap::new();
            let mut n = 0;
            for (p, j) in pieces.clone().iter_mut().ι::<usize>() {
                if j == i {
                    continue;
                }
                let a = (p.a[0]..=p.b[0]).flat_map(|x| (p.a[1]..=p.b[1]).map(move |y| (x, y)));
                let k = a.clone().map(|x| *m.get(&x).unwrap_or(&0)).max().unwrap() + 1;
                for e in a {
                    m.insert(e, k + p.b[2] - p.a[2]);
                }
                let z = p.a[2];
                *p = Piece {
                    a: p.a.trunc().and(k),
                    b: p.b.trunc().and(k + p.b[2] - p.a[2]),
                };

                n += (k < z) as u16;
            }
            n as u64
        })
        .sum::<u64>()
}

fn main() {
    let i = include_str!("inp.txt").trim();
    println!("{}", run(i));
}

#[bench]
fn bench(b: &mut test::Bencher) {
    let i = boxd(include_str!("inp.txt").trim());
    b.iter(|| run(i));
}