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 x1xx2, y1yy2.

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 ≤ x1x2 < N, 0 ≤ y1y2 < N

Output

For each Sum operation output a non-negative number denoting the sum modulo 109+7.

Sample Input

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

Sample Output

1
2
3
题目大意:给你一个N*N二维矩阵,初始值时都为0,有两种操作
1:单点修改值
2:给出一个子矩阵的左上和右下角的坐标,询问一个子矩形范围内的值的和.
二维树状数组模板题,单点更新区间查询
#include<queue>
#include<set>
#include<cstdio>
#include <iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
#define max_v 1005
#define mod 1000000007
typedef long long LL;
int c[max_v][max_v];
int lowbit(int x)
{
return x&(-x);
}
LL getsum(int x,int y)
{
LL sum=;
for(int i=x;i!=;i-=lowbit(i))
{
for(int j=y;j!=;j-=lowbit(j))
{
sum=(sum+c[i][j]);
}
}
return sum;
}
void update(int x,int y,int v)
{
for(int i=x;i<max_v;i+=lowbit(i))
{
for(int j=y;j<max_v;j+=lowbit(j))
{
c[i][j]=(c[i][j]+v);
}
}
}
int main()
{
char str[];
int n,m;
while(~scanf("%d %d",&n,&m))
{
memset(c,,sizeof(c));
int a,b,w;
while(m--)
{
scanf("%s",str);
if(str[]=='A')
{
scanf("%d %d %d",&a,&b,&w);
a++;
b++;
update(a,b,w);
}else
{
int sx,sy,ex,ey;
scanf("%d %d %d %d",&sx,&sy,&ex,&ey);
sx++;
sy++;
ex++;
ey++;
printf("%lld\n",(getsum(ex,ey)-getsum(sx-,ey)-getsum(ex,sy-)+getsum(sx-,sy-)+mod)%mod);
}
}
}
return ;
}
/*
题目大意:给你一个N*N二维矩阵,初始值时都为0,有两种操作
1:单点修改值
2:给出一个子矩阵的左上和右下角的坐标,询问一个子矩形范围内的值的和. 二维树状数组模板题,单点更新区间查询 */
 
												

HihoCoder - 1336 二维数状数组(单点更新 区间查询)的更多相关文章

  1. hdu 2642二维树状数组 单点更新区间查询 模板题

    二维树状数组 单点更新区间查询 模板 从零开始借鉴http://www.2cto.com/kf/201307/227488.html #include<stdio.h> #include& ...

  2. TZOJ 2725 See you~(二维树状数组单点更新区间查询)

    描述 Now I am leaving hust acm. In the past two and half years, I learned so many knowledge about Algo ...

  3. hdu 2642 二维树状数组 单点更新区间查询 模板水题

    Stars Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/65536 K (Java/Others) Total Subm ...

  4. hdu2642二维树状数组单点更新+区间查询

    http://acm.hdu.edu.cn/showproblem.php?pid=2642 题目大意:一个星空,二维的.上面有1000*1000的格点,每个格点上有星星在闪烁.一开始时星星全部暗淡着 ...

  5. 【2018年全国多校算法寒假训练营练习比赛(第五场)-E】情人节的电灯泡(二维树状数组单点更新+区间查询)

    试题链接:https://www.nowcoder.com/acm/contest/77/E 题目描述 情人节到了,小芳和小明手牵手,打算过一个完美的情人节,但是小刚偏偏也来了,当了一个明晃晃的电灯泡 ...

  6. SPOJ - MATSUM 二维树状数组单点更新

    忘记了单点更新时要在树状数组中减去原值..wa了一发 /* 矩形求和,单点更改 */ #include<iostream> #include<cstring> #include ...

  7. Matrix Sum HihoCoder - 1336 二维树状数组 感觉好像二维差分。

    #include<cstdio> #include<cstring> using namespace std; typedef long long ll; ; ; ll c[N ...

  8. hdu2642二维树状数组单点更新

    碰到这种题一定要注意坐标是不是有序的,也要注意坐标是不是有0的,有的话需要+1处理 #include<bits/stdc++.h> using namespace std; #define ...

  9. 牛客网 暑期ACM多校训练营(第二场)J.farm-STL(vector)+二维树状数组区间更新、单点查询 or 大暴力?

    开心.jpg J.farm 先解释一下题意,题意就是一个n*m的矩形区域,每个点代表一个植物,然后不同的植物对应不同的适合的肥料k,如果植物被撒上不适合的肥料就会死掉.然后题目将每个点适合的肥料种类( ...

随机推荐

  1. PHPCMS V9标签循环嵌套调用数据的方法

    PHPCMS V9的标签制作以灵活见长,可以自由DIY出个性的数据调用,对于制作有风格有创意的网站模板很好用,今天就介绍一个标签循环嵌套方法,可以实现对PC标签循环调用,代码如下: 在此文件里/php ...

  2. Django中间件解析

    一,中间件的概念 django 中的中间件(middleware),在django中,中间件其实就是一个类,在请求到来和结束后,django会根据自己的规则在合适的时机执行中间件中相应的方法.在dja ...

  3. 网络I/O模型--02阻塞模式(多线程)

    当服务器收到客户端 X 的请求后(读取到所有请求数据后),将这个请求送入一个独立线程进行处理,然后主线程继续接收客户端 Y 的请求. 客户端一侧也可以使用一个子线程和服务器端进行通信.这样客户端主线程 ...

  4. OpenGL学习--08--基本渲染(灯光)

    1.tutorial08.cpp // Include standard headers #include <stdio.h> #include <stdlib.h> #inc ...

  5. 3The superclass “javax.servlet.http.HttpServlet" was not found on the Java Build Path 之一

    另外一篇短文里还有第三种解决方案,查看请点击这里 1.异常信息 创建maven web项目时,出现 The superclass “javax.servlet.http.HttpServlet&quo ...

  6. JS + jQuery 实现元素自动滚动到底部,兼容IE、FF、Chrome

    HTML代码: <ul class="tasklog-dialog-ul" id="auto_to_bottom"> <li>删除虚拟机 ...

  7. font-family:中文字体的英文名称 (宋体 微软雅黑)

    宋体 SimSun 黑体 SimHei 微软雅黑 Microsoft YaHei 微软正黑体 Microsoft JhengHei 新宋体 NSimSun 新细明体 PMingLiU 细明体 Ming ...

  8. Oracle EBS 清理归档

    oraprod 登陆数据库服务器 执行 rman target / 如图: 执行: delete noprompt force archivelog all completed before ‘sys ...

  9. MySql EF6 DBFirst 向导无法生成 edmx 解决方法(同:您的项目引用了最新实体框架;但是,找不到数据链接所需的与版本兼容的实体框架数据库提供程序)

    使用 MySql EF6 DBfirst 生成模型时经常会遇到EF6模式无法选择的情况究其原因, 还是因为没有正确的使用 Connector/Net. 下面说一下使用方法. 使用 MySql DBFi ...

  10. 12 Things Developers Will Love About Oracle Database 12c Release 2

    by Chris Saxon-Oracle It's Here: Oracle Database 12c Release 2 (12.2) Is available on Oracle Cloud. ...