Even-Odd Increments

这是一个关于数组操作的编程题目,给定一个数组和一系列查询,查询分为两种类型:0 x_j(将偶数位置的元素增加x_j),1 x_j(将奇数位置的元素增加x_j)。每次操作后需要输出当前数组的元素之和。题目要求使用至少64位整型来存储答案,以防止溢出。示例中展示了几个测试用例及其处理过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

实验(三) * *******************************************************************************************/ // 创建1个任务,任务执行完毕后自动退出 #if(0) //任务优先级 #define TASK1_TASK_PRIO 4 //任务堆栈大小 #define TASK1_STK_SIZE 128 //任务控制块 OS_TCB Task1_TaskTCB; //任务堆栈 CPU_STK TASK1_TASK_STK[TASK1_STK_SIZE]; void task1_task(void *p_arg); //任务优先级 #define TASK2_TASK_PRIO 4 //任务堆栈大小 #define TASK2_STK_SIZE 128 //任务控制块 OS_TCB Task2_TaskTCB; //任务堆栈 CPU_STK TASK2_TASK_STK[TASK2_STK_SIZE]; void task2_task(void *p_arg); //任务优先级 #define TASK3_TASK_PRIO 4 //任务堆栈大小 #define TASK3_STK_SIZE 128 //任务控制块 OS_TCB Task3_TaskTCB; //任务堆栈 CPU_STK TASK3_TASK_STK[TASK3_STK_SIZE]; void task3_task(void *p_arg); OS_Q num_even; OS_Q num_odd; //开始任务任务函数 void start_task(void *p_arg) { CPU_INT32U cpu_clk_freq; CPU_INT32U cnts; OS_ERR err; (void)p_arg; BSP_Init(); /* Initialize BSP functions */ CPU_Init(); cpu_clk_freq = BSP_CPU_ClkFreq(); /* Determine SysTick reference freq. */ cnts = cpu_clk_freq / (CPU_INT32U)OSCfg_TickRate_Hz; /* Determine nbr SysTick increments */ OS_CPU_SysTickInit(cnts); /* Init uC/OS periodic time src (SysTick). */ // Mem_Init(); /* Initialize Memory Management Module */ //创建两个队列 OSQCreate(&num_even,"num_even",1,&err); OSQCreate(&num_odd,"num_odd",1,&err); #if OS_CFG_STAT_TASK_EN > 0u OSStatTaskCPUUsageInit(&err); /* Compute CPU capacity with no task running */ #endif CPU_IntDisMeasMaxCurReset(); #if OS_CFG_SCHED_ROUND_ROBIN_EN //当使用时间片轮转的时候 //使能时间片轮转调度功能,时间片长度为1个系统时钟节拍,既1*5=5ms OSSchedRoundRobinCfg(DEF_ENABLED,1,&err); #endif //创建TASK1任务 OSTaskCreate((OS_TCB * )&Task1_TaskTCB, (CPU_CHAR * )"Task1 task", (OS_TASK_PTR )task1_task, (void * )0, (OS_PRIO )TASK1_TASK_PRIO, (CPU_STK * )&TASK1_TASK_STK[0], (CPU_STK_SIZE)TASK1_STK_SIZE/10, (CPU_STK_SIZE)TASK1_STK_SIZE, (OS_MSG_QTY )0, (OS_TICK )0, (void * )0, (OS_OPT )OS_OPT_TASK_STK_CHK|OS_OPT_TASK_STK_CLR, (OS_ERR * )&err); //创建TASK2任务 OSTaskCreate((OS_TCB * )&Task2_TaskTCB, (CPU_CHAR * )"Task2 task", (OS_TASK_PTR )task2_task, (void * )0, (OS_PRIO )TASK2_TASK_PRIO, (CPU_STK * )&TASK2_TASK_STK[0], (CPU_STK_SIZE)TASK2_STK_SIZE/10, (CPU_STK_SIZE)TASK2_STK_SIZE, (OS_MSG_QTY )0, (OS_TICK )0, (void * )0, (OS_OPT )OS_OPT_TASK_STK_CHK|OS_OPT_TASK_STK_CLR, (OS_ERR * )&err); //创建TASK3任务 OSTaskCreate((OS_TCB * )&Task3_TaskTCB, (CPU_CHAR * )"Task3 task", (OS_TASK_PTR )task3_task, (void * )0, (OS_PRIO )TASK3_TASK_PRIO, (CPU_STK * )&TASK3_TASK_STK[0], (CPU_STK_SIZE)TASK3_STK_SIZE/10, (CPU_STK_SIZE)TASK3_STK_SIZE, (OS_MSG_QTY )0, (OS_TICK )0, (void * )0, (OS_OPT )OS_OPT_TASK_STK_CHK|OS_OPT_TASK_STK_CLR, (OS_ERR * )&err); //删除start_task任务自身 // OSTaskDel( (OS_TCB*)0, &err ); } //task1任务函数写队列 volatile uint32_t i; void task1_task(void *p_arg) { OS_ERR err; uint8_t count = 3; uint8_t a=4; uint8_t b; b = a % 2; while( count-- ) { LED0_ON(); OSTimeDly(200,OS_OPT_TIME_DLY,&err); LED0_OFF(); OSTimeDly(200,OS_OPT_TIME_DLY,&err); } if(b==0) { OSQPost(&num_even,&b,sizeof(255),OS_OPT_POST_ALL,&err); } else { OSQPost(&num_odd,&b,sizeof(255),OS_OPT_POST_ALL,&err); } while( 1 ) { LED0_ON(); OSTimeDly(200,OS_OPT_TIME_DLY,&err); LED0_OFF(); OSTimeDly(200,OS_OPT_TIME_DLY,&err); } } //task2任务函数读队列 volatile uint32_t i; void task2_task(void *p_arg) { OS_ERR err; uint8_t *b; OS_MSG_SIZE size=0; b=OSQPend(&num_odd,0,OS_OPT_PEND_BLOCKING,&size,0,&err); while(1) { LED1_ON(); OSTimeDly(200,OS_OPT_TIME_DLY,&err); LED1_OFF(); OSTimeDly(200,OS_OPT_TIME_DLY,&err); } } //task3任务函数读队列 volatile uint32_t i; void task3_task(void *p_arg) { OS_ERR err; uint8_t *b; OS_MSG_SIZE size=0; b=OSQPend(&num_even,0,OS_OPT_PEND_BLOCKING,&size,0,&err); while( 1) { LED2_ON(); OSTimeDly(200,OS_OPT_TIME_DLY,&err); LED2_OFF(); OSTimeDly(200,OS_OPT_TIME_DLY,&err); } } #endif 说明一下实验现象 并帮我写一份实验报告
06-28
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

linalw

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

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

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

打赏作者

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

抵扣说明:

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

余额充值