树状数组:区间修改,单点查询

树状数组:区间修改,单点查询

利用差分数组
参考:添加链接描述

#include<cstdio> 
#include<iostream>
#include<algorithm>
using namespace std;
int n,q;
const int N=1000010;
typedef long long LL;
LL sum[N];
 void add(int x,int y){
    
    
 	while(x<=n){
    
    
 		sum[x]+=y;
 		x+=x&-x;
	 }
 }
 void renge_add(int l,int r,LL x){
    
     	
 	add(l,x);
 	add(r+1,-x);	
 }
LL query(int p){
    
    
	LL res=0;
	while(p){
    
    
		res+=sum[p];
		p-=p&-p;
	}
	return res;
}
int main()
{
    
    
	scanf("%d%d",&n,&q);

	for(int i=1;i<=n;i++){
    
    
		LL a;
		scanf("%lld",&a);
		renge_add(i,i,a);	
	}
	for(int i=1;i<=q;i++){
    
    
		int op,l,r;
		LL x;
		scanf("%d",&op);
		if(op==1){
    
    
			scanf("%d%d%lld",&l,&r,&x);
			renge_add(l,r,x);		
		}
		else
		{
    
    
			scanf("%d",&x);
			 printf("%lld\n",query(x));		
		}
	}
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_47154574/article/details/108779051