hdu4456 Crowd(二维树状数组)
题意:给出一个n*n的矩阵,然后m个operation,1表示坐标(x,y)的值加z,2表示与坐标(x,y)的曼哈顿距离不超过z的点的权值和。
解题思路:将矩阵側过来45度。发现询问的时候,有效的点构成的事实上是一个矩阵。
然后就变成了单点改动。求矩阵和的问题。
我们考虑裸二维树状数组的做法。会发现矩阵太大,可是注意到,初始的时候,矩阵里面全部的值都为0,那么这个二维树状数组中。有效的点就是改动的那些点,以及掌控这些点的区间。这里总的状态数仅仅有m*logn*logn。所以我们把operation都拿出来,然后将有改动的状态映射到序列中就可以。
代码:
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<vector>
#define lowbit(x) (x&(-x))
using namespace std ;
struct Operation {
int op , x , y , z ;
} p[88888] ;
int base = 100000 ;
const int N = 2222222 ;
vector<int> mp ;
int val[N] , tot , n , m ;
const int maxn = 22222 ; inline int get ( int x , int y ) {
x = x * base + y ;
y = lower_bound(mp.begin(),mp.end(),x) - mp.begin() ;
if ( y == tot || mp[y] != x ) return -1 ;
return y ;
} void update ( int x , int y , int z ) {
while ( x < n*2+100 ) {
int pos = y ;
while ( pos < n*2+100 ) {
int now = get (x,pos) ;
val[now] += z ;
pos += lowbit ( pos ) ;
}
x += lowbit ( x ) ;
}
} int sum ( int x , int y ) {
int ret = 0 ;
while ( x ) {
int pos = y ;
while ( pos ) {
int now = get ( x , pos ) ;
if (now != -1) ret += val[now] ;
pos -= lowbit ( pos ) ;
}
x -= lowbit ( x ) ;
}
return ret ;
} int query ( int x1 , int y1 , int x2 , int y2 ) {
int ret1 = sum(x1-1,y1-1) , ret2 = sum(x2,y2) , ret3 = sum(x1-1,y2) , ret4 = sum(x2,y1-1) ;
// printf ( "ret2 = %d\n" , ret2 ) ;
return ret1+ret2-ret3-ret4 ;
} int get_num () {
int n = 0 , flag = 1 ;
char c ;
while ( (c = getchar ()) && c != '-' && (c<'0'||c>'9') ) ;
if ( c == '-' ) flag = -1 ;
else n = c - '0' ;
while ( (c = getchar ()) && c >= '0' && c <= '9' ) n = n * 10 + c - '0' ;
return n * flag ;
} void add ( int t , int x , int y ) {
while ( x < n*2+100 ) {
int pos = y ;
while ( pos < n*2+100 ) {
// if ( tot == N - 1 ) while (1) ;
mp.push_back ( x * base + pos ) ;
pos += lowbit ( pos ) ;
}
x += lowbit ( x ) ;
}
} void read ( int i ) {
p[i].op = get_num () ;
p[i].x = get_num () ;
p[i].y = get_num () ;
p[i].z = get_num () ;
int x = p[i].x + p[i].y - 1 , y = n - p[i].x + p[i].y ;
if ( p[i].op == 1 ) add ( 1 , x , y ) ;
} int main () {
while ( scanf ( "%d" , &n ) && n ) {
scanf ( "%d" , &m ) ;
mp.clear () ;
memset ( val , 0 , sizeof ( val ) ) ;
for ( int i = 1 ; i <= m ; i ++ )
read ( i ) ;
sort ( mp.begin() , mp.end() ) ;
vector<int>::iterator it = unique(mp.begin() , mp.end()) ;
mp.erase(it,mp.end()) ;
// tot = unique ( mp + 1 , mp + tot + 1 ) - mp - 1 ;
tot = mp.size () ;
for ( int i = 1 ; i <= m ; i ++ ) {
int op , x , y , z ;
op = p[i].op ;
x = p[i].x ;
y = p[i].y ;
z = p[i].z ;
// printf ( "op = %d , x = %d , y = %d , z = %d\n" , op , x , y , p[i].z ) ;
int xx = x + y - 1 , yy = n - x + y ;
if ( op == 1 ) {
update ( xx , yy , z ) ;
} else {
int x1 = std::max ( 1 , xx - z ) ;
int y1 = std::max ( 1 , yy - z ) ;
int x2 = std::min ( 2*n-1 , xx + z ) ;
int y2 = std::min ( 2*n-1 , yy + z ) ;
printf ( "%d\n" , query ( x1 , y1 , x2 , y2 ) ) ;
}
}
}
return 0 ;
}
hdu4456 Crowd(二维树状数组)的更多相关文章
- 【 HDU - 4456 】Crowd (二维树状数组、cdq分治)
BUPT2017 wintertraining(15) #5A HDU 4456 题意 给你一个n行n列的格子,一开始每个格子值都是0.有M个操作,p=1为第一种操作,给格子(x,y)增加z.p=2为 ...
- 二维树状数组 BZOJ 1452 [JSOI2009]Count
题目链接 裸二维树状数组 #include <bits/stdc++.h> const int N = 305; struct BIT_2D { int c[105][N][N], n, ...
- HDU1559 最大子矩阵 (二维树状数组)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1559 最大子矩阵 Time Limit: 30000/10000 MS (Java/Others) ...
- POJMatrix(二维树状数组)
Matrix Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 22058 Accepted: 8219 Descripti ...
- poj 1195:Mobile phones(二维树状数组,矩阵求和)
Mobile phones Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 14489 Accepted: 6735 De ...
- Codeforces Round #198 (Div. 1) D. Iahub and Xors 二维树状数组*
D. Iahub and Xors Iahub does not like background stories, so he'll tell you exactly what this prob ...
- POJ 2155 Matrix(二维树状数组+区间更新单点求和)
题意:给你一个n*n的全0矩阵,每次有两个操作: C x1 y1 x2 y2:将(x1,y1)到(x2,y2)的矩阵全部值求反 Q x y:求出(x,y)位置的值 树状数组标准是求单点更新区间求和,但 ...
- [poj2155]Matrix(二维树状数组)
Matrix Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 25004 Accepted: 9261 Descripti ...
- POJ 2155 Matrix (二维树状数组)
Matrix Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 17224 Accepted: 6460 Descripti ...
随机推荐
- struts action不在是一个单例类
在servlet中,servlet类是一个单例,在servlet中的成员变量,将会被所有请求共享,同时也有可能存在线程安全问题,如有一个成员变量num,每次方法后市的num自增 package act ...
- 对正在运行的mysql进行监控
对正在运行的mysql进行监控,其中一个方式就是查看mysql运行状态. (1)QPS(每秒Query量) QPS = Questions(or Queries) / seconds mysql &g ...
- 基于Qt的A*算法可视化分析
代码地址如下:http://www.demodashi.com/demo/13677.html 需求 之前做过一个无人车需要自主寻找最佳路径,所以研究了相关的寻路算法,最终选择A算法,因为其简单易懂, ...
- 每秒处理3百万请求的Web集群搭建-如何生成每秒百万级别的 HTTP 请求?
本文是构建能够每秒处理 3 百万请求的高性能 Web 集群系列文章的第一篇.它记录了我使用负载生成器工具的一些经历,希望它能帮助每一个像我一样不得不使用这些工具的人节省时间. 负载生成器是一些生成用于 ...
- Python 代码块左移或右移
(就 IDE 是 PyCharm 来说) 选中代码块: 1)右移:直接 Tab 2)左移:Shift + Tab Python 对代码对齐要求很严格的. Python的对齐方式很重要,对齐方式决定了 ...
- 编译ORBSLAM2 build_ros.sh,实现kinect2在ROS环境下运行ORBSLAM2
//編譯ORBSLAM2 build_ros.sh參考:“http://www.cnblogs.com/bigzhao/p/6635770.html”1.source ~/.bashrc出現問題:ct ...
- 异常:Caused by: java.lang.NoSuchMethodError: javax.persistence.OneToMany.orphanRemoval()Z/Caused by: java.lang.NoSuchMethodError: javax.persistence.JoinColumn.foreign
Spring3.0 + Hibernate3.5:启动服务器报:Caused by: java.lang.NoSuchMethodError: javax.persistence.OneToMany. ...
- Azure 服务监控API调用
概述 在使用Azure的过程中,很多用户希望通过code的方式获取服务在管理门户中显示的监视信息,如虚拟机的CPU.服务总线的总消息出入数等.目前Azure的大部分服务都已经支持通过监控器的API查询 ...
- 弹出键盘windowsoftinputmode属性设置值
windowSoftInputMode属性设置值 2012-08-30 16:49 1592人阅读 评论(0) 收藏 举报 androidattributes活动 (1).AndroidManifes ...
- Mysql主从(主从不同步解决办法,常见问题及解决办法,在线对mysql做主从复制)
一.主从不同步解决办法 先上Master库: mysql>show processlist; 查看下进程是否Sleep太多.发现很正常. show master status; 也正常. mys ...