Even-Odd Increments

Even-Odd Increments

You are given nn of integers a_1, a_2, \ldots, a_na1​,a2​,…,an​. Process qq queries of two types:

  • query of the form "0 x_jxj​": add the value x_jxj​ to all even elements of the array aa,
  • query of the form "1 x_jxj​": add the value x_jxj​ to‎数组的所有奇数元素‎‎一个‎‎一个‎.

Note that when processing the query, we look specifically at the odd/even value of a_iai​, not its index.

After processing each query, print the sum of the elements of the array aa.

Please note that the answer for some test cases won't fit into 32-bit integer type, so you should use at least 64-bit integer type in your programming language (like long long for C++).

Input

The first line of the input contains an integer tt (1 \leq t \leq 10^4(1≤t≤104) — the number of test cases.

The descriptions of the test cases follow.

The first line of each test case contains two integers nn and qq (1 \leq n1≤n, q \leq 10^5q≤105) — the length of array aa and the number of queries.

The second line of each test case contains exactly nn integers: a_1, a_2, \ldots, a_na1​,a2​,…,an​ (1 \leq a_i \leq 10^91≤ai​≤109) — elements of the array aa.

The following qq lines contain queries as two integers type_jtypej​ and x_jxj​ (0 \leq type_j \leq 1(0≤typej​≤1, 1 \leq x_j \leq 10^41≤xj​≤104).

It is guaranteed that the sum of values nn over all test cases in a test does not exceed 10^5105. Similarly, the sum of values qq over all test cases does not exceed 10^5105.

Output

For each test case, print qq numbers: the sum of the elements of the array aa after processing a query.

Sample 1

InputcopyOutputcopy
4
1 1
1
1 1
3 3
1 2 4
0 2
1 3
0 5
6 7
1 3 2 4 10 48
1 6
0 5
0 4
0 5
1 3
0 12
0 1
6 7
1000000000 1000000000 1000000000 11 15 17
0 17
1 10000
1 51
0 92
0 53
1 16
0 1
2
11
14
29
80
100
100
100
118
190
196
3000000094
3000060094
3000060400
3000060952
3000061270
3000061366
3000061366

Note

In the first test case, the array a = [2]a=[2] after the first query.

In the third test case, the array aa is modified as follows: [1, 3, 2, 4, 10, 48][1,3,2,4,10,48] \rightarrow→ [7, 9, 2, 4, 10, 48][7,9,2,4,10,48] \rightarrow→ [7, 9, 7, 9, 15, 53][7,9,7,9,15,53] \rightarrow→ [7, 9, 7, 9, 15, 53][7,9,7,9,15,53] \rightarrow→ [10, 12, 10, 12, 18, 56][10,12,10,12,18,56] \rightarrow→ [22, 24, 22, 24, 30, 68][22,24,22,24,30,68] \rightarrow→ [23, 25, 23, 25, 31, 69][23,25,23,25,31,69].

题解:存下奇数个数和偶数个数,对它们分别操作即可

#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
long long sum,jn,on,x,y,f;

int main() 
{
	int t;
	cin>>t;
	while(t--)
	{
		jn=on=sum=0;
		long long n,q;
		cin>>n>>q;
		for(int i=1;i<=n;i++)
		{
			cin>>x;
			if(x%2==0)
			{
				on++;
			}
			else
			{
				jn++;
			}
			sum+=x;
		}
		for(int i=1;i<=q;i++)
		{
			cin>>f>>y;
			if(f==0)
			{
				if(y%2==0)
				{
					sum+=y*on;
				}
				else
				{
					sum+=y*on;
					jn+=on;
					on=0;
				}
				cout<<sum<<endl;
			}
			else if(f==1)
			{
				if(y%2==0)
				{
					sum+=y*jn;
				}
				else
				{
					sum+=y*jn;
					on+=jn;
					jn=0;
				}
				cout<<sum<<endl;
			}
		}
	}
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

linalw

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值