#include
#include
#include
#include
#include
using namespace std;
int N,S[5000][5000];
int main()
{
int sign;
while(scanf("%d",&sign)!=EOF)
{
if(sign==0)
{
scanf("%d",&N);
memset(S,0,sizeof(S));
continue;
}
if(sign==1)
{
int X,Y,V;
scanf("%d%d%d",&X,&Y,&V);
X++,Y++;
S[X][Y]+=V;
}
if(sign==2)
{
int X1,Y1,X2,Y2,ans=0;
scanf("%d%d%d%d",&X1,&Y1,&X2,&Y2);
X1++,Y1++,X2++,Y2++;
for(int i=X1;i<=X2;i++)
for(int j=Y1;j<=Y2;j++)
ans+=S[i][j];
printf("%d\n",ans);
}
if(sign==3) break;
}
return 0;
}
#include
#include
#include
#include
#include
using namespace std;
#define MAXN 5005
int N,S[MAXN][MAXN];
void add(int x,int y,int value)
{
for(int i=x;i<=N+N/2;i+=(i&(-i)))
for(int j=y;j<=N+N/2;j+=(j&(-j)))
S[i][j]+=value;
}
int getsum(int x,int y)
{
int tmp=0;
for(int i=x;i>=1;i-=(i&(-i)))
for(int j=y;j>=1;j-=(j&(-j)))
tmp+=S[i][j];
return tmp;
}
int main()
{
int sign;
while(scanf("%d",&sign)!=EOF)
{
if(sign==0)
{
scanf("%d",&N);
continue;
}
if(sign==1)
{
int X,Y,V;
scanf("%d%d%d",&X,&Y,&V);
X++,Y++;
add(X,Y,V);
}
if(sign==2)
{
int X1,Y1,X2,Y2;
scanf("%d%d%d%d",&X1,&Y1,&X2,&Y2);
X1++,Y1++,X2++,Y2++;
printf("%d\n",getsum(X1-1,Y1-1)-getsum(X1-1,Y2)+getsum(X2,Y2)-getsum(X2,Y1-1));
}
if(sign==3) break;
}
return 0;
}
#include
#include
#include
#include
#include
using namespace std;
#define MAX 640001
#define MAXN 2000001
#define MAXQ 60001
struct ASK{int num,x,y,v,kind,in;}a[MAX],T[MAX];
int sign,N,Q,tot=0,S[MAXN],ans[MAXQ];
void add(int x,int value){for(int i=x;i<=MAXN;i+=(i&(-i)))S[i]+=value;}
int getsum(int x){int tmp=0;for(int i=x;i>0;i-=(i&(-i)))tmp+=S[i];return tmp;};
bool cmp(ASK X,ASK Y){return X.x
CDQ分治的详细分析可见:https://blog.csdn.net/csyzcyj/article/details/107298731
转载注明出处:https://blog.csdn.net/csyzcyj/