use proconio::input; fn dfs(p: &mut Vec<(usize, usize)>, visited: &mut Vec, path: &mut Vec, num:usize, depth: usize, ans:&mut usize) { if depth == num{ if check(p, path) { *ans +=1; } return } for u in 0..num{ if !visited[u]{ visited[u]=true; path.push(u); dfs(p, visited, path, num, depth + 1,ans); path.pop(); visited[u]=false; } } } fn check(p: &mut Vec<(usize, usize)>, path:&mut Vec) -> bool { let len = path.len(); let mut current = p[path[0]].0; for i in 1..len { if current <= p[path[i]].1 { current = current.max(p[path[i]].0); } else { return false; } } return true; } fn main() { input!{ n: usize, mut lr: [(usize, usize); n] } let mut ans = 0; dfs(&mut lr, &mut vec![false; n], &mut Vec::new(), n, 0, &mut ans); println!("{:?}", ans); }