树状数组单点修改模板

#pragma GCC optimize(3)
#include
#define mst(a,b) memset(a,b,sizeof(a))
#define For(i, k, j) for(int i = (k); i <= (j); i++)
#define INF 2147483647/3
#define ll long long
using namespace std;
inline int read()
{
    int num = 0, flag = 1;
    char c=' ';
    for(;c>'9'||c<'0';c=getchar()) if(c=='-') flag = -1;
    for(;c>='0'&&c<='9';num=(num<<1)+(num<<3)+c-48,c=getchar());
    return num * flag;
}
const int MAXN = 500005;
int a[MAXN], c[MAXN];
inline int lowbit(int x) {
	return x&-x; 
}
int n, m;
inline int getsum(int x) {
	int ans = 0;
	for(int i = x; i > 0; i -= lowbit(i)) {
		ans += c[i];
	}
	return ans;
}
inline int query(int l, int r) {
	return getsum(r) - getsum(l - 1);
}
inline void update(int x, int v) {
	for(int i = x; i <= n; i += lowbit(i)) {
		c[i] += v;
	}
}
int main()
{
	n = read(), m = read();
	For(i, 1, n) {
		scanf("%d", &a[i]);
		update(i, a[i]);
	}
	For(i, 1, m) {
		int opt = read();
		if(opt == 1) {
			int x = read(), v = read();
			update(x, v);
		} else {
			int l = read(), r = read();
			printf("%d\n", query(l, r));
		}
		
	}
    return 0;
}


 

你可能感兴趣的:(图论,线段树,算法)