#![allow(unused_imports)] use std::cmp::*; use std::collections::*; use std::io::Write; use std::ops::Bound::*; #[allow(unused_macros)] macro_rules! debug { ($($e:expr),*) => { #[cfg(debug_assertions)] $({ let (e, mut err) = (stringify!($e), std::io::stderr()); writeln!(err, "{} = {:?}", e, $e).unwrap() })* }; } fn main() { let v = read_vec::(); let (n, m, q) = (v[0], v[1], v[2]); let mut queries = vec![]; for i in 0..q { queries.push(read_vec::()); } let mut init = mat_zeros(n, n); for i in 0..n { init[i][i] = 1; } let mut seg = SegTree::new(m, init, matmul); for ref query in queries { match query[0] { 1 => { let d = query[1] - 1; let p = query[2..].iter().map(|&x| x - 1).collect::>(); let mut mat = mat_zeros(n, n); for i in 0..n { mat[i][p[i]] = 1; } seg.update(d, mat); } 2 => { let s = query[1] - 1; let m = seg.query(0, s + 1); for i in 0..n { for j in 0..n { if m[j][i] == 1 { print!("{} ", j + 1); } } } println!(""); } _ => { let (l, r) = (query[1] - 1, query[2] - 1); let m = seg.query(l, r + 1); let mut ans = 0; for i in 0..n { for j in 0..n { if m[j][i] == 1 { ans += (i as i64 - j as i64).abs(); } } } println!("{}", ans); } } } } type mat = Vec>; fn mat_zeros(n: usize, m: usize) -> mat { vec![vec![0i64; m]; n] } fn matmul(a: &mat, b: &mat) -> mat { let mut c = mat_zeros(a.len(), b[0].len()); for i in 0..a.len() { for k in 0..b.len() { for j in 0..b[0].len() { c[i][j] += a[i][k] * b[k][j]; } } } c } fn read() -> T { let mut s = String::new(); std::io::stdin().read_line(&mut s).ok(); s.trim().parse().ok().unwrap() } fn read_vec() -> Vec { read::() .split_whitespace() .map(|e| e.parse().ok().unwrap()) .collect() } #[derive(Clone)] struct SegTree where F: Fn(&T, &T) -> T, T: std::clone::Clone, { n: usize, dat: Vec, init: T, functor: F, } impl SegTree where F: Fn(&T, &T) -> T, T: std::clone::Clone, { fn new(n: usize, init: T, f: F) -> SegTree { let mut m = 1; // For simplicity, we use 2 ** n sized SegTree. while m < n { m *= 2; } SegTree { n: m, dat: vec![init.clone(); 2 * m - 1], init: init.clone(), functor: f, } } // dat[k] = a; fn update(&mut self, k: usize, a: T) { let mut k = k; k += self.n - 1; self.dat[k] = a; while k > 0 { k = (k - 1) / 2; self.dat[k] = (self.functor)(&self.dat[k * 2 + 1], &self.dat[k * 2 + 2]); } } // [a, b) fn query(&self, a: usize, b: usize) -> T { self.query_inner(a, b, 0, 0, self.n) } fn query_inner(&self, a: usize, b: usize, k: usize, l: usize, r: usize) -> T { if r <= a || b <= l { return self.init.clone(); } if a <= l && r <= b { return self.dat[k].clone(); } let vl = self.query_inner(a, b, k * 2 + 1, l, (l + r) / 2); let vr = self.query_inner(a, b, k * 2 + 2, (l + r) / 2, r); (self.functor)(&vl, &vr) } }