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 ...
随机推荐
- <译>Flink官方文档-Flink概述
Overview This documentation is for Apache Flink version 1.0-SNAPSHOT, which is the current developme ...
- MySQL外键的设置及作用
原文地址:http://www.php100.com/html/webkaifa/database/Mysql/2010/0830/5342.html 外键的作用: 保持数据一致性,完整性,主要目的是 ...
- .NET Remoting与Socket、Webservice和WCF的比较及优势 (转)
1:Socket VS Remoting 使用socket无疑是效率最高的.但是,在复杂的接口环境下,Socket的开发效率也是最低的.故在兼顾开发效率的情况下,可以使用Remoting来代替Sock ...
- 学会Git玩转Github笔记(二)——Git使用
一.Git基本工作流程 Git工作区域 向仓库中添加文件流程 二. Git初始化及仓库创建和操作 基本信息设置 . 设置用户名 git config --global user.name 'itcas ...
- maven打包时加入依赖包及加入本地依赖包
maven打包的时候默认是不加入依赖的jar包的,所以想打出一个独立的可运行jar包的话直接mvn clean install package是不行的.需要略改动下pom文件,加入如下plugin & ...
- FTP软件发送"AUTH TLS"提示 无法连接到服务器
响应: 220-FileZilla Server version 0.9.24 beta 响应: 220-written by Tim Kosse (Tim.Kosse@gmx.de) 响应: 220 ...
- AsyncTask的原理和缺点
番外tips: 特别喜欢一句话.假设你想了解一个人.那你从他出生開始了解吧.相同的道理,想要了解AsyncTask,就从它的源代码開始吧. 进入主题前,在说一下,开发中已经非常少使用AsyncTask ...
- win 7 下vim的使用
1.gVim74.exe ftp://ftp.vim.org/pub/vim/pc/gvim74.exe 2.vimcdoc-1.9.0-setup.exe 中文说明文档 http://211.147 ...
- JDK1.9-新特性
1. Java平台级模块系统 该特性使Java9最大的一个特性,Java提供该功能的主要的动机在于,减少内存的开销,JVM启动的时候,至少会有30~60MB的内存加载,主要原因是JVM需要加载rt.j ...
- 并发insert情况下会发生重复的数据插入问题
1.背景 用多线程接收推送的订单数据,把接收的订单数据存到一个表中,实现的需求是:如果接收的订单消息在数据库中已经存在,那么执行update操作:如果没有存在,那么执行insert操作代码逻辑: if ...