D - Division into 3 Editorial by evima
First, let’s consider the case with one query.
Suppose the input is . First, let . When decomposing into , we will consider the cases based on where is included among .
First, the case where is included in is simple. Since is determined to be , there is no harm in making larger. Therefore, we only need to consider the pattern .
Next, let’s consider the case where is included in . By the same reasoning as before, can be assumed to contain only one element. By trying all possible positions for , we can calculate the answer.
The case where is included in is similar to the case for . By reversing the sequence, we can solve it with exactly the same code.
Next, let’s consider answering multiple queries.
The case where is included in is still simple and can be handled with a simple RMQ.
There are two main approaches to handle the case where is included in .
The first approach is to manage for each . We will process the queries in ascending order of . As increases, for each will also increase. This increase can be represented by range addition, so we can solve it using a segment tree with range addition and range minimum query.
The second approach is to manage for each . Similar to the first approach, we process the queries in ascending order of . We manage the possible values of with a stack and also manage the possible minimum values for . This can be implemented with a simple segment tree for range minimum query.
Using either approach, the time complexity will be .
posted:
last update: