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 ...
随机推荐
- jQuery中的append()和prepend(),after()和before()的差别
jQuery中的append()和preappend(),after()和before()的差别 append()和prepend() 如果 <div class='a'> //<- ...
- ubuntu下运行第一个.net core web程序
前置条件 ubuntu系统 且已经安装dotnetcore运行环境 mkdir testMVC 创建一个文件夹 cd testMVC 进入文件夹 dotnet new -t web 创建程序( ...
- C# 取字符串中间文本 取字符串左边 取字符串右边
好像是第二种效率高一点,取str字符串中123左边的所有字符:第一种Between(str,"","123"),而第二种是Between(str,null,&q ...
- 文件操作FileStream,Log
1.关于读写文件,犯的一个低级错误,平常代码拷贝习惯了,就像电脑用多了会提笔忘字一样,所以平常还是要多多用心才好. 这段代码的意图是在文件中写入数据,如果原文件不存在,则先新建. 事实上,当真的执行了 ...
- post请求方式的翻页爬取内容及思考
1 #coding=utf-8 import urllib2 import urllib import json output = open('huizho.json', 'w') for page ...
- 现在的C语言编辑器里的int范围为什么是-2147483648~2147483647 2014-08-05 10:21 100人阅读 评论(0) 收藏
下面是引用百度文库的一段话: "这得从二进制的原码说起: 如果以最高位为符号位,二进制原码最大为0111111111111111=215-1=32767 最小为111111111111111 ...
- 213. String Compression【easy】
Implement a method to perform basic string compression using the counts of repeated characters. For ...
- iOS开发25个性能调优技巧
1. 用ARC管理内存 ARC(Automatic Reference Counting, 自动引用计数)和iOS5一起发布,它避免了最常见的也就是经常是由于我们忘记释放内存所造成的内存泄露.它自动为 ...
- Ubuntu 12.04 server 如何安装 OpenERP 7(转)
不经意的一次看到OpenERP这个开源ERP,就被其丰富的功能,简洁的画面,熟悉的语言所吸引.迫不及待的多方查询资料,自己架设一个测试环境来进行了解.以下为测试安装时候的步骤说明,以备查询,并供有需要 ...
- 打包iOS应用程序
如果想要将做的iOS应用程序装到自己的iOS设备上测试.或者装在别人的iOS设备上,或者想发布到App Store里,先要给应用签名.签名就要有证书,这就需要申请证书的过程了. 把自己的应用程序装到设 ...