在这个时刻|

Arthur_Douglas

园龄:1年4个月粉丝:3关注:3

P3368 【模板】树状数组 2

#include<bits/stdc++.h>
#define int long long
using namespace std;
const int Max = 500005;
int a[Max];
int n,m;
int lowbit(int x) {
	return x & -x;
}
void add(int x,int y) {
	while(x <= n) {
		a[x] += y;
		x += lowbit(x);
	}
}

int sum(int x) {
	int ans = 0;
	while(x > 0) {
		ans += a[x];
		x -= lowbit(x);
	}
	return ans;
}

signed main() {
	cin>>n>>m;
	int last = 0;
	for(register int i = 1; i <= n; ++ i) {
		int qwq;
		cin>>qwq;
		add(i,qwq - last);
		last = qwq;
	}
	for(int i = 1; i <= m; ++ i) {
		int qwq;
		cin>>qwq;
		if(qwq == 1) {
			int x ,y,z ;
			cin>>x>>y>>z;
			add(x,z);
			add(y + 1,-z);
		} else {
			int x ;
			cin>>x;
			cout << sum(x) << endl;
		}
	}
	return 0;
}

posted @   Arthur_Douglas  阅读(5)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示
评论
收藏
关注
推荐
深色
回顶
收起