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 (1,1,2)(1,1,2) is 22 because it contains two different elements. Its total diversity is the sum of the diversities of the subarrays (1),(1),(2),(1,1),(1,2),(1,1,2)(1),(1),(2),(1,1),(1,2),(1,1,2), i.e. 1+1+1+1+2+2=81+1+1+1+2+2=8.

You are given a sequence {ai}\{a_i\} of length NN and QQ independent queries. Each query gives an interval [l,r][l,r]. For each query, find the minimum possible total diversity of the subarray [l,r][l,r] after rearranging the numbers inside [l,r][l,r].

Input Format

The first line contains two integers NN and QQ, representing the sequence length and the number of queries.

The second line contains NN integers, representing the sequence {ai}\{a_i\}.

The next QQ lines each contain two integers li,ril_i, r_i, representing the interval of the ii-th query.

Output Format

Output QQ lines of integers. The integer on the ii-th line is the answer to the ii-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 1010.

For the second sample, all elements of the sequence are 11, so no rearrangement is needed. The total diversity of interval [1,2][1,2] is 33, and the total diversity of interval [2,4][2,4] is 66.

For the third sample, for the first query you can rearrange the sequence to (1,2,2,3)(1,2,2,3), whose total diversity is 1+1+1+1+2+1+2+2+2+3=161+1+1+1+2+1+2+2+2+3=16. For the second query you can rearrange it to (1,1,2)(1,1,2), whose total diversity is 1+1+1+1+2+2=81+1+1+1+2+2=8. For the third query you can rearrange it to (1,3)(1,3), whose total diversity is 1+1+2=41+1+2=4.

Subtasks

All testdata satisfy 1N3×1051\leq N\leq 3\times 10^5, 1ai3×1051\leq a_i\leq 3\times 10^5, 1Q5×1041\leq Q\leq 5\times 10^4.

The constraints for each subtask are as follows: | Subtask ID | Points | Constraints | | :--------: | :----: | :------------------------------------------------------------------: | | 11 | 44 | 1N111\leq N\leq 11, 1ai3×1051\leq a_i\leq 3\times 10^5, Q=1Q=1, l1=1l_1=1, r1=Nr_1=N | | 22 | 1010 | 1N3×1051\leq N\leq 3\times 10^5, 1ai111\leq a_i\leq 11, Q=1Q=1, l1=1l_1=1, r1=Nr_1=N | | 33 | 88 | 1N3×1051\leq N\leq 3\times 10^5, 1ai231\leq a_i\leq 23, Q=1Q=1, l1=1l_1=1, r1=Nr_1=N | | 44 | 1616 | 1N3×1051\leq N\leq 3\times 10^5, 1ai10001\leq a_i\leq 1000, Q=1Q=1, l1=1l_1=1, r1=Nr_1=N | | 55 | 2626 | 1N3×1051\leq N\leq 3\times 10^5, 1ai3×1051\leq a_i\leq 3\times 10^5, Q=1Q=1, l1=1l_1=1, r1=Nr_1=N | | 66 | 3636 | 1N3×1051\leq N\leq 3\times 10^5, 1ai3×1051\leq a_i\leq 3\times 10^5, 1Q5×1041\leq Q\leq 5\times 10^4 |

Translated by ChatGPT 5