poj1195
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 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
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的更多相关文章
- 二维树状数组poj1195
题目链接:https://vjudge.net/problem/POJ-1195 题意:一开始输入0和一个s,0代表开始,s代表这是一个s*s的图,接下来会输入1或2,1代表进行单点修改,后面会接3个 ...
- poj1195(二维树状数组)
题目链接:https://vjudge.net/problem/POJ-1195 题意:有s*s的矩阵,初始化为全0,有两种操作,单点修改(x,y)的值,区间查询(x,y)的值(l<=x< ...
- POJ-1195 Mobile phones---裸的二维树状数组(注意下标从1,1开始)
题目链接: https://vjudge.net/problem/POJ-1195 题目大意: 直接维护二维树状数组 注意横纵坐标全部需要加1,因为树状数组从(1,1)开始 #include<c ...
- 二维树状数组(水题) POJ1195
前段时间遇到线段树过不了,树状数组却过了的题.(其实线段树过得了的) 回忆了下树状数组. 主要原理,还是二进制位数,每一项的和表示其为它的前((最后一位1及其后)的二进制数)和,可从二进制图来看.(用 ...
- 【POJ1195】【二维树状数组】Mobile phones
Description Suppose that the fourth generation mobile phone base stations in the Tampere area operat ...
- poj1195二维树状数组模板
二维树状数组和一维的也差不多,改一下add和query函数即可:即按行修改,行内单点修改即可 /* 二维树状数组,询问一个二维区间内的数之和 */ #include<iostream> # ...
- poj-1195(二维树状数组)
题目链接:传送门 题意:给出操作,按照操作进行. 思路:将树状数组设置为二维的就行了. 注意: (1)每次求出的面积是S(x2,y2)-S(x1-1,y2)-S(x2,y1-1)+S(x1-1,y1- ...
- 【poj1195】Mobile phones(二维树状数组)
题目链接:http://poj.org/problem?id=1195 [题意] 给出一个全0的矩阵,然后一些操作 0 S:初始化矩阵,维数是S*S,值全为0,这个操作只有最开始出现一次 1 X Y ...
- POJ1195 Mobile phones 【二维线段树】
Mobile phones Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 14291 Accepted: 6644 De ...
随机推荐
- Android Studio 导入工程
最简单的方式 等待加载完就好了 第二种方式 在导入别人的android studio项目(假设为项目A)时,会遇到gradle不一致的情况,以下简短介绍解决方法: 1. 打开要导入的项目的目录,删除下 ...
- 微软官方的Excel android 移动版的折腾
微软官方的Excel android 移动版,有重大bug.害我折腾了一天多时间.最终确认是Excel自身的问题. 现象描述:手机上新建或是保存excel后.放到电脑上,不能打开.提示”Excel在B ...
- VB6 加密解密字符串
Public Function EnCodeStr(ByVal password As String) As String Dim il_bit, il_x, il_y, il_z, il_len, ...
- .NET的弹性及瞬间错误处理库Polly
原文:.NET的弹性及瞬间错误处理库Polly 本文基本是官方说明的翻译和总结(https://github.com/App-vNext/Polly) 什么是Polly? Polly是一款基于.NET ...
- LiveCharts文档-3开始-2基础
原文:LiveCharts文档-3开始-2基础 LiveCharts文档-3开始-2基础 基本使用 LiveCharts设计的很容易使用,所有的东西都可以自动的实现更新和动画,库会在它觉得有必要更新的 ...
- 【JVM.5】类文件结构
鲁迅曾经说过:代码编译的结构从本地机器码转变为字节码,是存储格式发展的一小步,确是编程语言发展的一大步. 一.无关性的基石 Java设计者在最初就承诺过“In the future, we will ...
- MVC使用Redis实现分布式锁
使用场景 在做Web项目的时候,有很多特殊的场景要使用到锁 比如说抢红包,资源分配,订单支付等场景 就拿抢红包来说,如果一个红包有5份,同时100个人抢如果没有用到锁的话 100个人同时并发都抢成功, ...
- Html5前端笔记
获取Dpi 在 window.load中添加: (function(){ if (!window.screen.deviceXDPI){ var tmpNode = document.createEl ...
- 浅谈meta viewport设置移动端自适应
1.viewport 移动设备上的viewport是设备屏幕上用来显示网页的那部分区域,再具体一点就是浏览器上用来显示网页的那部分区域,但viewport又不局限于浏览器可视区域的大小,它可能比浏览器 ...
- HTTP请求头和响应头部包括的信息有哪些?
每个HTTP请求和响应都会带有相应的头部信息.默认情况下,在发送XHR请求的同时,还会发送下列头部信息: Accept:浏览器能够处理的内容类型 Accept-Charset:浏览器能够显示的字符集 ...