poj 1195:Mobile phones(二维线段树,矩阵求和)
Time Limit: 5000MS | Memory Limit: 65536K | |
Total Submissions: 14391 | Accepted: 6685 |
Description
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
Source
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std; #define MAXN 1100 int tree[MAXN*][MAXN*],s; void Add_y(int d,int dy,int L,int R,int y,int a) //加数列操作
{
tree[d][dy] += a; //相关矩阵全部加a if(L==R){
return ;
} int mid = (L+R)>>;
if(mid>=y)
Add_y(d,dy<<,L,mid,y,a);
else
Add_y(d,dy<<|,mid+,R,y,a);
} void Add_x(int d,int L,int R,int x,int y,int a) //加数行操作
{
Add_y(d,,,s,y,a); //每一个行块都要更新其对应的列块,这样才能将所有与(x,y)相关的矩阵的值更新 if(L==R){
return ;
} int mid = (L+R)>>;
if(mid>=x)
Add_x(d<<,L,mid,x,y,a);
else
Add_x(d<<|,mid+,R,x,y,a);
} int Sum_y(int d,int dy,int L,int R,int b,int t)
{
if(L==b && R==t) //找到要找的矩阵,输出这个矩阵对应的值
return tree[d][dy]; //没找到
int mid = (L+R)>>;
if(mid >= t)
return Sum_y(d,dy<<,L,mid,b,t);
else if(mid < b)
return Sum_y(d,dy<<|,mid+,R,b,t);
else
return Sum_y(d,dy<<,L,mid,b,mid) + Sum_y(d,dy<<|,mid+,R,mid+,t);
} int Sum_x(int d,int L,int R,int l,int r,int b,int t)
{
if(L==l && R==r){ //找到要找的行块,继续查找列块
return Sum_y(d,,,s,b,t);
} //没找到
int mid = (L+R)>>;
if(mid >= r)
return Sum_x(d<<,L,mid,l,r,b,t);
else if(mid < l)
return Sum_x(d<<|,mid+,R,l,r,b,t);
else
return Sum_x(d<<,L,mid,l,mid,b,t) + Sum_x(d<<|,mid+,R,mid+,r,b,t);
} int main()
{
int cmd,x,y,a,l,r,b,t; while(scanf("%d",&cmd)!=EOF){
switch(cmd){
case : //初始化矩阵
scanf("%d",&s);
memset(tree,,sizeof(tree));
break; case : //加数
scanf("%d%d%d",&x,&y,&a);
Add_x(,,s,x+,y+,a);
break; case : //求矩阵和
scanf("%d%d%d%d",&l,&b,&r,&t);
printf("%d\n",Sum_x(,,s,l+,r+,b+,t+));
break; case : //退出程序
return ;
default:
break;
}
}
return ;
}
Freecode : www.cnblogs.com/yym2013
poj 1195:Mobile phones(二维线段树,矩阵求和)的更多相关文章
- poj 1195 Mobile phones(二维树状数组)
树状数组支持两种操作: Add(x, d)操作: 让a[x]增加d. Query(L,R): 计算 a[L]+a[L+1]……a[R]. 当要频繁的对数组元素进行修改,同时又要频繁的查询数组内任一 ...
- poj 2155:Matrix(二维线段树,矩阵取反,好题)
Matrix Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 17880 Accepted: 6709 Descripti ...
- POJ 2155 Matrix (二维线段树)
Matrix Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 17226 Accepted: 6461 Descripti ...
- POJ 2155 Matrix【二维线段树】
题目大意:给你一个全是0的N*N矩阵,每次有两种操作:1将矩阵中一个子矩阵置反,2.查询某个点是0还是1 思路:裸的二维线段树 #include<iostream>#include< ...
- poj 1195:Mobile phones(二维树状数组,矩阵求和)
Mobile phones Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 14489 Accepted: 6735 De ...
- POJ1195 Mobile phones 【二维线段树】
Mobile phones Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 14291 Accepted: 6644 De ...
- poj 2155 matrix 二维线段树 线段树套线段树
题意 一个$n*n$矩阵,初始全为0,每次翻转一个子矩阵,然后单点查找 题解 任意一种能维护二维平面的数据结构都可以 我这里写的是二维线段树,因为四分树的写法复杂度可能会退化,因此考虑用树套树实现二维 ...
- POJ 2155 Matrix (二维线段树入门,成段更新,单点查询 / 二维树状数组,区间更新,单点查询)
题意: 有一个n*n的矩阵,初始化全部为0.有2中操作: 1.给一个子矩阵,将这个子矩阵里面所有的0变成1,1变成0:2.询问某点的值 方法一:二维线段树 参考链接: http://blog.csdn ...
- POJ 2155 二维线段树 经典的记录所有修改再统一遍历 单点查询
本来是想找一个二维线段树涉及懒惰标记的,一看这个题,区间修改,单点查询,以为是懒惰标记,敲到一半发现这二维线段树就不适合懒惰标记,你更新了某段的某列,但其实其他段的相应列也要打标记,但因为区间不一样, ...
随机推荐
- HDU 2853 最大匹配&KM模板
http://acm.hdu.edu.cn/showproblem.php?pid=2853 这道题初看了没有思路,一直想的用网络流如何解决 参考了潘大神牌题解才懂的 最大匹配问题KM 还需要一些技巧 ...
- Vijos 1055 奶牛浴场
Description 求一个不覆盖指定点的最大子矩阵,\(n,m \leqslant 3\times 10^5,S \leqslant 5\times 10^3\) . Sol 没有名字的算法都叫x ...
- ubuntu14 谷歌输入法
sudo apt-get install ibus-googlepinyin 装完重启即可: (在右上角语言处右键,添加text entry)
- BurpSuite使用设置
一.设置字体 二.设置字符集 三.设置浏览器代理 四.BurpSuite访问步骤 五.在Target中可以查看截获的数据包
- C++ 输出调试的一些技巧
主要利用了宏和stderr... #define enable_debug #ifdef enable_debug FILL some macros/functions here #else /// ...
- IN和EXISTS的详解
从效率来看: 1) select * from T1 where exists(select 1 from T2 where T1.a=T2.a) ; T1数据量小而T2数据量非常大时,T1<& ...
- 【leetcode】Unique Binary Search Trees
Unique Binary Search Trees Given n, how many structurally unique BST's (binary search trees) that st ...
- poj 2240(floyd)
http://poj.org/problem?id=2240 题意:有些人会利用货币的不用汇率来进行套现,比如1美元换0.5英镑,而1英镑又可以换10法郎,而1法郎又可以换0.21的美元,那么经过货币 ...
- oracle11g 连接问题
一.The Network Adapter could not establish the connection 状态: 失败 -测试失败: IO 错误: The Network Adapter c ...
- ios NSURLSession completeHandler默认调用quque
注意 , [[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData *data, NSU ...