Suppose that the fourth generation mobile phone base stations in the Tampere area operate as follows. The area is divided into squares. The squares form an S * S matrix with the rows and columns numbered from 0 to S-1. Each square contains a base station. The number of active mobile phones inside a square can change because a phone is moved from a square to another or a phone is switched on or off. At times, each base station reports the change in the number of active phones to the main base station along with the row and the column of the matrix.

Write a program, which receives these reports and answers queries about the current total number of active mobile phones in any rectangle-shaped area.

Input

The input is read from standard input as integers and the answers to the queries are written to standard output as integers. The input is encoded as follows. Each input comes on a separate line, and consists of one instruction integer and a number of parameter integers according to the following table. 

The values will always be in range, so there is no need to check them. In particular, if A is negative, it can be assumed that it will not reduce the square value below zero. The indexing starts at 0, e.g. for a table of size 4 * 4, we have 0 <= X <= 3 and 0 <= Y <= 3.

Table size: 1 * 1 <= S * S <= 1024 * 1024 
Cell value V at any time: 0 <= V <= 32767 
Update amount: -32768 <= A <= 32767 
No of instructions in input: 3 <= U <= 60002 
Maximum number of phones in the whole table: M= 2^30 

Output

Your program should not answer anything to lines with an instruction other than 2. If the instruction is 2, then your program is expected to answer the query by writing the answer as a single line containing a single integer to standard output.

Sample Input

0 4
1 1 2 3
2 0 0 2 2
1 1 1 2
1 1 2 -1
2 1 1 2 3
3

Sample Output

3
4
解题思路:
二维树状数组,直接用一维板子改成二维就行了,要注意的是询问区间的时候两个区间的差值并不能直接减,而且还要考虑边界的问题。
所以最后两个区间的差值由 (x2,y2)-(x1-1,y2)-(x2,y1-1)+(x1-1,x2-1);
数组是从0开始的,坐标必须加一
实现代码:
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define lowbit(x) ((x)&(-(x)))
const int N = ;
int m;
int c[N][N];
inline void update(int x,int y,int date){
for(int i=x;i<=m;i+=lowbit(i))
for(int j=y;j<=m;j+=lowbit(j))
c[i][j] += date;
} inline int sum(int x,int y){
int ans = ;
for(int i=x;i>;i-=lowbit(i))
for(int j=y;j>;j-=lowbit(j))
ans += c[i][j];
return ans;
}
int main()
{
int n,k,x,y,d,x1,x2,y1,y2;
while(scanf("%d%d",&n,&m)!=EOF){
memset(c,,sizeof(c));
while(scanf("%d",&k)){
if(k==){
scanf("%d%d%d",&x,&y,&d);
x++;y++;
update(x,y,d);
//cout<<sum(x,y)<<endl;
}
else if(k==){
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
x1++;y1++;x2++;y2++;
//cout<<sum(x2,y2)<<endl;
printf("%d\n",sum(x2,y2)-sum(x1-,y2)-sum(x2,y1-)+sum(x1-,y1-));
}
else
break;
}
}
return ;
}

poj1195的更多相关文章

  1. 二维树状数组poj1195

    题目链接:https://vjudge.net/problem/POJ-1195 题意:一开始输入0和一个s,0代表开始,s代表这是一个s*s的图,接下来会输入1或2,1代表进行单点修改,后面会接3个 ...

  2. poj1195(二维树状数组)

    题目链接:https://vjudge.net/problem/POJ-1195 题意:有s*s的矩阵,初始化为全0,有两种操作,单点修改(x,y)的值,区间查询(x,y)的值(l<=x< ...

  3. POJ-1195 Mobile phones---裸的二维树状数组(注意下标从1,1开始)

    题目链接: https://vjudge.net/problem/POJ-1195 题目大意: 直接维护二维树状数组 注意横纵坐标全部需要加1,因为树状数组从(1,1)开始 #include<c ...

  4. 二维树状数组(水题) POJ1195

    前段时间遇到线段树过不了,树状数组却过了的题.(其实线段树过得了的) 回忆了下树状数组. 主要原理,还是二进制位数,每一项的和表示其为它的前((最后一位1及其后)的二进制数)和,可从二进制图来看.(用 ...

  5. 【POJ1195】【二维树状数组】Mobile phones

    Description Suppose that the fourth generation mobile phone base stations in the Tampere area operat ...

  6. poj1195二维树状数组模板

    二维树状数组和一维的也差不多,改一下add和query函数即可:即按行修改,行内单点修改即可 /* 二维树状数组,询问一个二维区间内的数之和 */ #include<iostream> # ...

  7. poj-1195(二维树状数组)

    题目链接:传送门 题意:给出操作,按照操作进行. 思路:将树状数组设置为二维的就行了. 注意: (1)每次求出的面积是S(x2,y2)-S(x1-1,y2)-S(x2,y1-1)+S(x1-1,y1- ...

  8. 【poj1195】Mobile phones(二维树状数组)

    题目链接:http://poj.org/problem?id=1195 [题意] 给出一个全0的矩阵,然后一些操作 0 S:初始化矩阵,维数是S*S,值全为0,这个操作只有最开始出现一次 1 X Y ...

  9. POJ1195 Mobile phones 【二维线段树】

    Mobile phones Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 14291   Accepted: 6644 De ...

随机推荐

  1. Excel 2007 底层实现方式

    一.EXCEL的底层实现 能力有限,了解的比较浅,有不足之处望指正,首先看下图: 一. excel2007是使用xml格式来存储的,把一个excel文件后缀改为.zip,打开之后就直接可以看到一个ex ...

  2. (原创)odoo关系字段在视图中的行为控制 总结

    字段类型 选项或属性 格式示例 描述 many2one , many2many_tags(widget) no_create options='{"no_create":True} ...

  3. (一)在 Blend 中绘制形状和路径

    原文:(一)在 Blend 中绘制形状和路径 https://docs.microsoft.com/zh-cn/previous-versions/jj170881(v=vs.120) 在 Blend ...

  4. Nginx Windows版的服务安装和管理工具

    以前研究过负载均衡,最近正在项目上实施(从来没做过小项目以上级别的东西,哈),nginx挺好,不过Windows有点为难,小流量和本地不追求性能,简单易用是目标. Nginx Windows上并没有提 ...

  5. HTTP协议基础与web服务的重定向,跳转以及请求转发

    JavaWeb中,HttpServletRequest与HttpServletResponse几乎是处理各种请求与操作必备的参数,与原始的ServletRequest/ServletResponse相 ...

  6. use_frameworks!和#use_frameworks!的区别、解决Swift项目中use_frameworks!冲突的问题

    use_frameworks!和#use_frameworks!的区别 转自:https://www.jianshu.com/p/0ae58a477459 1. 用cocoapods 导入swift ...

  7. omnigraffle 的一些总结

    http://jingyan.baidu.com/article/fcb5aff7a16337edab4a714d.html Omnigraffle绘制连接线时从任意点开始 点击直线工具后,在右侧设置 ...

  8. HashMap和HashTable区别【转载】

    今天看到的HashMap和HashTable区别介绍,收藏留着学习. 出处:http://www.importnew.com/24822.html 代码版本 JDK每一版本都在改进.本文讨论的Hash ...

  9. 战神答题APP 无敌结束版

    APP发布了哦~~     多多捧场~ http://anzhuoyuan.com/app/info/appid/242381.html 还有github https://github.com/784 ...

  10. Flask-论坛开发-2-Jinja2模板

    对Flask感兴趣的,可以看下这个视频教程:http://study.163.com/course/courseLearn.htm?courseId=1004091002 1. Jinja2 模板介绍 ...