use std::collections::HashMap; use std::hash::Hash; use proconio::input; fn value_and_count(arr: &[T]) -> HashMap { let mut ans = Vec::new(); let mut count = 0; let mut current: Option<&T> = None; for val in arr { match current { Some(cur) if cur == val => { }, _ => { if let Some(cur) = current { ans.push((cur.clone(), count)); } current = Some(val); }, } count += 1; } if let Some(cur) = current { ans.push((cur.clone(), count)); } ans.into_iter().collect::>() } fn main () { input! { n: usize, q: usize, a: [usize; n], } let mut acopy = a.clone(); acopy.sort(); let range_people = value_and_count(&acopy); let num_range = a.iter() .enumerate() .map(|(i, &x)| { (i+1, x) }) .collect::>(); //println!("{:?}",num_range); //println!("{:?}",range_people); for _ in 0..q { input! { x: usize, y: usize, } let x = *num_range.get(&x).unwrap(); let x = *range_people.get(&x).unwrap() as i64; let y = *num_range.get(&y).unwrap(); let y = *range_people.get(&y).unwrap() as i64; println!("{}", if x-y-1 < 0 { 0 } else {x-y-1}); } }