码迷,mamicode.com
首页 > 编程语言 > 详细

P3374 【模板】树状数组 1

时间:2020-06-28 20:39:52      阅读:53      评论:0      收藏:0      [点我收藏+]

标签:https   sum   ble   main   scan   class   ==   i++   std   

P3374 【模板】树状数组 1

#include<bits/stdc++.h>
using namespace std;
const int N=5e5+10;
int n,m;
int a[N];
int c[N<<2];

int lowbit(int x){
	return x&-x;
}

void updata(int x,int k){
	while(x<=n){
		c[x]+=k;
		x+=lowbit(x);
	}
}

int getsum(int x){
	int ans=0;
	while(x){
		ans+=c[x];
		x-=lowbit(x);
	}
	return ans;
}

int main(){
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;i++){
		scanf("%d",&a[i]);
		updata(i,a[i]);
	}
	for(int i=1;i<=m;i++){
		int go;
		scanf("%d",&go);
		if(go==1){
			int x,y;
			scanf("%d%d",&x,&y);
			updata(x,y);
		}
		if(go==2){
			int x,y;
			scanf("%d%d",&x,&y);
			printf("%d\n",getsum(y)-getsum(x-1));
		}
	}
	return 0;
}

 

P3374 【模板】树状数组 1

标签:https   sum   ble   main   scan   class   ==   i++   std   

原文地址:https://www.cnblogs.com/LightyaChoo/p/13204734.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!