[Ybt Advanced 4-2-1] Single-point modification interval query

Single-point modification interval query

Topic link: ybt efficient advanced 4-2-1

General idea

Template questions.
For a sequence, maintain the operations of adding a value to a number and summing the interval.

Ideas

It's just an ordinary tree-like array, nothing to say.
Interval sum can be realized by prefix sum.

Code

#include<cstdio>
#define ll long long

using namespace std;

ll tree[1000001];
int n, q, op, x, y;

void add(int x, ll y) {
    
    
	for (int i = x; i <= n; i += i & (-i))
		tree[i] += y;
}

ll ask(int x) {
    
    
	ll re = 0;
	for (int i = x; i; i -= i & (-i))
		re += tree[i];
	return re;
}

ll get_ans(int l, int r) {
    
    
	return ask(r) - ask(l - 1);
}

int main() {
    
    
	scanf("%d %d", &n, &q);
	for (int i = 1; i <= n; i++) {
    
    
		scanf("%d", &x);
		add(i, 1ll * x);
	}
	
	for (int i = 1; i <= q; i++) {
    
    
		scanf("%d %d %d", &op, &x, &y);
		if (op == 1) add(x, 1ll * y);
			else printf("%lld\n", get_ans(x, y));
	}
	
	return 0;
}

Guess you like

Origin blog.csdn.net/weixin_43346722/article/details/114750653