https://atcoder.jp/contests/math-and-algorithm/tasks/math_and_algorithm_al
出勤時間と退勤時間をマージしてソートしておくと、そのあとは線形時間で時刻ごとの人数が得られます。
// Convenience Store 2 #![allow(non_snake_case)] //////////////////// library //////////////////// fn read<T: std::str::FromStr>() -> T { let mut line = String::new(); std::io::stdin().read_line(&mut line).ok(); line.trim().parse().ok().unwrap() } fn read_vec<T: std::str::FromStr>() -> Vec<T> { read::<String>().split_whitespace() .map(|e| e.parse().ok().unwrap()).collect() } //////////////////// process //////////////////// fn read_input() -> (usize, Vec<(usize, usize)>) { let T: usize = read(); let N: usize = read(); let intervals = (0..N).map(|_| read_vec()).map(|v| (v[0], v[1])). collect::<Vec<(usize, usize)>>(); (T, intervals) } fn sort(intervals: Vec<(usize, usize)>) -> Vec<(usize, i32)> { let mut v = intervals.into_iter(). map(|(L, R)| vec![(L, 1), (R, -1)]). flatten().collect::<Vec<(usize, i32)>>(); v.sort(); v } fn f(T: usize, intervals: Vec<(usize, usize)>) { let moves = sort(intervals); let mut num_workers: i32 = 0; let mut i = 0; for t in 0..T { while i < moves.len() && t == moves[i].0 { num_workers += moves[i].1; i += 1 } println!("{}", num_workers) } } fn main() { let (T, intervals) = read_input(); f(T, intervals) }