spoj 1029. Matrix Summation(二维树状数组)

1、http://www.spoj.com/problems/MATSUM/

2、题目大意:

给一个n*n的矩阵,对这个矩阵有三种操作,一是SET x y num将(x,y)位置的数改成num,

二是SUM x1 y1 x2 y2,求(x1,y1)到(x2,y2)这一矩形内的数字和,并将其输出

题目中说是每组样例输出一个换行,但是输出就不正确了,纠结。。。

3、题目:

SPOJ Problem Set (classical)

1029. Matrix Summation

Problem code: MATSUM


A N × N matrix is filled with numbers. BuggyD is analyzing the matrix, and he wants the sum of certain submatrices every now and then, so he wants a system where he can get his results from a query. Also, the matrix is dynamic, and the value of any cell can be changed with a command in such a system.

Assume that initially, all the cells of the matrix are filled with 0. Design such a system for BuggyD. Read the input format for further details.

Input

The first line of the input contains an integer t, the number of test cases. t test cases follow.

The first line of each test case contains a single integer N (1 <= N <= 1024), denoting the size of the matrix.

A list of commands follows, which will be in one of the following three formats (quotes are for clarity):

  1. "SET x y num" - Set the value at cell (x, y) to num (0 <= x, y < N).
  2. "SUM x1 y1 x2 y2" - Find and print the sum of the values in the rectangle from (x1, y1) to (x2, y2), inclusive. You may assume that x1 <= x2 and y1 <= y2, and that the result will fit in a signed 32-bit integer.
  3. "END" - Indicates the end of the test case.

Output

For each test case, output one line for the answer to each "SUM" command. Print a blank line after each test case.

Example

Input:
1
4
SET 0 0 1
SUM 0 0 3 3
SET 2 2 12
SUM 2 2 2 2
SUM 2 2 3 3
SUM 0 0 2 2
END

Output:
1
12
12
13
4、AC代码:

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define N 1030
int n;
int c[N][N];
int b[N][N];
int lowbit(int i)
{
    return i&(-i);
}
void update(int x,int y,int v)
{
    for(int i=x;i<=n;i+=lowbit(i))
    {
        for(int j=y;j<=n;j+=lowbit(j))
        {
            c[i][j]+=v;
        }
    }
}
int getsum(int x,int y)
{
    int sum=0;
    for(int i=x;i>0;i-=lowbit(i))
    {
        for(int j=y;j>0;j-=lowbit(j))
        {
           // printf("c=%d %d %d\n",i,j,c[i][j]);
            sum+=c[i][j];
        }
    }
    return sum;
}
int main()
{
    int t,x,y,v,x1,y1,x2,y2;
    char s[10];
    scanf("%d",&t);
    while(t--)
    {
        memset(c,0,sizeof(c));
        memset(b,0,sizeof(b));
        scanf("%d",&n);
        while(scanf("%s",s))
        {
            if(strcmp(s,"END")==0)
            break;
            if(strcmp(s,"SET")==0)
            {
                scanf("%d%d%d",&x,&y,&v);
                x++;
                y++;
                int tmp=v-b[x][y];
                //printf("*%d %d %d\n",x,y,v);
                update(x,y,tmp);
                b[x][y]=v;
            }
            else if(strcmp(s,"SUM")==0)
            {
                scanf("%d%d%d%d",&x1,&y1,&x2,&y2);

                x1++;
                y1++;
                x2++;
                y2++;
                //printf("&%d %d %d %d\n",x1,y1,x2,y2);
                //printf("getsum(x2,y2)=%d\n",getsum(x2,y2));
                //printf("sum=%d %d %d %d\n",getsum(x2,y2),getsum(x2,y1-1),getsum(x1-1,y2),getsum(x1-1,y1-1));
                int ans=getsum(x2,y2)-getsum(x2,y1-1)-getsum(x1-1,y2)+getsum(x1-1,y1-1);
                printf("%d\n",ans);
            }
        }
       // printf("\n");
    }
    return 0;
}
/*
3
4
SET 0 0 1
SUM 0 0 3 3
SET 2 2 12
SUM 2 2 2 2
SUM 2 2 3 3
SUM 0 0 2 2
END
4
SET 0 0 3
SUM 0 0 3 3
SET 2 2 12
SUM 2 2 2 2
SUM 2 2 3 3
SUM 0 0 2 2
END
4
SET 0 0 1
SUM 0 0 3 3
SET 2 2 12
SUM 2 2 2 2
SUM 2 2 3 3
SUM 0 0 2 2
END
*/


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值