hihoCoder Week 172 Matrix Sum (二维树状数组)
来源:互联网 发布:不用网络的游戏 编辑:程序博客网 时间:2024/06/05 20:20
时间限制:10000ms
单点时限:1000ms
内存限制:256MB
- 样例输入
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
- 样例输出
- 1
- 2
- 3
- 二维树状数组讲解:
- http://blog.csdn.net/z309241990/article/details/9615259
- http://blog.csdn.net/int64ago/article/details/7429868
- 简单的二维树状数组求区间面积:
- Sum([x1,x2],[y1,y2])=query(x2,y2)+query(x1-1,y1-1)-query(x2,y1-1)-query(x1-1,y2);
- 这道题的取模有点坑:负数取模
#include<cstdio>#include<cstring>typedef long long ll;const int MAXN = 2000+10;#define MOD 1000000007ll c[MAXN][MAXN];int n;int lowbit(int x){return x&(-x);}void update(int x,int y,ll value){for(int i=x;i<=n;i+=lowbit(i)){for(int j=y;j<=n;j+=lowbit(j)){c[i][j]=c[i][j]+value;}}}ll query(int x,int y){ll sum=0;for(int i=x;i>0;i-=lowbit(i)){for(int j=y;j>0;j-=lowbit(j)){sum=sum+c[i][j];}}return sum%MOD;}int main(){int m;scanf("%d%d",&n,&m);memset(c,0,sizeof(c));char str[5];while(m--){getchar();scanf("%s",str);if(str[0]=='A'){int x,y,value;scanf("%d%d%d",&x,&y,&value);x++;y++;update(x,y,value);}else{int x1,y1,x2,y2;scanf("%d%d%d%d",&x1,&y1,&x2,&y2);x1++;y1++;x2++;y2++;ll ans=query(x2,y2)+query(x1-1,y1-1)-query(x2,y1-1)-query(x1-1,y2);printf("%lld\n",(ans+MOD)%MOD);}}}
描述
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 elementAxy. (Subscripts starts from 0
2. Sum x1 y1x2 y2: Return the sum of every elementAxy for x1 ≤x ≤ x2, y1 ≤ y ≤ y2.
输入
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
输出
For each Sum operation output a non-negative number denoting the sum modulo 109+7.
阅读全文
0 0
- hihoCoder Week 172 Matrix Sum (二维树状数组)
- hihoCoder 1336:Matrix Sum(二维树状数组)
- hihocoder #1336 : Matrix Sum 二维线段树 或 二维树状数组模板
- hihocoder1336-Matrix Sum(二维树状数组)
- Matrix(二维树状数组)
- HihoCoder 1336 二维树状数组
- hihocoder#1336 : Matrix Sum(二维线段树)
- B. Matrix(二维树状数组)
- poj2155--Matrix--二维树状数组
- POJ2155 Matrix(二维树状数组)
- poj2155 Matrix 【二维树状数组】
- poj2155~Matrix~二维树状数组!
- POJ2155 Matrix 二维树状数组
- poj2155 Matrix 【二维树状数组】
- 【poj2155】【二维树状数组】Matrix
- poj2155 Matrix 二维树状数组
- [POJ2155] Matrix - 二维树状数组
- 【POJ2155】Matrix(二维树状数组)
- C语言关键字static和volatile 的含义和作用
- iOS高级面试题(一)内存管理篇--runtime(一)
- github一些常见命令
- java 标识符
- centos rhel7 linux下怎么创建桌面快捷方式
- hihoCoder Week 172 Matrix Sum (二维树状数组)
- mybatis一对多关联查询
- VUE mixins 个人见解
- MFC使用mscomm串口通信
- C语言操作符使用总结
- 实现基于basic验证的目录访问
- idea激活方式详解
- oracle递归查询
- (M)Backtracking:77. Combinations