Editorial for CCO '22 P1 - Alternating Heights
Submitting an official solution before solving the problem yourself is a bannable offence.
Author:
Subtask 1
For this subtask, a subarray is valid if and only if it is alternating between and .
Preprocess by marking positions where we have adjacent s or adjacent s, and then use a PSA to check queries.
Time Complexity:
Hint
Graph Theory?
Subtask 2
Represent the problem as a graph, numbers from to being nodes and adjacent elements being edges.
A graph is valid iff it has no directed cycles.
Preprocess all subarrays.
Time Complexity:
Subtask 3
Process all queries in linear time.
Time Complexity:
Subtask 4
If a subarray is invalid, any subarray containing it is invalid. Use two-pointer to preprocess all subarrays.
Time Complexity:
Comments