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

模板题

#include<cstdio> 
#include<iostream>
#include<algorithm>
using namespace std;
const int N=1000010;
typedef long long LL;
int n,q;
LL c[N];
void add(int x,int y){
    
    
	for(int i=x;i<=n;i+=i&-i)
	c[i]+=y;
}
LL getsum(int x){
    
    
	LL res=0;
	while(x){
    
    
		res+=c[x];
		x-=x&-x;
	}
	return res;
}
int main()
{
    
    
     scanf("%d %d",&n,&q);
     for(int i=1;i<=n;i++){
    
    
     	LL a;
        scanf("%lld",&a);
        add(i,a);	
	 }
	 for(int i=1;i<=q;i++){
    
    
	 	int op,l,r;
	 	 scanf("%d%d%d",&op,&l,&r);
	 	  if(op==1)
	 	     add(l,r);
	 	  else
	 	   printf("%lld",getsum(r)-getsum(l-1));
	 }
    return 0;
}

猜你喜欢

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