Rust でのマージテク

take を使わないと shared XOR mutable で引っかかる

fn solve3(&self) -> Answer {
    let Problem { n_box, nq, colors, queries } = self;

    // box_to_colors[box_i]: box_i 番目の箱に入っているの色の集合
    let mut box_to_colors = colors.iter().copied().map(|c| HashSet::from([c])).collect_vec();
    let mut ans = Vec::new();
    for q in queries {
        // 箱a のボールを箱 b に移す

        if box_to_colors[q.a].len() > box_to_colors[q.b].len() {
            box_to_colors.swap(q.a, q.b)
        }
        for &x in &box_to_colors[q.a] { // ここで take しないと
            box_to_colors[q.b].insert(x); // ここでエラーになる!!!
        }

        ans.push(box_to_colors[q.b].len());
    }

    Answer { ans }
}

cannot borrow box_to_colors as mutable because it is also borrowed as immutable

std::mem

std::mem

マージテクが $O(n \log n)$ な理由