做得有点苦逼,各种错误,但最后还是被我发现了
#include <iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int c[1025][1025],n;
inline int lowbit(int k)
{
return k&(-k);
}
void insert(int x,int y,int d)
{
for(int i=x;i<=n;i+=lowbit(i))
for(int j=y;j<=n;j+=lowbit(j))
c[i][j]+=d;
}
int getsum(int x,int y)
{
int t=0;
for(int i=x;i>0;i-=lowbit(i))
for(int j=y;j>0;j-=lowbit(j))
t+=c[i][j];
return t;
}
int main()
{
int x1,y1,x2,y2,d,x;
while(~scanf("%d",&x))
{
if(x==3)
break;
if(x==0)
{
scanf("%d",&n);
//memset(c,0,sizeof(int)*(n+1)*(n+1));对于二维数组不能这样初始化
for(int i=0;i<=n;i++)
for(int j=0;j<=n;j++)
c[i][j]=0;
}
else if(x==1)
{
scanf("%d%d%d",&x1,&y1,&d);
x1++;y1++;
insert(x1,y1,d);
}
else
{
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
x1++;y1++;x2++;y2++;
int sum=getsum(x2,y2)-getsum(x1-1,y2)-getsum(x2,y1-1)+getsum(x1-1,y1-1);
printf("%d\n",sum);
}
}
return 0;
}
/*
0 4
1 1 2 3
2 0 0 2 2
1 1 1 2
1 1 2 -1
2 1 1 2 3
3
*/