主要是树状数组的改段求段操作。。。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long int LL;
const int maxn=4000400;
int n,m,w,nm;
LL B[2][maxn],C[2][maxn];
int lowbit(int x)
{
return x&(-x);
}
void add_b(int id,int p,LL v)
{
for(int i=p;i;i-=lowbit(i)) B[id][i]+=v;
}
void add_c(int id,int p,LL v)
{
for(int i=p;i<=nm;i+=lowbit(i)) C[id][i]+=v*p;
}
LL sum_b(int id,int p)
{
LL sum=0;
for(int i=p;i<=nm;i+=lowbit(i)) sum+=B[id][i];
return sum;
}
LL sum_c(int id,int p)
{
LL sum=0;
for(int i=p;i;i-=lowbit(i)) sum+=C[id][i];
return sum;
}
void ADD(int id,int l,int r,LL v)
{
add_b(id,r,v);
add_c(id,r,v);
if(l-1)
{
add_b(id,l-1,-v);
add_c(id,l-1,-v);
}
}
LL sUm(int id,int p)
{
if(p) return sum_b(id,p)*p+sum_c(id,p-1);
else return 0;
}
LL SUM(int id,int l,int r)
{
return sUm(id,r)-sUm(id,l-1);
}
int main()
{
scanf("%d%d%d",&n,&m,&w);
nm=max(n,m)+10;
while(w--)
{
int c,x1,x2,y1,y2,v,dx,dy;
scanf("%d%d%d%d%d",&c,&x1,&y1,&x2,&y2);
dx=x2-x1+1;dy=y2-y1+1;
if(c==0)
{
scanf("%d",&v);
ADD(0,x1,x2,v*dy);
ADD(1,y1,y2,v*dx);
}
else
{
printf("%I64d\n",SUM(1,y1,y2)-SUM(0,1,x1-1)-SUM(0,x2+1,nm));
}
}
return 0;
}