求助树状数组
查看原帖
求助树状数组
804607
rainygame楼主2023/3/21 16:53

几乎完全按照第一篇题解写的,不知为什么样例都过不了。

#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 init(){
	for (int i=1; i<=n; i++) sum[i] = sum[i-1] + a[i];
	for (int i=1; i<=n; i++) c[i] = sum[i] + sum[i-lowbit(i)];
}

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];
//	init();
	
	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;
}

2023/3/21 16:53
加载中...