Zero Array

20 篇文章 0 订阅
10 篇文章 1 订阅

Zero Array
You are given an array a consisting of n elements, and q queries. There are two types of queries, as follow:
“1 p v” – An update query asks to change the value at position p in array a to v.
“2” – A query asks to print the minimum number of required operations to convert array a to a zero array.
A zero array is defined as an array which all its elements are zeros. There is only one allowed operation to convert an array to a zero array. At each operation, you can choose a value x and subtract it from all non-zero elements in the array, such that no element will be negative after the operation.
Input
The first line contains an integer T (1 ≤ T ≤ 100), in which T is the number of test cases.

The first line of each test case consists of two integers n and q (1 ≤ n, q ≤ 105), in which n is the size of the array a, and q is the number of queries.

Then a line follows containing n elements a1, a2, …, an (0 ≤ ai ≤ 109), giving the array a.

Then q lines follow, each line containing a query in the format described in the problem statement. It is guaranteed that the following constraints hold for the first type of queries: 1 ≤ p ≤ n, 0 ≤ v ≤ 109.

The sum of n and q overall test cases does not exceed 106 for each.

Output
For each query of the second type, print the minimum number of required operations to convert array a to a zero array. The queries must be answered in the order given in the input.

Example
Input

1
5 5
3 2 1 5 4
1 2 3
2
1 3 2
1 4 1
2
Output
4
4
这个题的难点(个人理解),在对p位置的数进行更新的时候,还实时记录新数组有几个不同的数(非零的)
题比较好理解(纯英文题面有点长 ),把问题分解一下
1.利用a[]数组来记录存进去的数,下标i来记录位置信息,对p位置的数进行更新也要用到这个数组
2.利用map函数,来记录每个数出现的频率,对于第一次出现的数,就代表着来了个新数。
下面附上代码。

#include<iostream>
#include<cstring>
#include<algorithm>
#include<map>
using namespace std;
map<int,int>mp;
int a[111111];
int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		mp.clear(); //格式化,避免下一个测试数据出问题 
		int n,q;
		scanf("%d%d",&n,&q);
		int count=0;   //用来标记有几个不同的数的(不包括0),因为题意是问把某个数变成0,所以0不考虑 
		for(int i=1;i<=n;i++)
		{
			int num;
			scanf("%d",&num);
			a[i]=num;  
			if(num&&mp[num]==0) //如果这个数第一次出现,说明输入了一个新的数,所以conut+1 
			{
				count++;
			}
			mp[num]++;//用mp来储存该数出现的次数 
		}
		while(q--)
		{
			int x;
			scanf("%d",&x);
			if(x==1)    
			{
				int p,v;
				scanf("%d%d",&p,&v); //把第p个数,变成v 
				int y=a[p]; //先把第p个数记下来(这个数是还没改动之前的) ,记下来是为了下面,删掉时,这个数对应出现的次数-1 
				if(v&&mp[v]==0)  //如果新加的数,是之前没出现过的,就count+1,出现过的话,就没必要了 
				{
					count++;
				}
				mp[v]++;  //v这个数出现次数+1 
				a[p]=v;     //更新p位置的数为v 
				mp[y]--; //这是把之前那个旧的数,删掉, 
				if(y&&mp[y]==0) //如果旧的那个数完全没了,说明有个种类的数完全没了,conut-1 
				{
					count--;
				}
			}
			else 
			{
				printf("%d\n",count);  //count就是要输出的答案,即有几个不同的数(非零) 
			}
		}
	}
	return 0;
} 

如果你有任何建议或者批评和补充,请留言指出,不胜感激。

  • 5
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值