Editorial for OTHS Coding Competition 3 (Mock CCC) S4 - Magic Library


Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
Submitting an official solution before solving the problem yourself is a bannable offence.

Authors: Ivan_Li, kevlu8

Using a square root decomposition approach with lazy propagation, A is divided into about \sqrt{N} blocks, each with a size of approximately \sqrt{N}. For each block, maintain a frequency array F, where F_i (1 \le i \le 500) represents the number of books labeled i in that block. Additionally, keep track of a variable lazy, initially set to -1, representing whether the entire block's books all have a label of lazy.

When updating a range (l, r) to v_i, there will be at most \sqrt{N} blocks fully covered by the range and at most 2 blocks that are partially covered. For fully covered blocks, update their value lazy to v_i. For partially covered blocks, if lazy = -1, update F and A based on lazy (eg. F_{lazy} = block size) and set lazy back to -1. Then update the individual books. A single update takes \mathcal{O}(\sqrt{N}) time.

A similar approach is used to query the range (l, r) for occurrences of v_i. For fully covered blocks, if lazy = v_i, add the block size to the count. Otherwise, add F_{v_i}. For partially covered blocks, if lazy = -1, update F and A based on lazy and set lazy back to -1. Then compare the individual books with v_i and add the occurrences accordingly. A single query takes \mathcal{O}(\sqrt{N}) time.


Comments

There are no comments at the moment.