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

P3368 【模板】树状数组 2

时间:2020-06-28 20:38:15      阅读:50      评论:0      收藏:0      [点我收藏+]

标签:cpp   pre   namespace   lan   while   bsp   space   scanf   模板   

P3368 【模板】树状数组 2

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

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

 

P3368 【模板】树状数组 2

标签:cpp   pre   namespace   lan   while   bsp   space   scanf   模板   

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

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