Codeforces 1744B. Even-Odd Increments

time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
You are given n of integers a1,a2,…,an. Process q queries of two types:

query of the form “0 xj”: add the value xj to all even elements of the array a,
query of the form “1 xj”: add the value xj to all odd elements of the array a.
Note that when processing the query, we look specifically at the odd/even value of ai, not its index.

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

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 t (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 n and q (1≤n, q≤105) — the length of array a and the number of queries.

The second line of each test case contains exactly n integers: a1,a2,…,an (1≤ai≤109) — elements of the array a.

The following q lines contain queries as two integers typej and xj (0≤typej≤1, 1≤xj≤104).

It is guaranteed that the sum of values n over all test cases in a test does not exceed 105. Similarly, the sum of values q over all test cases does not exceed 105.

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

Example
inputCopy
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
outputCopy
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] after the first query.

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

#include<bits/stdc++.h>
using namespace std;
int main(void) {
	int t;
	cin >> t;
	while (t--) {
		long long n, q,sum=0;
		cin >> n >> q;
		int odd = 0, even = 0, x;
		for (int i = 0; i < n; i++) {
			cin >> x;
			if (x % 2 == 0)odd++;
			else even++;
			sum += x;
		}
		long long ty, p;
		while (q--) {
			cin >> ty >> p;
			if (ty == 0) { 
				sum += p * odd;
				if (p % 2 == 1) even += odd, odd = 0;
			}
			else { 
				sum += p * even;
				if (p % 2 == 1)odd += even, even = 0;
			}
			cout << sum << endl;
		}
	}
}
 
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值