题目描述
Mobile phones
解法:二维树状数组
#include <iostream>
#include <string.h>
using namespace std;
const int N = 1124;
long long c[N][N];
int row, col;
int lowbit(int x){return x&(-x);}
long long sum(int i, int j){
int jj, s = 0;
while(i>0)
{
jj = j;
while(jj>0)
{
s += c[i][jj];
jj -= lowbit(jj);
}
i -= lowbit(i);
}
return s;
}
void update(int i, int j, int v){
int jj;
while(i<=row)
{
jj = j;
while(jj<=col)
{
c[i][jj] += v;
jj += lowbit(jj);
}
i += lowbit(i);
}
}
int main()
{
int cmd, n,x, y, v, l, b, r, t;
while(~scanf("%d%d", &cmd, &n))
{
row = col = n;
memset(c, 0, sizeof(c));
while(~scanf("%d", &cmd))
{
if(cmd==3) break;
if(cmd==1)
{
scanf("%d%d%d", &x, &y, &v);
update(x+1, y+1, v);
}
if(cmd==2)
{
scanf("%d%d%d%d", &l, &b, &r, &t);
printf("%lld\n", sum(r+1, t+1)+sum(l, b)-sum(l, t+1)-sum(r+1, b));
}
}
}
return 0;
}