E - Grid Filling Editorial by Kiri8128
For each \(i\), pre calculate:
- the set of numbers which appear in the first \(i\) rows
- the set of numbers which appear in the last \(i\) rows
- the set of numbers which appear in the first \(i\) columns
- the set of numbers which appear in the last \(i\) columns
Then you can answer to each query, by the union of 4 of these results. This can be done in \(O(HWN)\) time, with naive implementation. Further speed-up can be done with parallel calculation by bitsets.
posted:
last update: