poj1195(二维树状数组,点修改,区间求和)

题目链接:点击打开链接

//题目大意:一个平面区间,修改某个点的 number of active phones,区间求和


#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#define n 1500

using namespace std;

int c[1500][1500];

void add(int x, int y, int d)
{
	for(int i= x; i<= n; i+= i&-i)
		for(int j= y; j<= n; j+= j&-j)
			c[i][j]+= d;
}

int sum(int x, int y)
{
	int s= 0;
	for(int i= x; i> 0; i-= i&-i)
		for(int j= y; j> 0; j-= j&-j)
			s+= c[i][j];
	return s;
}

int main()
{
    int instruction, s;
    while(scanf("%d", &instruction)== 1)
    {
        if(instruction== 0)
        {
            scanf("%d", &s);
            memset(c, 0, sizeof(c));
            while(scanf("%d", &instruction)== 1 && instruction!= 3)
            {
                if(instruction== 1)
                {
                    int x, y, a; scanf("%d%d%d", &x, &y, &a); x++; y++;
                    add(x, y, a);
                }
                if(instruction== 2)
                {
                    int k, l, m, p; scanf("%d%d%d%d", &k, &l, &m, &p); k++; l++; m++; p++;
                    printf("%d\n", sum(m, p)- sum(m, l- 1)- sum(k- 1, p)+ sum(k- 1, l- 1));
                }
            }
        }
    }
    return 0;
}


你可能感兴趣的:(poj,树状数组)