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 二维线段树 经典的记录所有修改再统一遍历 单点查询
本来是想找一个二维线段树涉及懒惰标记的,一看这个题,区间修改,单点查询,以为是懒惰标记,敲到一半发现这二维线段树就不适合懒惰标记,你更新了某段的某列,但其实其他段的相应列也要打标记,但因为区间不一样, ...
随机推荐
- 2.实现Express中间件
Express提供的大部分功能都是通过中间件函数完成,这些中间件函数在Node.js收到 请求的时点 和 发送响应的时点 执行 connect模块提供了中间件框剪 方便在全局或路径级别或为单个路由插入 ...
- zend framework2 入门实例代码album模型
下载album模型 一.目录结构说明 - zf_project - config - autoload global.php -- 数据库在这里配置 local.php ...
- linux 编程环境搭建过程记录
1, 安装centos 7 最小版 过程略 ...... 2, 安装桌面安装yum groupinstall "GNOME Desktop" 更新系统运行级别ln -sf /li ...
- JavaWeb出现404一个很隐蔽的原因
- idea 排除编译文件,恢复编译
- sizeof和strlen的区别
一.sizeof sizeof(...)是运算符,而不是一个函数. sizeof操作符的结果类型是size_t,在头文件中typedef为unsigned int,其值在编译时即计算好了, ...
- 页面加载时执行JQ代码
$(function () { //jq加载时执行的这里面是 $("#ss").append("<strong>这是新加的</strong>&qu ...
- C#之设置无边框后如何移动窗体(转)
转载:http://www.cnblogs.com/techmango/archive/2012/03/31/2427523.html 第一种,利用windows的消息机制来实现: 首先﹐.定义鼠標左 ...
- mysql查询当前正在使用数据库
1.select database(): 2.status: 3.show tables:
- socket端口重复占用问题
1.一个服务端进程在主动释放端口后(调用close)端口状态为TIME_WAIT,这时再去监听同样的端口,不论是否设置SO_REUSEADDR,都能监听成功,也能接收到客户端的连接,但是无法收到数据. ...