E - Prefix Equality Editorial by Kiri8128


We have a simple \(O(N+Q)\) solution using a hash, allowing a small probability of an error. First define a hash for each integer \(a\). The hash of a set is determined by the sum of all hashes of integers it contains. You can calculate all hashes for the set of the first \(i\) terms of \(A\) and \(B\), for all \(i=1, \cdots, N\) before reading the queries. Now you can answer to each query with pre-calculated hashes in \(O(1)\) time. In this submission (PyPy3), we used \(a * (a + 1346) * (a + 9185)\) as a hash of integer \(a\).

posted:
last update: