SPOJ-1029 MATSUM
来源:互联网 发布:数据备份方式 编辑:程序博客网 时间:2024/05/19 05:40
MATSUM - Matrix Summation
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):
- "SET x y num" - Set the value at cell (x, y) to num (0 <= x, y < N).
- "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.
- "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:14SET 0 0 1SUM 0 0 3 3SET 2 2 12SUM 2 2 2 2SUM 2 2 3 3SUM 0 0 2 2ENDOutput:1121213
#include <stdio.h>#include <string.h>#include <algorithm>using namespace std;#define maxn 1033int c[maxn][maxn], a[maxn][maxn];inline int lowbit(int x){ return x & (-x);}void add(int x, int y, int v){ while(x < maxn){ int cur = y; while(cur < maxn){ c[x][cur] += v; cur += lowbit(cur); } x += lowbit(x); }}int query(int x, int y){ int ans = 0; while(x){ int cur = y; while(cur){ ans += c[x][cur]; cur -= lowbit(cur); } x -= lowbit(x); } return ans;}int main(){ int T; scanf("%d", &T); while(T--){ int n, x1, y1, x2, y2, v; scanf("%d", &n); memset(c, 0, sizeof(c)); memset(a, 0, sizeof(a)); char s[10]; while(scanf("%s", s) != EOF){ if(s[0] == 'E'){ break; } if(s[1] == 'E'){ scanf("%d %d %d", &x1, &y1, &v); x1++; y1++; add(x1, y1, -a[x1][y1]); a[x1][y1] = v; add(x1, y1, v); } else{ scanf("%d %d %d %d", &x1, &y1, &x2, &y2); x1++; y1++; x2++; y2++; printf("%d\n", query(x2, y2) - query(x1 - 1, y2) - query(x2, y1 - 1) + query(x1 - 1, y1 - 1)); } } }} /*题意:1024*1024的矩阵,需要对单点修改值,查询子矩阵中的数字和。多cases。思路:二维树状数组的裸题。*/
- SPOJ-1029 MATSUM
- spij MATSUM - Matrix Summation
- spoj 1029
- SPOJ 1029 Matrix Summation
- SPOJ
- SPOJ
- SPOJ
- SPOJ
- SPOJ
- SPOJ
- SPOJ
- SPOJ
- SPOJ
- SPOJ
- SPOJ
- SPOJ
- SPOJ
- SPOJ
- 单例模式的总结
- 头一次被黑!阿里云与腾讯云两台服务器数据库均被清空,比特币额
- Git客户端的常用配置
- LeetCode 34 Search for a Range
- Echarts 饼状图的标注 字体等样式修改
- SPOJ-1029 MATSUM
- b树,b+树,红黑树
- Fragment引起错误(待完成)
- 深度学习Day2(神经网络的基础编程)
- 【互联网安全】阿里云安全专家为你详细解读内容安全及防护
- Spring Integration 学习笔记
- git_快速入门 03【三种境界】全世界朋友一起玩
- Git命令总结
- 软设之路1.1-计算机系统