Editorial for IOI '18 P3 - Werewolf


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.

Subtask 1

You choose a vertex V where you transform yourself from human form to wolf form.

For each choice of V, you need to decide whether it is possible to travel from Si to V in human form (i.e. only using vertices whose indices are Li), and to decide whether it is possible to travel from V to Ei in wolf form (i.e. only using vertices whose indices are Ri).

The time complexity of this solution is O(QN(N+M)).

Subtask 2

Determine the set of vertices you can visit from Si in human form, and determine the set of vertices you can visit from Ei in wolf form.

Then check whether these two sets intersect.

The time complexity of this solution is O(Q(N+M)).

Subtask 3

Let Ui be the set of the vertices which are reachable from Si by passing only vertices with index at least Li. Similarly, let Vi be the set of the vertices which are reachable from Ei by passing only vertices with index at most Ri. Note that Ui forms a range on the line on which cities are located. This range can be efficiently computed using doubling or Segment tree. Vi can be similarly computed. Then, we can answer the query by checking whether these two ranges intersect.

Subtask 4

We can construct a rooted tree so that Ui forms a subtree. This can be done using adding vertices to a disjoint set union structure in the descending order of indices. Then, using Euler-Tour on this tree, we can obtain a sequence of vertices and every Ui corresponds to a contiguous segment of this sequence. We can compute a similar sequence for Vi. Then, we can answer the query by checking whether two segments for Ui and Vi share a vertex. This can be done by the sweep line algorithm with a segment tree. The time complexity of this solution is O((Q+M)logN).


Comments

There are no comments at the moment.