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
Inputcopy | Outputcopy |
---|---|
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;
}
}
}
}