[CF2152C]C. Triple Removal题解

C. Triple Removal

time limit per test

2 seconds

memory limit per test

1024 megabytes

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

For an array b=[b1,b2,…,bm] of length m where bi=0 or bi=1, consider the following triple removal operation:

  1. Choose three indices 1≤i<j<k≤m such that the elements at these positions are identical (bi=bj=bk).
  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=[a1,a2,…,an] of length n where ai=0 or ai=1. For each query, you are given a range 1≤l≤r≤n and must find the cost for the array [al,al+1,…,ar].

讯飞听见 翻译

厌倦了支持范围进位,Keria现在正在创建一个关于支持范围查询的数据结构问题。对于长度为 m 的数组 b=[b1,b2,…,bm] ,其中 bi=0 或 bi=1 ,请考虑以下三重删除操作:

选择三个索引 1≤i<j<k≤m ,使这些位置的元素相同( bi=bj=bk )。

2.从数组中删除这三个元素。此操作的成本定义为 min(k−j,j−i) 。删除后,数组的剩余部分将连接在一起,并重新标记其索引。我们希望使用三重删除操作使数组 b 为空。

因此,我们将数组的总成本定义为使数组为空所需的三重移除操作的成本的最小可能总和。如果无法使数组为空,则将成本定义为 −1 。

Keria想要测试他的数据结构。

为此,您必须回答 q 个独立查询。最初,您会得到一个长度为 n 的数组 a=[a1,a2,…,an] ,其中 ai=0 或 ai=1 。对于每个查询,您都会得到一个范围 1≤l≤r≤n ,并且必须找到数组 [al,al+1,…,ar] 的开销。

Input

Each test contains multiple test cases. The first line contains the number of test cases t (1≤t≤104). The description of the test cases follows.

The first line of each test case contains two integers n and q (1≤n,q≤250000) — the length of the array and the number of queries.

The next line contains n integers a1,a2,…,an (ai=0 or ai=1) — the elements of the array.

Then q lines follow. The i-th of them contains two integers li and ri (1≤li≤ri≤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 250000.

It is guaranteed that the sum of q over all test cases does not exceed 250000.

讯飞听见 翻译

输入

每个测试包含多个测试用例。第一行包含测试用例的数量 t ( 1≤t≤104 )。

测试用例的描述如下。

每个测试用例的第一行包含两个整数 n 和 q ( 1≤n,q≤250000 )

-数组的长度和查询的数量。

下一行包含 n 整数 a1,a2,…,an ( ai=0 或 ai=1 )

-数组的元素。

然后是 q 行。第 i 个包含两个整数 li 和 ri ( 1≤li≤ri≤n )

—第 i 个查询的子数组的范围。保证所有测试用例的 n 之和不超过 250000 。

保证所有测试用例的 q 之和不超过 250000 。

Output

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

讯飞听见 翻译

输出

对于每个测试用例,输出 q 行。第 i 行应包含表示第 i 个查询的答案的单个整数。

Example

Input

Copy



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

Output

Copy



4

2

3

-1

1

1

2

Note

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 0s and six 1s. A possible optimal sequence of operations is:

  1. Remove the three 1s 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 0s 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 1s 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 0s 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.

讯飞听见 翻译

注意

第一个测试用例的说明,第一个查询(112):

子数组为 [0,0,1,1,0,1,0,1,0,1,1,0] 。有六个 0 和六个 1 。

可能的最佳操作顺序是:

  1. 删除索引 3 、 4 、 6 处的三个 1 。

费用为 min(6−4,4−3)=min(2,1)=1 。数组变为 [0,0,0,0,1,0,1,1,0] 。

  1. 删除索引 1 、 2 、 3 处的三个 0 。费用为 min(3−2,2−1)=min(1,1)=1 。数组变为 [0,1,0,1,1,0] 。

  2. 删除索引 2 、 4 、 5 处的三个 1 。费用为 min(5−4,4−2)=min(1,2)=1 。数组变为 [0,0,0] 。

  3. 删除索引 1 、 2 、 3 处的三个 0 。费用为 min(3−2,2−1)=min(1,1)=1 。数组现在是空的。

总费用为 1+1+1+1=4 。

思路

直接前缀和+贪心。

代码见下

#include<bits/stdc++.h>
using namespace std;
long long t,n,q,a[250005],b[250005],c[250005],d[250005],l,r,lk=0;
int main(){
	cin>>t;
	while(t--){
		cin>>n>>q;
		for(int i=0;i<=n;i++){
			b[i]=c[i]=d[i]=0;
		}
		for(int i=1;i<=n;i++){
			cin>>a[i];
			b[i]=b[i-1];
			if(a[i]==1){
				b[i]++;
			}
			if(i!=1&&a[i]==a[i-1]){
				c[i-1]++;
				d[i]++;
			}
		}
		for(int i=1;i<=n;i++){
			c[i]+=c[i-1];
			d[i]+=d[i-1];
		}
		for(int i=1;i<=q;i++){
			cin>>l>>r;
			if((r-l+1)%3==0&&(b[r]-b[l-1])%3==0){
				lk=d[r]-c[l-1];
				if(lk==0){
					cout<<1+(r-l+1)/3<<endl;
				}
				else{
					cout<<(r-l+1)/3<<endl;
				}
			}
			else{
				cout<<-1<<endl;
			}
		}
	}
	return 0;
}

posted @ 2025-10-09 11:53  bz02_2023f2  阅读(9)  评论(0)    收藏  举报  来源