http://acm.hdu.edu.cn/showproblem.php?pid=4456

第一道二维树状数组就这么麻烦,题目要计算的是一个菱形范围内的和,于是可以把原来的坐标系旋转45度,就是求一个正方形范围内的和,这里还涉及到坐标系平移和放大,由于题目数据较大,用了离散化来保存需要处理的点,放在h数组内,对应的二维树状数组存在tree数组内。

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std; inline int lowbit(int x)
{
return x & (-x);
}
int p[],posx[],posy[],v[],h[],tree[],n,m,N,cnt; void update(int x,int y,int z)
{
for(int i = x;i <= N;i += lowbit(i))
{
for(int j = y;j <= N;j += lowbit(j))
{
int pos = lower_bound(h,h+cnt,i*N+j)-h;
tree[pos] += z;
}
}
} int getsum(int x,int y)
{
int ans = ;
for(int i = x;i > ;i -= lowbit(i))
{
for(int j = y;j > ;j -= lowbit(j))
{
int pos = lower_bound(h,h+cnt,i*N+j)-h;
if(h[pos] == i*N+j) ans += tree[pos];
}
}
return ans;
} int main()
{
while(scanf("%d",&n) && n)
{
memset(tree,,sizeof(tree));
int x,y;
N = *n;
cnt = ;
scanf("%d",&m);
for(int i = ;i <= m;i++)
{
scanf("%d%d%d%d",&p[i],&x,&y,&v[i]);
int xx = x-y+n,yy = x+y;
posx[i] = xx;
posy[i] = yy;
if(p[i] == )
{
for(int j = xx;j <= N;j += lowbit(j))
{
for(int k = yy;k <= N;k += lowbit(k))
{
h[cnt++] = j*N+k;
}
}
}
}
sort(h,h+cnt);
for(int i = ;i <= m;i++)
{
if(p[i] == ) update(posx[i],posy[i],v[i]);
else
{
int x1 = max(,posx[i]-v[i]),y1 = max(,posy[i]-v[i]);
int x2 = min(N,posx[i]+v[i]),y2 = min(N,posy[i]+v[i]);
printf("%d\n",getsum(x2,y2)-getsum(x2,y1-)-getsum(x1-,y2)+getsum(x1-,y1-));
}
}
}
return ;
}

HDU_4456_二维树状数组的更多相关文章

  1. 二维树状数组 BZOJ 1452 [JSOI2009]Count

    题目链接 裸二维树状数组 #include <bits/stdc++.h> const int N = 305; struct BIT_2D { int c[105][N][N], n, ...

  2. HDU1559 最大子矩阵 (二维树状数组)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1559 最大子矩阵 Time Limit: 30000/10000 MS (Java/Others)  ...

  3. POJMatrix(二维树状数组)

    Matrix Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 22058   Accepted: 8219 Descripti ...

  4. poj 1195:Mobile phones(二维树状数组,矩阵求和)

    Mobile phones Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 14489   Accepted: 6735 De ...

  5. 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 ...

  6. POJ 2155 Matrix(二维树状数组+区间更新单点求和)

    题意:给你一个n*n的全0矩阵,每次有两个操作: C x1 y1 x2 y2:将(x1,y1)到(x2,y2)的矩阵全部值求反 Q x y:求出(x,y)位置的值 树状数组标准是求单点更新区间求和,但 ...

  7. [poj2155]Matrix(二维树状数组)

    Matrix Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 25004   Accepted: 9261 Descripti ...

  8. POJ 2155 Matrix (二维树状数组)

    Matrix Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 17224   Accepted: 6460 Descripti ...

  9. [POJ2155]Matrix(二维树状数组)

    题目:http://poj.org/problem?id=2155 中文题意: 给你一个初始全部为0的n*n矩阵,有如下操作 1.C x1 y1 x2 y2 把矩形(x1,y1,x2,y2)上的数全部 ...

随机推荐

  1. mysql-5.7.9-winx64遇坑记

    昨天在mysql5.0上导入sql文件时,一直卡在一个地方报错,也没仔细分析,认为应该是mysql版本太低不支持这个语法而已.遂决心下载一个最新版本的mysql,却浑然不知前面无数的坑已经埋伏好了在等 ...

  2. 让vue-router渲染为指定的标签

    <router-link :to="{name:'cart'}" tag="li"> cart </router-link> 在rout ...

  3. mongodb学习(三)——函数使用的小技巧

    $group 下 $sum 函数 Returns a sum of numerical values. Ignores non-numeric values 只能对数字求和,非数字没有作用 查询一段时 ...

  4. dp - 循环数组的最大和

    首尾相连数组的最大子数组和 时间限制:1000 ms  |  内存限制:65535 KB 难度:4 描述 给定一个由N个整数元素组成的数组arr,数组中有正数也有负数,这个数组不是一般的数组,其首尾是 ...

  5. JMeter——聚合报告

    AggregateReport 是 JMeter 常用的一个 Listener,中文被翻译为“聚合报告”. ​ 对于每个请求,它统计响应信息并提供请求数,平均值,最大,最小值,错误率,大约吞吐量(以请 ...

  6. 头条一面竟然问我Maven?

    maven package和maven install 有什么区别? 你常用的maven命令有哪些? <dependencyManagement> 是干什么的? 还有用过其它构建工具吗? ...

  7. TypeScript 源码详细解读(4)语法1-语法树

    在上一节介绍了标记的解析,就相当于识别了一句话里有哪些词语,接下来就是把这些词语组成完整的句子,即拼装标记为语法树. 树(tree) 树是计算机数据结构里的专业术语.就像一个学校有很多年级,每个年级下 ...

  8. 深入Nodejs模块fs - 文件系统操作

    node 的fs文档密密麻麻的 api 非常多,毕竟全面支持对文件系统的操作.文档组织的很好,操作基本分为文件操作.目录操作.文件信息.流这个大方面,编程方式也支持同步.异步和 Promise. 本文 ...

  9. selenium-第一个自动化脚本

    经过上一篇的环境搭建,这一篇我们开始编写第一个自动化脚本. 一个简单的测试百度的demo #coding=utf-8 from selenium import webdriver driver = w ...

  10. ThreeJS 物理材质shader源码分析(像素着色器)

    再此之前推荐一款GLTF物理材质在线编辑器https://tinygltf.xyz/ 像素着色器(meshphysical_frag.glsl) #define PHYSICAL uniform ve ...