Triple Removal
시간 제한2초메모리 제한2048 MB
0과 1로 이루어진 배열에서 같은 값을 가진 세 원소를 묶어 지울 때 두 내부 간격 중 작은 값이 비용이 된다. 각 구간 질의마다 배열을 완전히 비우는 최소 비용을 구하거나 불가능하면 -1을 출력한다.
문제
Tired of supporting ranged carries, Keria is now creating a data structure problem about supporting range queries.
For an array of length where or , consider the following triple removal operation:
- Choose three indices such that the elements at these positions are identical ().
- Remove these three elements from the array. The cost of this operation is defined as . After the removal, the remaining parts of the array are concatenated, and their indices are relabeled.
We want to make the array empty using the triple removal operation. Hence, we define the total cost of an array as the minimum possible sum of the costs of triple removal operations required to make the array empty. If it is impossible to make the array empty, the cost is defined to be .
Keria wants to test his data structure. For this, you must answer independent queries. Initially, you are given an array of length where or . For each query, you are given a range and must find the cost for the array .
입력
Each test contains multiple test cases. The first line contains the number of test cases (). The description of the test cases follows.
The first line of each test case contains two integers and () --- the length of the array and the number of queries.
The next line contains integers ( or ) --- the elements of the array.
Then lines follow. The -th of them contains two integers and () --- the range of the subarray for the -th query.
It is guaranteed that the sum of over all test cases does not exceed .
It is guaranteed that the sum of over all test cases does not exceed .
출력
For each test case, output lines. The -th line should contain a single integer representing the answer to the -th query.
힌트
Explanation of the first test case, first query (1 12):
The subarray is . There are six s and six s. A possible optimal sequence of operations is:
- Remove the three s at indices , , . The cost is . The array becomes .
- Remove the three s at indices , , . The cost is . The array becomes .
- Remove the three s at indices , , . The cost is . The array becomes .
- Remove the three s at indices , , . The cost is . The array is now empty.
The total cost is .