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. SkylineGlobe 移动端开发测试

    基于SkylineGlobe提供的安卓版本SDK,在已有菜单中增加自定义内容,测试代码如下: 新增加文件ZhaoHeContainer.java package com.skyline.terraex ...

  2. 格式化angularjs日期'/Date(-62135596800000)/'

    在实现在angularjs时,发现经序列化后的日期需要格式化显示. 翻看以前的博客,似乎有写过一篇有关js方面的解决办法<格式化json日期'/Date(-62135596800000)/'&g ...

  3. RabbmitMQ-组成及简单使用

    什么是MQ? MQ全程Message Queue,消息队列(MQ)是一种应用程序对应用程序的通信方法.MQ是消费者-生产者模型的典型代表.一端往消息队列中不断写消息而另一端则可以读取队列中的消息. R ...

  4. C#_面试

    class Program { static void Main(string[] args) { , , , , }; var arry = ConvertSum(arr); , , , , , } ...

  5. 11.18 Daily Scrum

    这两天开发人员陆续提交了自己开发的部分. 目前所有开发任务都已经完成,剩下的只是测试和整合,做最后的冲刺. 明天的任务: 李承晗:测试与整合

  6. #个人博客作业week2——关于代码规范的个人观点

    对于这一讨论的前提我们首先要知道什么是代码规范. 在这个问题上我同意一篇参考文章的观点——代码规范不仅只编码风格.编码风格仅是代码规范的一个方面,除了编码风格,代码规范还包括函数返回值等其他方面.在我 ...

  7. .NET组件介绍系列

    一款开源免费的.NET文档操作组件DocX(.NET组件介绍之一)http://www.cnblogs.com/pengze0902/p/6122311.html 高效而稳定的企业级.NET Offi ...

  8. Github链接及git学习心得总结

    众所周知GitHub已经是当下非常流行的代码托管库了,全世界有无数的程序员把他们的代码放在GitHub里.那比起云盘之类的工具,用GitHub有什么好处呢:1. 以后在帖子里只需要扔一个链接,大家就能 ...

  9. BugPhobia进阶篇章:前端技术/设计文档

    0x01 :前端概述 0x0100 :前端基本描述 前端基础框架 Semantic UI 根据http://semantic-ui.com/提供的样例和文档,依据Version 2.1.4版本的特性进 ...

  10. boost::asio之(一)简单客户端服务器回显功能

    客户端: // BoostDev.cpp: 定义控制台应用程序的入口点. // #include "stdafx.h" #include <iostream> #inc ...