A Simple Problem with Integers

//出现RE后,将数组大小开大,就好了。

//把int改为long long  就由WA变为AC了

 

//区间更新  //参见 https://blog.youkuaiyun.com/SSL_ZYC/article/details/81940902 http://kenby.iteye.com/blog/962159

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<string.h>
 
using namespace std;
 
#define lowbit(x) (x&(-x)) //计算2^k 

typedef long long LL;

const int Max = 500000+5;
LL c[Max];//c[i] = a[i – 2^k + 1] + … + a[i],k为i在二进制下末尾0的个数
LL c1[Max];
 
void Update(LL *c, int x, int y, int MAX)
{
	while(x <= MAX){
		c[x] += y;
		x += lowbit(x);
	}
}
 
LL Sum(LL *c, LL x)
{
	LL sum = 0;
	
	while(x > 0){
		sum += c[x];
		x -= lowbit(x);
	}
	return sum;
	
}
 
 
 
void SumSQ(int y, int x)
{
	int sum = 0;
	
	while(y > 0){
		sum += c[y];
		y -= lowbit(y);
	}
	
	while(x > 0){
		sum -= c[x];
		x -= lowbit(x);
	}
	
//	cout << sum << endl;
 
	printf("%d\n", sum);	
}

int main()
{	
	LL T, cnt = 1;
	string s;
	
	//cin >> T;
{
		LL N, t;
		
	//	cin >> N;
		scanf("%lld%lld", &N, &t);
			
		memset(c, 0, sizeof(c));
		
		for(LL i=1; i<=N; i++){
			LL x;
			
		//	cin >> x;
			scanf("%lld", &x);
				
			Update(c, i, x, N);		
		}
		
//		cout << "Case " << cnt++ << ":" << endl;
        
//		printf("Case %d:\n", cnt++);
		
//		while(cin >> s && s != "End"){
		while(t--){
 
 cin >> s;
 
			LL x, y, num;
			
//			cin >> x >> y;
						
			if(s == "C"){
				scanf("%lld%lld%lld", &x, &y, &num);

				Update(c, x, -num*(x-1), N);
				Update(c, y+1, num*y, N);	
				
				Update(c1, x, num, N);	
				Update(c1, y+1, -num, N);	
			}
			else if(s == "Q"){
				scanf("%lld%lld", &x, &y);
            long long ans = 0;
            
            ans += Sum(c, y) + Sum(c1, y)*y;
            ans -= Sum(c, x-1) + Sum(c1, x-1)*(x-1);//区间求和
            printf("%lld\n", ans);

		//		SumSQ(y, x-1);
		//		cout << Sum(y)-Sum(x-1) << endl;
			}
			
		}
		
	}
	return 0;
} 

 

### 编程实现多个整数求和并处理多组测试用例 为了满足需求,可以创建一个函数用于接收不定数量的参数,并返回这些参数之和。此外,还需设计一种机制来处理多组测试用例,确保每组输入都能得到正确验证。 #### 函数定义 Python 支持传递任意数量的位置参数给函数,这可以通过星号 `*` 来完成。下面是一个简单的例子: ```python def sum_of_integers(*args): """Calculate the sum of given integers.""" total = 0 for num in args: if isinstance(num, (int)): # Ensure all elements are integers. total += num return total ``` 此段代码展示了如何利用 Python 的特性构建一个多参求数和的方法[^1]。 #### 测试框架搭建 考虑到要对上述功能执行多次检验,采用类似于 TYPED_TEST 的模式是非常合适的。这里推荐使用 PyTest 这样的单元测试工具来进行自动化测试案例管理。PyTest 能够轻松支持参数化测试,即允许同一个测试逻辑应用于多种不同的输入组合之上。 下面是基于 pytest.mark.parametrize 实现的一个简单示例: ```python import pytest @pytest.mark.parametrize("test_input,expected", [ ([], 0), ([1, 2, 3], 6), ((-1, -2, -3), -6), ]) def test_sum(test_input, expected): result = sum_of_integers(*test_input) assert result == expected ``` 这段脚本说明了怎样配置一组或多组测试数据去调用目标方法,并对比实际输出与预期结果是否一致。 #### 类型安全考量 当涉及到不同类型的数据时,应该特别关注类型的安全性和准确性。正如提到过的,在某些情况下,适当应用类型标注可以帮助开发者更好地理解和维护代码。因此可以在原始基础上进一步改进我们的加法器函数,加入更严格的类型约束: ```python from typing import Union, Tuple, List def improved_sum_of_integers(*args: Union[int]) -> int: """An enhanced version that strictly accepts only integer arguments and returns their sum.""" return sum(args) # Example usage with type hints included if __name__ == "__main__": print(improved_sum_of_integers(1, 2, 3)) # Output will be 6 ``` 此处引入了来自标准库模块 `typing` 中的相关组件,使得接口更加清晰明了[^4]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值