spij MATSUM - Matrix Summation

SPOJ 1029

MATSUM - Matrix Summation

no tags 

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
二维数组模板题

坑点 一定都用INT 不要LONG LONG 一个


都不要LONG LONG


还有就是他给的SET是重新设置不是add


 在通常树状数组的模板都是add的意思




#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
#include<stdlib.h>
#include<math.h>
#define N 2100
#define ll long long
using namespace std;
int cc[N][N];
int ccn;
int num[N][N];
int lowbit(int x)
{
	return x&(-x);
}
void update2(int x,int y,int val)
{
	int tem = val;
	val -= num[x][y];
	num[x][y] = tem;
	for(int i = x;i < N; i += lowbit(i))
	{
		for(int j = y; j < N ;j += lowbit(j))
		{
			cc[i][j] += val;
		}
	}
}
int sum2(int x,int y)
{
	int ans = 0;
	for(int i = x;i > 0;i -= lowbit(i))
	{
		for(int j = y; j > 0; j -= lowbit(j))
		{
			ans += cc[i][j];
		}
	}
	return ans;
}


int main()
{
	int T;
	scanf("%d",&T);
	while(T--)
	{	
		scanf("%d",&ccn);
		int xx,yy,v,xxx,yyy;
		int s1,s2,s3,s4,s5;
		char C[10];
		
		for(int i = 0;i <= ccn;i++)
			for(int j = 0;j <= ccn;j++)
			{
				cc[i][j] = num[i][j] = 0;
			}
		while(scanf("%s",C))
		{
			if(C[0]=='E')
			 break;
			switch(C[1])
			{
				
				case 'E' :
					 int xx,yy,v;
					 scanf("%d%d%d",&xx,&yy,&v);
					 xx++;yy++;
					 update2(xx,yy,v);
					 break;
				case 'U' :
					 scanf("%d%d%d%d",&xx,&yy,&xxx,&yyy);
					 xx++;yy++;xxx++;yyy++;
					 s1 = sum2(xx-1,yy-1);
					 s2 = sum2(xx-1,yyy);
					 s3 = sum2(xxx,yy-1);
					 s4 = sum2(xxx,yyy);
					 s5 = s4 - s3 - s2 + s1;
				//	 printf("%lld %lld %lld %lld\n",s1,s2,s3,s4);
					 printf("%d\n",s5);
					 break;
				
			}
	
		}
	}
}




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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值