树状数组模板题
当n1, a[x][y]+=w;
当n2 计算a[x1][y1]到a[x2][y2]之间矩阵的元素和
当n==3退出
#include<iostream>
#include<string.h>
#include<cstdio>
using namespace std;
typedef long long ll;
const int N = 1025;
int c[N][N]; int Row, Col;
inline int Lowbit(const int &x){// x > 0
return x&(-x);
}
ll Sum(int i,int j){
ll tempj, sum = 0;
while( i > 0 ){
tempj= j;
while( tempj > 0 ){
sum += c[i][tempj];
tempj -= Lowbit(tempj);
}
i -= Lowbit(i);
}
return sum;
}
void Update(int i, int j, int num){
int tempj;
while( i <= Row ){
tempj = j;
while( tempj <= Col ){
c[i][tempj] += num;
tempj += Lowbit(tempj);
}
i += Lowbit(i);
}
}
int main()
{
int n,s, x1, y1, x2, y2, w;
while(~scanf("%d%d", &n, &s))
{
memset(c,0,sizeof(c));
Row=s;
Col=s;
while(~scanf("%d", &n)){
if(n==1)
{
scanf("%d%d%d", &x1, &y1, &w);
Update(x1+1,y1+1,w); //下标不能从0开始
}
else if(n==2)
{
scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
printf("%lld\n", Sum(x2+1,y2+1)-Sum(x2+1,y1)-Sum(x1, y2+1)+Sum(x1, y1));
}
else if(n==3) break;
}
}
return 0;
}