题解
查看原帖
题解
648871
lvchengze楼主2024/6/1 08:40
#include <bits/stdc++.h>
using namespace std;
#define MAXN 500001

int n, m, opt, x, y, k;
long long a[MAXN], sum[MAXN], c[MAXN];
int lowbit(int x){
	return x & -x;
}
void add(int i, int k){
	while (i <= n){
		c[i] += k;
		i += lowbit(i);
	}
}
long long get_sum(int x){
	long long ans = 0;
	while (x){
		ans += c[x];
		x -= lowbit(x);
	}
	return ans;
}
int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	cin >> n >> m;
	for (int i=1; i<=n; i++) cin >> a[i];
	while (m--){
		cin >> opt;
		if (opt == 1){
			cin >> x >> y >> k;
			add(x, k);
			add(y+1, -k);
		}else{
			cin >> x;
			cout << a[x]+get_sum(x) << '\n';
		}
	}
	return 0;
}
2024/6/1 08:40
加载中...