[二维树状数组] *单点修改 区间查询*

题目

[二维树状数组] *单点修改 区间查询*_第1张图片

代码

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#if __cplusplus >= 201103L
#include 
#include 
#endif
#define ll long long
#define int long long
using namespace std;
const int INF = 0x3f3f3f3f;
int lowbit(int x){
	return (-x)&x;
}
int bit[5000][5000];
int n,m;
void edit(int pos1,int pos2,int val){
	for(int i=pos1;i<=n;i+=lowbit(i)){
		for(int j=pos2;j<=m;j+=lowbit(j)){
			bit[i][j]+=val;
		}
	}
}
int query(int pos1,int pos2){
	int res=0;  
	for(int i=pos1;i>=1;i-=lowbit(i)){
		for(int j=pos2;j>=1;j-=lowbit(j)){
			res+=bit[i][j];	
		}
	}
	return res;
}
signed main(){
	//ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
	scanf("%lld%lld",&n,&m); 
	int a;
	while(~scanf("%lld",&a)){
		if(a==1){
			int x,y,k;
			scanf("%lld%lld%lld",&x,&y,&k); 
			edit(x,y,k);
		}
		if(a==2){
			int x1,x2,y1,y2;
			scanf("%lld%lld%lld%lld",&x2,&y2,&x1,&y1);
			printf("%lld\n",query(x1,y1)+query(x2-1,y2-1)-query(x1,y2-1)-query(x2-1,y1));
		}
	}
    return 0;
}

你可能感兴趣的:([二维树状数组] *单点修改 区间查询*)