poj之旅:1195 树状
来源:互联网 发布:win10摄像头拍照软件 编辑:程序博客网 时间:2024/06/05 00:12
Mobile phones
Description
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.
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
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 41 1 2 32 0 0 2 2 1 1 1 21 1 2 -12 1 1 2 3 3
Sample Output
34
这个问题通过构造一个二维的树状数组来解决:
无论是单单看x轴和y轴,都是一个一维的Binary Index Tree,只不过两个关键操作变为了:
void add(int x,int y,int num,int size){while(x<=size){int curr=y;while(curr<=size){Tree[x][curr]+=num;curr+=(-curr&curr);}x=x+(-x&x);}}int sum(int x,int y){int res=0;while(x>0){int curr=y;while(curr>0){res+=Tree[x][curr];curr-=(-curr&curr);}x=x-(-x&x);}//cout<<"res: "<<res<<endl;return res;}
0 0
- poj之旅:1195 树状
- 树状数组 之 poj 2352
- 树状数组 之 poj 2481
- 树状数组 之 poj 3067
- 树状数组 poj 1195
- poj 1195(树状数组)
- poj 1195 树状数组
- 二维树状数组学习之二:练习POJ 1195
- poj 2155 二维树状数组 poj 1195 二维树状数组
- POJ 1195 二维树状数组
- POJ 1195 二维树状数组
- POJ 1195 二维树状数组
- Poj 1195 二维树状数组
- POJ 1195 二维树状数组
- 【二维树状数组】POJ 1195
- POJ 1195 二维树状数组
- poj 1195(树状数组)
- poj 1195 二维树状数组
- ES6 -- 异步编程神器2:Generator
- .net操作SQLite数据库
- eclipse配置git
- 电商超卖
- 大数据IMF传奇行动绝密课程第120课:Spark Streaming性能优化:如何在End-to-End生产环境下安全高效地把结果数据存入HBase中
- poj之旅:1195 树状
- 离散基础 (14). 驻点极值和最值问题
- Android-okhttp下载图片并保存(刷新图库)
- 【树】洛谷 P1087 FBI树
- py编辑器激活
- 段选择器 段描述符 段描述符表 线性地址形成
- 程序生成打包
- 第五章、数字
- Oracle11gR2 RAC for Windows安装上篇:Grid Infrastructure + ASM 两节点安装 简要步骤及安装问题总结