luogu#P8171. [CEOI 2021] Diversity
[CEOI 2021] Diversity
Background
Translated from CEOI 2021 Day 1 Task 1. Diversity。
Problem Description
Define the diversity of a sequence as the number of distinct elements in it. Define the total diversity of a sequence as the sum of the diversities of all its subarrays.
For example, the diversity of the sequence is because it contains two different elements. Its total diversity is the sum of the diversities of the subarrays , i.e. .
You are given a sequence of length and independent queries. Each query gives an interval . For each query, find the minimum possible total diversity of the subarray after rearranging the numbers inside .
Input Format
The first line contains two integers and , representing the sequence length and the number of queries.
The second line contains integers, representing the sequence .
The next lines each contain two integers , representing the interval of the -th query.
Output Format
Output lines of integers. The integer on the -th line is the answer to the -th query.
3 1
1 2 3
1 3
10
4 2
1 1 1 1
1 2
2 4
3
6
5 3
1 2 1 3 2
2 5
1 3
3 4
16
8
4
Hint
Sample Explanation
For the first sample, no matter how you rearrange it, the total diversity of the queried interval is always .
For the second sample, all elements of the sequence are , so no rearrangement is needed. The total diversity of interval is , and the total diversity of interval is .
For the third sample, for the first query you can rearrange the sequence to , whose total diversity is . For the second query you can rearrange it to , whose total diversity is . For the third query you can rearrange it to , whose total diversity is .
Subtasks
All testdata satisfy , , .
The constraints for each subtask are as follows: | Subtask ID | Points | Constraints | | :--------: | :----: | :------------------------------------------------------------------: | | | | , , , , | | | | , , , , | | | | , , , , | | | | , , , , | | | | , , , , | | | | , , |
Translated by ChatGPT 5