| 시간 제한 | 메모리 제한 | 제출 | 정답 | 맞힌 사람 | 정답 비율 |
|---|---|---|---|---|---|
| 2 초 | 2048 MB | 88 | 86 | 78 | 97.500% |
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, \ldots, b_m]$ of length $m$ where $b_i=0$ or $b_i=1,ドル consider the following triple removal operation:
We want to make the array $b$ 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$.
Keria wants to test his data structure. For this, you must answer $q$ independent queries. Initially, you are given an array $a = [a_1, a_2, \ldots, a_n]$ of length $n$ where $a_i=0$ or $a_i=1$. For each query, you are given a range 1ドル \le l \le r \le n$ and must find the cost for the array $[a_l, a_{l+1}, \ldots, a_r]$.
Each test contains multiple test cases. The first line contains the number of test cases $t$ (1ドル \le t \le 10^4$). The description of the test cases follows.
The first line of each test case contains two integers $n$ and $q$ (1ドル \le n, q \le 250,000円$) --- the length of the array and the number of queries.
The next line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($a_i = 0$ or $a_i=1$) --- the elements of the array.
Then $q$ lines follow. The $i$-th of them contains two integers $l_i$ and $r_i$ (1ドル \le l_i \le r_i \le n$) --- the range of the subarray for the $i$-th query.
It is guaranteed that the sum of $n$ over all test cases does not exceed 250ドル,000円$.
It is guaranteed that the sum of $q$ over all test cases does not exceed 250ドル,000円$.
For each test case, output $q$ lines. The $i$-th line should contain a single integer representing the answer to the $i$-th query.
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
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]$. There are six 0ドル$s and six 1ドル$s. A possible optimal sequence of operations is:
The total cost is 1ドル+1+1+1=4$.
Contest > Codeforces > Squarepoint Challenge (Codeforces Round 1055, Div. 1 + Div. 2) C번