#P16059. [CSPro 24] 序列查询
[CSPro 24] 序列查询
Background
Luogu’s testdata are only for non-official community communication and are not official testdata. Official judging link: https://www.cspro.org/.
Problem Description
In the shopping mall on Xixi Aifu Island, there are many stores and a wide variety of products. To help visitors quickly choose the product they want within their budget, the IT department decides to develop a product retrieval system. For any given budget , it should query the most expensive product whose price is within the budget range (). If no product meets the budget requirement, it will recommend a customized souvenir from Xixi Aifu Island that can be collected for free.
Assume there are products in the mall, and their prices from low to high are . Then the process of retrieving a product based on budget can be abstracted as the following sequence query problem.
is a sequence of integers in the range , satisfying . (This definition implies that must be less than .)
Based on the sequence , for any integer in the range , define the query as: the index of the largest integer in sequence that is less than or equal to . Specifically, there are two cases:
- There exists an index such that .
In this case, all numbers in from to are less than or equal to . The largest is , whose index is , so .
- .
In this case, all numbers in are less than or equal to . The largest is , so .
Let denote the sum of to , that is:
$$\begin{aligned} sum(A) = \sum_{i=0}^{N-1} f(i) = f(0) + f(1) + f(2) + \cdots + f(N - 1) \end{aligned}$$Given the sequence , compute .
Input Format
Read input from standard input.
The first line contains two positive integers and , separated by spaces.
The second line contains integers , separated by spaces.
Note that is fixed as , so the input does not include .
Output Format
Write output to standard output.
3 10
2 5 8
15
9 10
1 2 3 4 5 6 7 8 9
45
Hint
Sample 1 Explanation
| 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | |
|---|---|---|---|---|---|---|---|---|---|---|
| 0 | 1 | 2 | 3 | |||||||
As shown in the table above, .
Considering that , , , and , you can also compute using the following expression:
$$\begin{aligned} sum(A) = f(0) \times 2 + f(2) \times 3 + f(5) \times 3 + f(8) \times 2 \end{aligned}$$Subtasks
of the testdata satisfy and ;
all testdata satisfy and .
Hint
If there exists an interval such that , using multiplication instead of adding to one by one may greatly improve the algorithm efficiency.
Translated by ChatGPT 5