use std::io::Read; use std::collections::{HashMap, BinaryHeap}; fn main() { const MIN_VAL: isize = -10_000_000; let mut all_data = String::new(); std::io::stdin().read_to_string(&mut all_data).ok(); let all_data: Vec<&str> = all_data.trim().split('\n').map(|s| s.trim()).collect(); let nm: Vec = all_data.iter().next().unwrap().split_whitespace().map(|i| i.parse::().unwrap()).collect(); let n: isize = nm[0]; let m: isize = nm[1]; let mut idx2kmc: HashMap = HashMap::new(); let mut starts: BinaryHeap = BinaryHeap::new(); let mut ends: BinaryHeap = BinaryHeap::new(); let mut mapping: Vec> = vec![vec![]; (n as usize)+2]; all_data.iter().enumerate().skip(1).take(m as usize).for_each(|pair| { let mark_val = pair.0 as isize; let values: Vec<&str> = (pair.1).split_whitespace().collect(); let start: usize = values[0].parse::().unwrap(); let end: usize = values[1].parse::().unwrap(); let keyword: String = values[2].to_string(); idx2kmc.insert(mark_val as usize, keyword); mapping[start].push(mark_val); mapping[end+1].push(-mark_val); }); let mut result_map: Vec> = vec![None; n as usize]; let mut current_val: isize = MIN_VAL; mapping.iter().skip(1).take(n as usize).enumerate().for_each(|pair| { let start_ends = pair.1; if !start_ends.is_empty() { start_ends.iter().for_each(|se| { if se > &0isize { starts.push(-se); } else { ends.push(*se); } }); } if !ends.is_empty() { if ends.peek().unwrap() == ¤t_val { current_val = MIN_VAL; ends.pop(); } } while !starts.is_empty() && !ends.is_empty() { if starts.peek().unwrap() != ends.peek().unwrap() { break; } else { starts.pop(); ends.pop(); } } if !starts.is_empty() { if starts.peek().unwrap() > ¤t_val { if current_val > MIN_VAL { starts.push(current_val); } current_val = starts.pop().unwrap(); } } let v = (-current_val) as usize; result_map[pair.0] = idx2kmc.get(&v).map(|i| i.to_string()); }); let mut y = 0; let mut k = 0; let mut c = 0; result_map.iter().for_each(|r| { match r { Some(x) => { match x.as_str() { "Y" => { y += 1; }, "K" => { k += 1; }, "C" => { c += 1; }, _ => { }, } }, None => { }, } }); println!("{} {} {}", y, k, c); }