Editorial for JOI '19 Open P1 - Triple Jump
Submitting an official solution before solving the problem yourself is a bannable offence.
For any and
, we consider when they are possibly used as
and
(in any one of the queries).
First, if there exists satisfying
and
, we do not need to consider the solution with
and
because if we put
, we get a solution which is not worse than it.
Second, if and there exists
satisfying
and
, we do not need to consider the
solution with
and
because if we put
, we get a solution which is not worse than it.
By the above two reasons, the number of pairs we need only consider is
. We can list all such pairs if
for each
from
to
, we keep the set of candidate
's. We get a list of pairs
which are candidates of
.
Then, we process the queries. For each from
to
, we keep a sequence
. Here the
-th value of the sequence
is defined as the maximum of the sum
for
(
) satisfying
.
Once we can keep the sequence , it is easy to answer the queries.
Let us consider how the sequence varies if we change
from
to
. For each pair
of a candidate of
as above satisfying
, for every
with
, we have
Using Segment Tree, we can update a candidate in time. We can calculate the answer for each query
in
time. Therefore, we can solve this task in
time.
Comments