[Ybt high-efficiency advanced 4-2-5] single-point modification interval query

Single-point modification interval query

Topic link: ybt high-efficiency advanced 4-2-5

General idea

Given you a matrix, you are required to maintain the sum of the value of a certain place and the sum of the value of a certain sub-matrix.

Ideas

There is nothing to say, it is a two-dimensional tree array.

The prefix sum is the two-dimensional prefix sum, the exclusive one.

Then the tree-like array operation becomes a double loop.

Code

#include<cstdio>
#define ll long long

using namespace std;

int n, m, op, a, b, c, d;
ll tree[(1 << 12) + 1][(1 << 12) + 1];

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

ll get_ans(int x, int y) {
    
    
	ll re = 0;
	for (; x; x -= x & (-x))
		for (int ny = y; ny; ny -= ny & (-ny))
			re += tree[x][ny];
	return re;
}

int main() {
    
    
	scanf("%d %d", &n, &m);
	
	while (scanf("%d", &op) != EOF) {
    
    
		if (op == 1) {
    
    
			scanf("%d %d %d", &a, &b, &c);
			add(a, b, 1ll * c);
		}
		else {
    
    
			scanf("%d %d %d %d", &a, &b, &c, &d);
			printf("%lld\n", get_ans(c, d) - get_ans(c, b - 1) - get_ans(a - 1, d) + get_ans(a - 1, b - 1));
		}
	}
	
	return 0;
}

Guess you like

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