C - 区间更新区间求和板子 *

You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of operation is to add some given number to each number in a given interval. The other is to ask for the sum of numbers in a given interval.

Input

The first line contains two numbers N and Q. 1 ≤ N,Q ≤ 100000.
The second line contains N numbers, the initial values of A1, A2, ... , AN. -1000000000 ≤ Ai ≤ 1000000000.
Each of the next Q lines represents an operation.
"C a b c" means adding c to each of AaAa+1, ... , Ab. -10000 ≤ c ≤ 10000.
"Q a b" means querying the sum of AaAa+1, ... , Ab.

Output

You need to answer all Q commands in order. One answer in a line.

Sample Input

10 5
1 2 3 4 5 6 7 8 9 10
Q 4 4
Q 1 10
Q 2 4
C 3 6 3
Q 2 4

Sample Output

4
55
9
15

Hint

The sums may exceed the range of 32-bit integers

这个题有的数据要开 long long ,否则会 wa掉,数据较大的话用 scanf、printf 会快一点,

或者用cin、cout但是要加上 std::ios::sync_with_stdio(false); 这样会快一点;

当然,我也不是太理解线段树,有的地方也没有看懂(哎)........

#include<iostream>
#include<stdio.h>
#include<cstring>
using namespace std;
#define ls o<<1
#define rs o<<1|1
typedef long long ll;
const int M=1e5+10;
ll a[M];//必须用long long 
struct node{
	int l,r;
	ll x;//必须用long long,否则wrong answer 
	ll lazy; 
}t[M<<2];
inline void push_up(int o){//向上传递 
	t[o].x =t[ls].x +t[rs].x ;
}
inline void build(int l,int r,int o){
	t[o].l =l; t[o].r =r; t[o].lazy =0;
	if(l==r) {
		t[o].x =a[l];
		return ;
	}
	int mid=(l+r)>>1;
	build(l,mid,ls); build(mid+1,r,rs);
	push_up(o);//叶节点的上一个 
}
inline void push_down(int o,int m){//向下传递 
	if(t[o].lazy ){
		t[ls].lazy +=t[o].lazy ;
		t[rs].lazy +=t[o].lazy ;
		t[ls].x +=t[o].lazy *(m-m/2);//左边一半 
		t[rs].x +=t[o].lazy *(m/2);//右边一半 
		t[o].lazy =0;//标记为0 
	}
}
inline ll query(int l,int r,int o){
	if(l<=t[o].l &&t[o].r <=r){
		return t[o].x ;
	}
	push_down(o,t[o].r -t[o].l +1);
	int mid=(t[o].l +t[o].r )>>1;
	if(r<=mid) return query(l,r,ls);
	else if(l>mid) return query(l,r,rs);
	else return query(l,mid,ls)+query(mid+1,r,rs);
} 
inline void update(int l,int r,int o,int x){
	if(l<=t[o].l&&t[o].r<=r){
		t[o].lazy +=x;
		t[o].x +=x*(t[o].r -t[o].l +1);
		return ;
	}
	push_down(o,t[o].r -t[o].l +1);//我也不知道为什么加这句 
	int mid=(t[o].l +t[o].r )>>1;
	if(r<=mid) update(l,r,ls,x);
	else if(l>mid) update(l,r,rs,x);
	else{
		update(l,mid,ls,x);
		update(mid+1,r,rs,x);
	}
	push_up(o);
}

int main()
{
	int n,q;
//	std::ios::sync_with_stdio(false);
	while(cin>>n>>q){
		memset(a,0,sizeof(a));
		memset(t,0,sizeof(t));
		for(int i=1;i<=n;i++){
			//cin>>a[i];
			scanf("%lld",&a[i]);
		}
		build(1,n,1);
		char s[2];
		int l,r,x;
		while(q--){
			scanf("%s",s);
			if(s[0]=='Q'){
				//cin>>l>>r;
				scanf("%d%d",&l,&r);
				//cout<<query(l,r,1)<<endl;
				printf("%lld\n",query(l,r,1));
			}
			if(s[0]=='C'){
				//cin>>l>>r>>x;
				scanf("%d%d%d",&l,&r,&x);
				update(l,r,1,x);
			}
		}
	}
	return 0;
}

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值