use std::collections::{BTreeSet, HashMap}; fn main() { let mut temp = String::new(); std::io::stdin().read_line(&mut temp).ok(); let temp: Vec = temp.trim().split_whitespace().map(|s| s.parse().unwrap()).collect(); let n = temp[0]; let a = temp[1]; let mut x = String::new(); std::io::stdin().read_line(&mut x).ok(); let x: Vec = x.trim().split_whitespace().map(|s| s.parse().unwrap()).collect(); let mut t = String::new(); std::io::stdin().read_line(&mut t).ok(); let t: usize = t.trim().parse().unwrap(); let lrs = (0..t).map(|_| { let mut temp = String::new(); std::io::stdin().read_line(&mut temp).ok(); let temp: Vec = temp.trim().split_whitespace().map(|s| s.parse().unwrap()).collect(); (temp[0], temp[1]) }) .collect::>(); let mut result = vec![-1isize; n]; let mut order = (0..n).collect::>(); order.sort_by_key(|&i| (x[i], i)); let mut sounds = BTreeSet::new(); let mut start_map: HashMap> = HashMap::new(); let mut end_map: HashMap> = HashMap::new(); for i in 0..t { if start_map.contains_key(&lrs[i].0) { start_map.get_mut(&lrs[i].0).unwrap().push(i as isize + 1); } else { start_map.insert(lrs[i].0, vec![i as isize + 1]); } if end_map.contains_key(&lrs[i].1) { end_map.get_mut(&lrs[i].1).unwrap().push(i as isize + 1); } else { end_map.insert(lrs[i].1, vec![i as isize + 1]); } } let starts = (0..t).map(|i| lrs[i].0).collect::>(); let ends = (0..t).map(|i| lrs[i].1).collect::>(); let mut pos = 0usize; for idx in 0..n { let i = order[idx]; let next_pos = x[i]; for s in starts.range(pos..=next_pos) { for &sidx in start_map.get(s).unwrap().iter() { sounds.insert(sidx); } } for s in ends.range(pos..next_pos) { for &sidx in end_map.get(s).unwrap().iter() { sounds.remove(&sidx); } } result[i] = *sounds.last().unwrap_or(&-1); if idx+1 < n && x[order[idx]] < x[order[idx+1]] { pos = next_pos; } } let result = result.into_iter().map(|v| v.to_string()).collect::>(); println!("{}", result.join("\n")); }