Description
Input

Output
Sample Input
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
Sample Output
3 4
Source
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <string>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <math.h>
#include <bitset>
#include <list>
#include <algorithm>
#include <climits>
using namespace std;
#define lson 2*i
#define rson 2*i+1
#define LS l,mid,lson
#define RS mid+1,r,rson
#define UP(i,x,y) for(i=x;i<=y;i++)
#define DOWN(i,x,y) for(i=x;i>=y;i--)
#define MEM(a,x) memset(a,x,sizeof(a))
#define W(a) while(a)
#define gcd(a,b) __gcd(a,b)
#define LL long long
#define N 2005
#define INF 0x3f3f3f3f
#define EXP 1e-8
#define lowbit(x) (x&-x)
const int mod = 1e9+7;
int a[N];
int n,c[N][N],ans[N],x,y;
int sum(int x,int y)
{
    int ret=0,i,j;
    for(i = x; i>0; i-=lowbit(i))
    {
        for(j = y; j>0; j-=lowbit(j))
        {
            ret+=c[i][j];
        }
    }
    return ret;
}
void add(int x,int y,int d)
{
    int i,j;
    for(i = x; i<=n; i+=lowbit(i))
    {
        for(j = y; j<=n; j+=lowbit(j))
        {
            c[i][j]+=d;
        }
    }
}
int main()
{
    int i,j,k,op,x,y;
    while(~scanf("%d%d",&i,&n))
    {
        MEM(c,0);
        while(1)
        {
            scanf("%d",&op);
            if(op==3)
                break;
            if(op==1)
            {
                scanf("%d%d%d",&x,&y,&k);
                add(x+1,y+1,k);
            }
            else
            {
                int l,b,r,t;
                scanf("%d%d%d%d",&l,&b,&r,&t);
                l++,b++,r++,t++;
                printf("%d\n",sum(r,t)-sum(r,b-1)-sum(l-1,t)+sum(l-1,b-1));
            }
        }
    }
    return 0;
}
原文:http://blog.csdn.net/libin56842/article/details/46584235