hihoCoder 1336:Matrix Sum(二维树状数组)
来源:互联网 发布:程序员的简历怎么写 编辑:程序博客网 时间:2024/05/16 14:57
Matrix Sum
Problem Description
You are given an N × N matrix. At the beginning every element is 0. Write a program supporting 2 operations:
1. Add x y value: Add value to the element Axy. (Subscripts starts from 02. Sum x1 y1 x2 y2: Return the sum of every element Axy for x1 ≤ x ≤ x2, y1 ≤ y ≤ y2.
Input
The first line contains 2 integers N and M, the size of the matrix and the number of operations.
Each of the following M line contains an operation.
1 ≤ N ≤ 1000, 1 ≤ M ≤ 100000
For each Add operation: 0 ≤ x < N, 0 ≤ y < N, -1000000 ≤ value ≤ 1000000
For each Sum operation: 0 ≤ x1 ≤ x2 < N, 0 ≤ y1 ≤ y2 < N
Output
For each Sum operation output a non-negative number denoting the sum modulo
Sample Input
5 8Add 0 0 1Sum 0 0 1 1Add 1 1 1Sum 0 0 1 1Add 2 2 1Add 3 3 1Add 4 4 -1Sum 0 0 4 4
Sample Output
123
题意:
给你一个N*N二维矩阵,初始值时都为0,有两种操作
1:单点修改值
2:给出一个子矩阵的左上和右下角的坐标,询问一个子矩形范围内的值的和.
解题思路:
还是看官方的题解吧。。。《Matrix Sum》题目分析
Code:
#include <iostream>#include <cstdio>#include <cstring>#define mem(a,b) memset(a,b,sizeof(a))using namespace std;typedef long long LL;const int maxn=1000+5, mod=1e9+7;LL BIT[maxn][maxn];int N,m;int lowbit(int x){ return x&(-x);}void add(int x,int y,int val){ for(int i=x;i<maxn;i+=lowbit(i)) { for(int j=y;j<maxn;j+=lowbit(j)) { BIT[i][j]=(BIT[i][j]+val)%mod; } }}int sum(int x,int y){ LL ret=0; for(int i=x;i>0;i-=lowbit(i)) { for(int j=y;j>0;j-=lowbit(j)) { ret=(ret+BIT[i][j])%mod; } } return (int)ret;}int main(){ mem(BIT,0); scanf("%d%d",&N,&m); char op[10]; for(int i=0;i<m;i++) { scanf("%s",op); if(op[0]=='A') { int x,y,val; scanf("%d%d%d",&x,&y,&val); x++,y++; add(x,y,val); } else { int x1,y1,x2,y2; scanf("%d%d%d%d",&x1,&y1,&x2,&y2); x1++,x2++,y1++,y2++; int res=(sum(x2,y2)-sum(x1-1,y2)-sum(x2,y1-1)+sum(x1-1,y1-1)+mod)%mod; printf("%d\n",res); } } return 0;}
阅读全文
0 0
- hihoCoder 1336:Matrix Sum(二维树状数组)
- hihocoder #1336 : Matrix Sum 二维线段树 或 二维树状数组模板
- hihoCoder Week 172 Matrix Sum (二维树状数组)
- HihoCoder 1336 二维树状数组
- hihocoder1336-Matrix Sum(二维树状数组)
- hihocoder#1336 : Matrix Sum(二维线段树)
- Matrix(二维树状数组)
- 1336 : Matrix Sum (hihocoder)
- poj 2155 Matrix(二维树状数组)
- POJ 2155 Matrix (二维树状数组)
- POJ 2155 Matrix(二维树状数组)
- POJ2155--Matrix(二维树状数组)
- 【树状数组(二维)】poj 2155 Matrix
- POJ 2155 Matrix (二维树状数组)
- [poj 2155] Matrix(二维树状数组)
- POJ2155 Matrix (二维树状数组)
- poj2155--Matrix(二维树状数组)
- POJ 2155 Matrix (二维树状数组)
- Deap : 遗传算法算法解决 背包问题
- 【ASP.NET】总结
- JavaScript框架之AngularJS学习——指令(二)
- k-近邻算法 手写识别系统
- Python学习笔记
- hihoCoder 1336:Matrix Sum(二维树状数组)
- <Qt>windows平台调用mplayer播放视频
- uva 1610
- OSPF笔记-5
- ubuntu初级篇
- redis
- 免密登录linux服务器
- 2017.10.16 測試總結并今日總結
- css使其背景固定