acdream1197 Points In Cuboid(hash树状数组)
题目链接:http://acdream.info/problem?pid=1197
题意:给出三维空间n个点,m个查询,每次查询某个立方体内的点的个数。
思路:按照一维排序,根据查询插入,其他两位用二位树状数组维护。由于这个坐标太大,二位数组开不出来。这时候就是hash,对于一个位置(x,y),哈希成一个数,作为下标。查询的时候不存在的数字为0.
const int mod=4000007;
const int INF=1000000005;
const int N=100005;
const int M=20011;
struct node
{
int x,y,z;
};
node a[N],b[N][2];
int n,m;
int ans[N];
pii Q[N];
int cmp(node a,node b)
{
return a.z<b.z;
}
int A[mod];
int arr[mod];
inline int ha(int x)
{
int k=x%mod;
int i;
for(i=k;;i++)
{
if(i==mod) i=0;
if(A[i])
{
if(A[i]==x) return i;
}
else
{
A[i]=x;
return i;
}
}
}
inline int find(int x)
{
int k=x%mod;
int i;
for(i=k;;i++)
{
if(i==mod) i=0;
if(A[i])
{
if(A[i]==x) return i;
}
else
{
return i;
}
}
return 0;
}
int que(int x,int y)
{
if(!x||!y) return 0;
int ans=0;
while(x)
{
int i;
for(i=y;i;i-=i&-i)
{
ans+=arr[find(x*M+i)];
}
x-=x&-x;
}
return ans;
}
void add(int x,int y)
{
while(x<M)
{
int i=y;
while(i<M) arr[ha(x*M+i)]++,i+=i&-i;
x+=x&-x;
}
}
int cal(int t)
{
int ans=0;
ans+=que(b[t][1].x,b[t][1].y);
ans-=que(b[t][1].x,b[t][0].y-1);
ans-=que(b[t][0].x-1,b[t][1].y);
ans+=que(b[t][0].x-1,b[t][0].y-1);
return ans;
}
void cal()
{
clr(A,0);
clr(arr,0);
int i;
for(i=1;i<=n;i++)
{
scanf("%d%d%d",&a[i].x,&a[i].y,&a[i].z);
a[i].x+=10005;
a[i].y+=10005;
}
scanf("%d",&m);
int j;
for(i=1;i<=m;i++)
{
for(j=0;j<2;j++)
{
scanf("%d%d%d",&b[i][j].x,&b[i][j].y,&b[i][j].z);
b[i][j].x+=10005;
b[i][j].y+=10005;
}
if(b[i][0].x>b[i][1].x) swap(b[i][0].x,b[i][1].x);
if(b[i][0].y>b[i][1].y) swap(b[i][0].y,b[i][1].y);
if(b[i][0].z>b[i][1].z) swap(b[i][0].z,b[i][1].z);
Q[i*2-1]=MP(b[i][0].z-1,-i);
Q[i*2]=MP(b[i][1].z,i);
}
sort(a+1,a+n+1,cmp);
sort(Q+1,Q+2*m+1);
j=1;
for(i=1;i<=m;i++) ans[i]=0;
for(i=1;i<=n;i++)
{
while(j<=2*m&&Q[j].first<a[i].z)
{
int x=Q[j].second;
if(x>0) ans[x]+=cal(x);
else ans[-x]-=cal(-x);
j++;
}
add(a[i].x,a[i].y);
}
while(j<=2*m)
{
int x=Q[j].second;
if(x>0) ans[x]+=cal(x);
else ans[-x]-=cal(-x);
j++;
}
for(i=1;i<=m;i++) printf("%d\n",ans[i]);
}
int main()
{
int num=0;
while(scanf("%d",&n)!=-1)
{
printf("Case #%d:\n",++num);
cal();
}
}
acdream1197 Points In Cuboid(hash树状数组)的更多相关文章
- [bzoj2124]等差子序列(hash+树状数组)
我又来更博啦 2124: 等差子序列 Time Limit: 3 Sec Memory Limit: 259 MBSubmit: 941 Solved: 348[Submit][Statu ...
- URAL-1989 Subpalindromes 多项式Hash+树状数组
题目链接:http://acm.timus.ru/problem.aspx?space=1&num=1989 题意:给出一个字符串,m个操作:1,修改其中一个字符串,2,询问 [a, b] 是 ...
- P7114 [NOIP2020] 字符串匹配 (字符串hash+树状数组)
好多题解用的扩展KMP(没学过,所以不用这种方法). 我们按照题目要求记F(s)表示s串的权值,可以预处理出前缀权值(用于A)和后缀权值(用于C),枚举AB的长度i=2~n-1,不需要分开枚举,我们只 ...
- CF452F Permutations/Luogu2757 等差子序列 树状数组、Hash
传送门--Luogu 传送门--Codeforces 如果存在长度\(>3\)的等差子序列,那么一定存在长度\(=3\)的等差子序列,所以我们只需要找长度为\(3\)的等差子序列.可以枚举等差子 ...
- bzoj 2124 等差子序列 树状数组维护hash+回文串
等差子序列 Time Limit: 3 Sec Memory Limit: 259 MBSubmit: 1919 Solved: 713[Submit][Status][Discuss] Desc ...
- 【BZOJ2124】等差子序列 树状数组维护hash值
[BZOJ2124]等差子序列 Description 给一个1到N的排列{Ai},询问是否存在1<=p1<p2<p3<p4<p5<…<pLen<=N ...
- ACdreamoj 1011(树状数组维护字符串hash前缀和)
题目链接:http://acdream.info/problem? pid=1019 题意:两种操作,第一种将字符串某个位置的字符换为还有一个字符.另外一种查询某个连续子序列是否是回文串: 解法:有两 ...
- bzoj 1669: [Usaco2006 Oct]Hungry Cows饥饿的奶牛【dp+树状数组+hash】
最长上升子序列.虽然数据可以直接n方但是另写了个nlogn的 转移:f[i]=max(f[j]+1)(a[j]<a[i]) O(n^2) #include<iostream> #in ...
- ural 1989(树状数组+多项式hash)
题意:给出一个字符串.有两种操作,一个是p a b,问字符串从位置a到位置b的子串是否是一个回文子串.还有一个操作 c a b,把字符串位置a的字符替换为b. 题解:由于字符串长度为1e5且问的次数也 ...
随机推荐
- zw版_Halcon图像库delphi接口文件
zw版_Halcon图像库delphi接口文件 Halcon图像库delphi接口文件,根据安装时用户设置的文件目录不同,会有所差异,笔者一般安装在delphi的import目录下. 参见:& ...
- 「thunar」给thunar增加搜索文件功能
1.安装catfish sudo apt-get install catfish 2.配置thunar,添加[自定义动作] 打开 Thunar 后,点击 Edit -> Configure cu ...
- ipseccmd命令解析
IPSec 首先需要指出的是,IPSec和TCP/IP筛选是不同的东西,大家不要混淆了.TCP/IP筛选的功能十分有限,远不如IPSec灵活和强大.下面就说说如何在命令行下控制IPSec. XP系统用 ...
- HGE游戏引擎之hgeQuad结构体的使用(用于渲染图片)
HGE基本的渲染图元是hgeQuad (Quad is the basic HGE graphic primitive),其中有一个hgeVertex成员结构,它用来描述图元顶点信息.The hgeV ...
- android 项目学习随笔二十一(IM、语音识别、机器人、统计、扫描二维码、条形码)
语音识别:科大讯飞语音云 http://www.xfyun.cn/ 语音机器人模拟 public class TalkBean { public String text; public boolean ...
- laravel的安装
安装composer http://docs.phpcomposer.com/download/ curl -sS https://getcomposer.org/installer | php mv ...
- Maven之Nexus构建企业级Maven仓库
什么是Nexus? Nexus是Maven仓库管理器,用来搭建一个本地仓库服务器,这样做的好处是便于管理,节省网络资源,速度快,还有一个非常有用的功能就是可以通过项目的SNAPSHOT版本管理,来进行 ...
- ImageLoader 图片加裁
// String picurl = article.cateLogo;// ImageLoader.getInstance().displayImage(picurl, holder.cate_Lo ...
- linux下echo命令详解(转)
linux的echo命令, 在shell编程中极为常用, 在终端下打印变量value的时候也是常常用到的, 因此有必要了解下echo的用法 echo命令的功能是在显示器上显示一段文字,一般起到一个 ...
- HDU 5876:Sparse Graph(BFS)
http://acm.hdu.edu.cn/showproblem.php?pid=5876 Sparse Graph Problem Description In graph theory, t ...