Logo
(追記) (追記ここまで)

34628번 - Triple Removal 다국어

시간 제한메모리 제한제출정답맞힌 사람정답 비율
2 초 2048 MB88867897.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:

  1. Choose three indices 1ドル \le i < j < k \le m$ such that the elements at these positions are identical ($b_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)$. After the removal, the remaining parts of the array are concatenated, and their indices are relabeled.

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.

제한

예제 입력 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

예제 출력 1

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:

  1. Remove the three 1ドル$s at indices 3ドル,ドル 4ドル,ドル 6ドル$. The cost is $\min(6-4, 4-3) = \min(2, 1) = 1$. The array becomes $[0, 0, 0, 0, 1, 0, 1, 1, 0]$.
  2. Remove the three 0ドル$s at indices 1ドル,ドル 2ドル,ドル 3ドル$. The cost is $\min(3-2, 2-1) = \min(1, 1) = 1$. The array becomes $[0, 1, 0, 1, 1, 0]$.
  3. Remove the three 1ドル$s at indices 2ドル,ドル 4ドル,ドル 5ドル$. The cost is $\min(5-4, 4-2) = \min(1, 2) = 1$. The array becomes $[0, 0, 0]$.
  4. Remove the three 0ドル$s at indices 1ドル,ドル 2ドル,ドル 3ドル$. The cost is $\min(3-2, 2-1) = \min(1, 1) = 1$. The array is now empty.

The total cost is 1ドル+1+1+1=4$.

출처

Contest > Codeforces > Squarepoint Challenge (Codeforces Round 1055, Div. 1 + Div. 2) C번

(追記) (追記ここまで)

출처

대학교 대회

  • 사업자 등록 번호: 541-88-00682
  • 대표자명: 최백준
  • 주소: 서울시 서초구 서초대로74길 29 서초파라곤 412호
  • 전화번호: 02-521-0487 (이메일로 연락 주세요)
  • 이메일: contacts@startlink.io
  • 통신판매신고번호: 제 2017-서울서초-2193 호

AltStyle によって変換されたページ (->オリジナル) /