Editorial for SAC '22 Code Challenge 2 P5 - Even Colouring


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.

Author: maxcruickshanks

Subtask 1

It suffices to maintain an array with each value and loop the range [L, R] while skipping every other element.

Time Complexity: \mathcal{O}(NQ)

Subtask 2

Maintain two Fenwick Trees (or Segment Trees) for the two parities (i.e., whether the index is even or odd).

If the left bound of the query is on an even index, query the even index Fenwick Tree from [L, R]; if the left bound of the query is on an odd index, query the odd index Fenwick Tree from [L, R].

Time Complexity: \mathcal{O}((N+Q) \log N)

Note that this problem is very similar to this Codeforces educational problem with some slight modifications.


Comments

There are no comments at the moment.