A Simple Problem with Integers

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

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

 

//区间更新  //参见 https://blog.csdn.net/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;
} 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值