Triple Removal

시간 제한2초메모리 제한2048 MB

요약
0과 1로 이루어진 배열에서 같은 값을 가진 세 원소를 묶어 지울 때 두 내부 간격 중 작은 값이 비용이 된다. 각 구간 질의마다 배열을 완전히 비우는 최소 비용을 구하거나 불가능하면 -1을 출력한다.
난이도

어려움10점 중 8점

유형
동적 계획법, 누적 합, 그리디, 수학
정답자
아직 제출이 없습니다

문제

Tired of supporting ranged carries, Keria is now creating a data structure problem about supporting range queries.

For an array b=\[b_1,b_2,…,b_m]b = \[b\_1, b\_2, \ldots, b\_m] of length mm where b_i=0b\_i=0 or b_i=1b\_i=1, consider the following triple removal operation:

  1. Choose three indices 1≤i<j<k≤m1 \le i < j < k \le m such that the elements at these positions are identical (b_i=b_j=b_kb\_i = b\_j = b\_k).
  2. Remove these three elements from the array. The cost of this operation is defined as min⁡(k−j,j−i)\min(k-j, j-i). After the removal, the remaining parts of the array are concatenated, and their indices are relabeled.

We want to make the array bb 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 −1-1.

Keria wants to test his data structure. For this, you must answer qq independent queries. Initially, you are given an array a=\[a_1,a_2,…,a_n]a = \[a\_1, a\_2, \ldots, a\_n] of length nn where a_i=0a\_i=0 or a_i=1a\_i=1. For each query, you are given a range 1≤l≤r≤n1 \le l \le r \le n and must find the cost for the array \[a_l,a_l+1,…,a_r]\[a\_l, a\_{l+1}, \ldots, a\_r].

입력

Each test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤1041 \le t \le 10^4). The description of the test cases follows.

The first line of each test case contains two integers nn and qq (1≤n,q≤250,0001 \le n, q \le 250\\,000) --- the length of the array and the number of queries.

The next line contains nn integers a_1,a_2,…,a_na\_1, a\_2, \ldots, a\_n (a_i=0a\_i = 0 or a_i=1a\_i=1) --- the elements of the array.

Then qq lines follow. The ii-th of them contains two integers l_il\_i and r_ir\_i (1≤l_i≤r_i≤n1 \le l\_i \le r\_i \le n) --- the range of the subarray for the ii-th query.

It is guaranteed that the sum of nn over all test cases does not exceed 250,000250\\,000.

It is guaranteed that the sum of qq over all test cases does not exceed 250,000250\\,000.

출력

For each test case, output qq lines. The ii-th line should contain a single integer representing the answer to the ii-th query.

힌트

Explanation of the first test case, first query (1 12):

The subarray is \[0,0,1,1,0,1,0,1,0,1,1,0]\[0, 0, 1, 1, 0, 1, 0, 1, 0, 1, 1, 0]. There are six 00s and six 11s. A possible optimal sequence of operations is:

  1. Remove the three 11s at indices 33, 44, 66. The cost is min⁡(6−4,4−3)=min⁡(2,1)=1\min(6-4, 4-3) = \min(2, 1) = 1. The array becomes \[0,0,0,0,1,0,1,1,0]\[0, 0, 0, 0, 1, 0, 1, 1, 0].
  2. Remove the three 00s at indices 11, 22, 33. The cost is min⁡(3−2,2−1)=min⁡(1,1)=1\min(3-2, 2-1) = \min(1, 1) = 1. The array becomes \[0,1,0,1,1,0]\[0, 1, 0, 1, 1, 0].
  3. Remove the three 11s at indices 22, 44, 55. The cost is min⁡(5−4,4−2)=min⁡(1,2)=1\min(5-4, 4-2) = \min(1, 2) = 1. The array becomes \[0,0,0]\[0, 0, 0].
  4. Remove the three 00s at indices 11, 22, 33. The cost is min⁡(3−2,2−1)=min⁡(1,1)=1\min(3-2, 2-1) = \min(1, 1) = 1. The array is now empty.

The total cost is 1+1+1+1=41+1+1+1=4.

예제1

  1. 예제 1

    입력
    2
    12 4
    0 0 1 1 0 1 0 1 0 1 1 0
    1 12
    2 7
    5 10
    6 11
    6 3
    0 0 0 1 1 1
    1 3
    4 6
    1 6
    
    예상 출력
    4
    2
    3
    -1
    1
    1
    2