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. Android Studio 导入工程

    最简单的方式 等待加载完就好了 第二种方式 在导入别人的android studio项目(假设为项目A)时,会遇到gradle不一致的情况,以下简短介绍解决方法: 1. 打开要导入的项目的目录,删除下 ...

  2. 微软官方的Excel android 移动版的折腾

    微软官方的Excel android 移动版,有重大bug.害我折腾了一天多时间.最终确认是Excel自身的问题. 现象描述:手机上新建或是保存excel后.放到电脑上,不能打开.提示”Excel在B ...

  3. VB6 加密解密字符串

    Public Function EnCodeStr(ByVal password As String) As String Dim il_bit, il_x, il_y, il_z, il_len, ...

  4. .NET的弹性及瞬间错误处理库Polly

    原文:.NET的弹性及瞬间错误处理库Polly 本文基本是官方说明的翻译和总结(https://github.com/App-vNext/Polly) 什么是Polly? Polly是一款基于.NET ...

  5. LiveCharts文档-3开始-2基础

    原文:LiveCharts文档-3开始-2基础 LiveCharts文档-3开始-2基础 基本使用 LiveCharts设计的很容易使用,所有的东西都可以自动的实现更新和动画,库会在它觉得有必要更新的 ...

  6. 【JVM.5】类文件结构

    鲁迅曾经说过:代码编译的结构从本地机器码转变为字节码,是存储格式发展的一小步,确是编程语言发展的一大步. 一.无关性的基石 Java设计者在最初就承诺过“In the future, we will ...

  7. MVC使用Redis实现分布式锁

    使用场景 在做Web项目的时候,有很多特殊的场景要使用到锁 比如说抢红包,资源分配,订单支付等场景 就拿抢红包来说,如果一个红包有5份,同时100个人抢如果没有用到锁的话 100个人同时并发都抢成功, ...

  8. Html5前端笔记

    获取Dpi 在 window.load中添加: (function(){ if (!window.screen.deviceXDPI){ var tmpNode = document.createEl ...

  9. 浅谈meta viewport设置移动端自适应

    1.viewport 移动设备上的viewport是设备屏幕上用来显示网页的那部分区域,再具体一点就是浏览器上用来显示网页的那部分区域,但viewport又不局限于浏览器可视区域的大小,它可能比浏览器 ...

  10. HTTP请求头和响应头部包括的信息有哪些?

    每个HTTP请求和响应都会带有相应的头部信息.默认情况下,在发送XHR请求的同时,还会发送下列头部信息: Accept:浏览器能够处理的内容类型 Accept-Charset:浏览器能够显示的字符集 ...