HihoCoder1336 Matrix Sum(二维树状数组求和)
描述
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 0
2. Sum x1 y1 x2 y2: Return the sum of every element Axy 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.
- 样例输入
-
5 8
Add 0 0 1
Sum 0 0 1 1
Add 1 1 1
Sum 0 0 1 1
Add 2 2 1
Add 3 3 1
Add 4 4 -1
Sum 0 0 4 4 - 样例输出
-
1
2
3
#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
int n,c[][];
const int Mod=1e9+;
int lowbit(int x)
{
return x&(-x);
}
void add(int x,int y,int val)
{
for(int i=x;i<=n;i+=lowbit(i))
for(int j=y;j<=n;j+=lowbit(j))
c[i][j]=(c[i][j]+val)%Mod;
}
int getsum(int x, int y) {
int res = ; for (int i = x; i; i -= lowbit(i)) {
for (int j = y; j; j -= lowbit(j)) {
res += c[i][j];
}
} return res;
}
int main()
{
int m,i,a,b,x,y,z;
char c[];
scanf("%d%d",&n,&m);n++;
for(i=;i<=m;i++){
scanf("%s",c);
if(c[]=='A'){
scanf("%d%d%d",&x,&y,&z);
add(x+,y+,z);
}
else {
scanf("%d%d%d%d",&x,&y,&a,&b);
printf("%d\n",((getsum(a+,b+)+getsum(x,y)-getsum(a+,y)-getsum(x,b+))%Mod+Mod)%Mod);
}
}
return ;
}
HihoCoder1336 Matrix Sum(二维树状数组求和)的更多相关文章
- POJ 2155 Matrix (二维树状数组)
Matrix Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 17224 Accepted: 6460 Descripti ...
- POJ 2155 Matrix【二维树状数组+YY(区间计数)】
题目链接:http://poj.org/problem?id=2155 Matrix Time Limit: 3000MS Memory Limit: 65536K Total Submissio ...
- HLJU 1188 Matrix (二维树状数组)
Matrix Time Limit: 4 Sec Memory Limit: 128 MB Description 给定一个1000*1000的二维矩阵,初始矩阵中每一个数都为1,然后为矩阵有4种操 ...
- POJ 2155 Matrix(二维树状数组+区间更新单点求和)
题意:给你一个n*n的全0矩阵,每次有两个操作: C x1 y1 x2 y2:将(x1,y1)到(x2,y2)的矩阵全部值求反 Q x y:求出(x,y)位置的值 树状数组标准是求单点更新区间求和,但 ...
- POJ 2155 Matrix 【二维树状数组】(二维单点查询经典题)
<题目链接> 题目大意: 给出一个初始值全为0的矩阵,对其进行两个操作. 1.给出一个子矩阵的左上角和右上角坐标,这两个坐标所代表的矩阵内0变成1,1变成0. 2.查询某个坐标的点的值. ...
- PKU 2155 Matrix(裸二维树状数组)
题目大意:原题链接 题意很简单,就不赘诉了. 解题思路: 使用二维树状数组,很裸的题. 二维的写起来也很方便,两重循环. Add(int x,int y,int val)表示(x,y)-(n,n)矩形 ...
- POJ_2155 Matrix 【二维树状数组】
一.题面 POJ2155 二.分析 楼教主出的题,是二维树状数组非常好的题,还结合了开关问题(开关变化的次数如果为偶数,状态不变,奇数状态相反). 题意就是给了一个二维的坐标平面,每个点初始值都是0, ...
- POJ2155 Matrix(二维树状数组||区间修改单点查询)
Given an N*N matrix A, whose elements are either 0 or 1. A[i, j] means the number in the i-th row an ...
- [POJ2155]Matrix(二维树状数组)
题目:http://poj.org/problem?id=2155 中文题意: 给你一个初始全部为0的n*n矩阵,有如下操作 1.C x1 y1 x2 y2 把矩形(x1,y1,x2,y2)上的数全部 ...
随机推荐
- mysql --single-transaction 在从库导入完数据以后要在配置文件/etc/my.cnf 中加上read_only=1的参数
1.在做数据库的主从时,防止在这个过程中,有数据访问进来,要: 要想连super权限用户的写操作也禁止,就使用"flush tables with read lock;",这样设置 ...
- (转) pip Fatal error in launcher: Unable to create process using
接上篇“Eclipse启动报错:JVM terminated. Exit code=2”,今天把Python的安装位置也从C盘剪切到了D盘, 然后修改了Path环境变量中对应的盘符:D:\Python ...
- 针对yarn的8088端口攻击
参考: https://www.wangbokun.com/%E8%BF%90%E7%BB%B4/2019/09/02/%E6%8C%96%E7%9F%BF%E7%97%85%E6%AF%92.htm ...
- hello2源代码分析
String username = request.getParameter("username");/* *以 String 形式返回请求参数"username&quo ...
- Hive 教程(七)-DML基础
DML,Hive Data Manipulation Language,数据操作语言: 通俗理解就是数据库里与数据的操作,如增删改查,统计汇总等: Loading files into tables ...
- js设计模式--发布订阅模式
前言 本系列文章主要根据<JavaScript设计模式与开发实践>整理而来,其中会加入了一些自己的思考.希望对大家有所帮助. 概念 发布-订阅模式又叫观察者模式,它定义对象间的一种一对多的 ...
- java web 二维码生成
pom支持: <!-- 二维码支持包 start--> <dependency> <groupId>com.google.zxing</groupId> ...
- 设置adb shell的环境变量
1.设置adb系统变量 adb D:\androidStudio\platform-tools;D:\androidStudio\tools 2.设置path系统变量 path D:\android ...
- O017、部署DevStack
参考https://www.cnblogs.com/CloudMan6/p/5357273.html 本节按照以下步骤部署 DevStack 实验环境,包括控制节点和计算节点.详细的部署和配置可以 ...
- wpf ActualWidth为0解决方法
LocalNewsControl() { var descriptor = DependencyPropertyDescriptor.FromProperty(ActualWidthProperty, ...