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 二维线段树 经典的记录所有修改再统一遍历 单点查询
本来是想找一个二维线段树涉及懒惰标记的,一看这个题,区间修改,单点查询,以为是懒惰标记,敲到一半发现这二维线段树就不适合懒惰标记,你更新了某段的某列,但其实其他段的相应列也要打标记,但因为区间不一样, ...
随机推荐
- BZOJ 2822: [AHOI2012]树屋阶梯
Description 求拼成阶梯状的方案数. Sol 高精度+Catalan数. 我们可以把最后一行无线延伸,所有就很容易看出Catalan数了. \(f_n=f_0f_{n-1}+f_1f_{n- ...
- BZOJ 4423: [AMPPZ2013]Bytehattan
Sol 对偶图+并查集. 思路非常好,将网格图转化成对偶图,在原图中删掉一条边,相当于在对偶图中连上一条边(其实就是网格的格点相互连边),每次加边用并查集维护就可以了. 哦对,还要注意边界就是网格外面 ...
- JustMock Lite (Free Mocking Framework For .net)
通过 Nuget 安装 2. 官网下载(官网不行点这里) 3. 帮助文档 商业版和免费版区别概览 MockingContainer 测试类准备:一般来说也是业务类 public class C ...
- MySQL ODBC for Linux
参考自http://blog.csdn.net/allens_zhou/article/details/8575400 centos7 64bit [IP:192.168.0.100] yum ins ...
- irssi忽略退出,加入消息
IRSSI: IGNORE JOINS, PARTS, QUITS AND NICKS MESSAGES I use IRC on a daily basis and my client of cho ...
- Effective C++ -----条款34:区分接口继承和实现继承
接口继承和实现继承不同.在public继承之下,derived classes总是继承base class的接口. pure virtual函数只具体指定接口继承. 简朴的(非纯)impure vir ...
- Js 旋转木马 轮播
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- DB2环境设置
作者:gnuhpc 出处:http://www.cnblogs.com/gnuhpc/ 1.级别对应 • Environment variables at the operating system l ...
- (2016弱校联盟十一专场10.2) A.Nearest Neighbor Search
题目链接 水题,算一下就行. #include <bits/stdc++.h> using namespace std; typedef long long ll; ll x[],y[], ...
- POJ 3597 Polygon Division (DP)
题目链接 题意:把一个正多边形分成数个三角形或者四边形,问有多少种方案. 题解: 如果分出的全为三角形的话,那就是正多边形三角剖分问题.它的结果就是Catalan数.现在也可以划分出四边形的话,可以采 ...