题目链接

Distance

题意

1<=nmh,q<=1e5

q个操作

  • 1 x y z往坐标里加入一个点
  • 2 x y z查询距离该点最近的点的距离(曼哈顿距离)

题解

做法一

将要插入的点保存在一个队列中,当队列中的点数达到一个阈值就把队列中所有点取出,暴力的bfs一次把答案记录在\(dis[getid(x,y,z)]\)中表示距离点\((x,y,z)\)最近的点的距离,查询的时候就取暴力的计算查询点和队列中每个点的距离,再和已经插入的点也就是dis数组取最小值,当阈值取\(\sqrt{nmh}\)时复杂度为\(O(\frac{qnmh}E + qE) = O(nmh + q\sqrt{nmh})\)

做法二

将距离公式\(|x_0-x_i| + |y_0-y_i| + |z_0-z_i|\)的绝对值拆开有八种情况\(\pm(x_0-x_i) \pm (y_0-y_i) \pm (z_0-z_i)\)

这八种情况的最大值就是真正的距离,我们将插入的点分成\((\pm x, \pm y, \pm z)\)八种情况分别插入八个树状数组,树状数组维护\(x_i <= x, y_i <= y, z_i <= z的x+y+z\)的最大值,目的是为了把求最近点的距离转换成求\((x+y+z-x_i-y_i-z_i)\)的最小值,查询的时候对八个树状数组答案取min就行了

代码

做法一

#include <bits/stdc++.h>
using namespace std;
typedef long long ll; const int mx = 1e5+5;
const int mod = 998244353;
const int INF = 0x3f3f3f3f;
const int dir[6][3] = {{0,0,1}, {0,0,-1}, {0,1,0}, {0,-1,0}, {1,0,0}, {-1,0,0}};
int dis[mx];
int n, m, h, q;
struct node {
int x, y, z, step;
}; vector <node> v;
queue <node> Q; int getid(int x, int y, int z) {
return (x-1)*m*h + (y-1)*h + z;
} int getdis(int x, int y, int z, int a, int b, int c) {
return abs(x-a) + abs(y-b) + abs(z-c);
} int main() {
for (int i = 0; i < mx; i++) dis[i] = INF; scanf("%d%d%d%d", &n, &m, &h, &q);
int sq = sqrt(n*m*h) ;
while (q--) {
int op, x, y, z;
scanf("%d%d%d%d", &op, &x, &y, &z);
if (op == 1) {
v.push_back({x, y, z, 0});
if (v.size() == sq) {
for (int i = 0; i < v.size(); i++) {
Q.push(v[i]);
dis[getid(v[i].x, v[i].y, v[i].z)] = 0;
}
v.clear();
while (!Q.empty()) {
node now = Q.front();
node next;
Q.pop();
for (int i = 0; i < 6; i++) {
next.x = now.x + dir[i][0];
next.y = now.y + dir[i][1];
next.z = now.z + dir[i][2];
if (next.x < 1 || next.x > n || next.y < 1 || next.y > m || next.z < 1 || next.z > h) continue;
next.step = now.step + 1;
if (next.step < dis[getid(next.x, next.y, next.z)]) {
dis[getid(next.x, next.y, next.z)] = next.step;
Q.push(next);
}
}
}
}
} else {
int ans = dis[getid(x, y, z)];
for (int i = 0; i < v.size(); i++) {
ans = min(ans, getdis(x, y, z, v[i].x, v[i].y, v[i].z));
}
printf("%d\n", ans);
}
}
return 0;
}

做法二

#include <bits/stdc++.h>
using namespace std;
typedef long long ll; const int mx = 8e5+5;
const int mod = 998244353;
const int INF = 0x3f3f3f3f;
const int dir[8][3] = {{1,1,1}, {1,1,-1}, {1,-1,1}, {1,-1,-1}, {-1,1,1}, {-1,1,-1}, {-1,-1,1}, {-1,-1,-1}};
int dis[mx];
int n, m, h, q; int getid(int x, int y, int z) {
return (x-1)*m*h + (y-1)*h + z;
} struct Bit {
int a[mx];
int lowbit(int x) {
return x & -x;
}
void update(int x, int y, int z) {
if (x < 0) x += n;
if (y < 0) y += m;
if (z < 0) z += h;
for (int i = x; i <= n; i+=lowbit(i))
for (int j = y; j <= m; j+=lowbit(j))
for (int k = z; k <= h; k+=lowbit(k))
a[getid(i,j,k)] = max(a[getid(i,j,k)], x+y+z);
} int query(int x, int y, int z) {
if (x < 0) x += n;
if (y < 0) y += m;
if (z < 0) z += h;
int ans = 0;
for (int i = x; i > 0; i-=lowbit(i))
for (int j = y; j > 0; j-=lowbit(j))
for (int k = z; k > 0; k-=lowbit(k))
ans = max(a[getid(i,j,k)], ans);
if (ans == 0) return INF;
else return x+y+z-ans;
}
}bit[8]; int main() {
scanf("%d%d%d%d", &n, &m, &h, &q);
n++; m++; h++;
while (q--) {
int op, x, y, z;
scanf("%d%d%d%d", &op, &x, &y, &z);
if (op == 1) {
for (int i = 0; i < 8; i++) {
bit[i].update(x*dir[i][0], y*dir[i][1], z*dir[i][2]);
}
} else {
int ans = INF;
for (int i = 0; i < 8; i++) ans = min(ans, bit[i].query(x*dir[i][0], y*dir[i][1], z*dir[i][2]));
printf("%d\n", ans);
}
}
return 0;
}

D-Distance_2019牛客暑期多校训练营(第八场)的更多相关文章

  1. 2019牛客暑期多校训练营(第九场)A:Power of Fibonacci(斐波拉契幂次和)

    题意:求Σfi^m%p. zoj上p是1e9+7,牛客是1e9:  对于这两个,分别有不同的做法. 前者利用公式,公式里面有sqrt(5),我们只需要二次剩余求即可.     后者mod=1e9,5才 ...

  2. 2019牛客暑期多校训练营(第一场)A题【单调栈】(补题)

    链接:https://ac.nowcoder.com/acm/contest/881/A来源:牛客网 题目描述 Two arrays u and v each with m distinct elem ...

  3. 2019牛客暑期多校训练营(第一场) B Integration (数学)

    链接:https://ac.nowcoder.com/acm/contest/881/B 来源:牛客网 Integration 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/C++ 5242 ...

  4. 2019牛客暑期多校训练营(第一场) A Equivalent Prefixes ( st 表 + 二分+分治)

    链接:https://ac.nowcoder.com/acm/contest/881/A 来源:牛客网 Equivalent Prefixes 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/ ...

  5. 2019牛客暑期多校训练营(第二场)F.Partition problem

    链接:https://ac.nowcoder.com/acm/contest/882/F来源:牛客网 Given 2N people, you need to assign each of them ...

  6. 2019牛客暑期多校训练营(第一场)A Equivalent Prefixes(单调栈/二分+分治)

    链接:https://ac.nowcoder.com/acm/contest/881/A来源:牛客网 Two arrays u and v each with m distinct elements ...

  7. [状态压缩,折半搜索] 2019牛客暑期多校训练营(第九场)Knapsack Cryptosystem

    链接:https://ac.nowcoder.com/acm/contest/889/D来源:牛客网 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/C++ 262144K,其他语言52428 ...

  8. 2019牛客暑期多校训练营(第二场)J-Subarray(思维)

    >传送门< 前言 这题我前前后后看了三遍,每次都是把网上相关的博客和通过代码认真看了再思考,然并卵,最后终于第三遍也就是现在终于看懂了,其实懂了之后发现其实没有那么难,但是的的确确需要思维 ...

  9. J-Subarray_2019牛客暑期多校训练营(第二场)

    题意 有一个只由1,-1组成的数组,给出所有连续的1所在位置,求满足1的个数大于-1的个数的子区间的数量 题解 参考博客:https://www.cnblogs.com/Yinku/p/1122149 ...

  10. 2019牛客暑期多校训练营(第一场)-A (单调栈)

    题目链接:https://ac.nowcoder.com/acm/contest/881/A 题意:给定两个长度均为n的数组a和b,求最大的p使得(a1,ap)和(b1,bp)等价,等价的定义为其任意 ...

随机推荐

  1. Windows 使用 helm3 和 kubectl

    简介: 主要原因是,我不会 vim ,在 linux 上修改 charts 的很蹩脚,所以就想着能不能再 windows 上执行 helm 命令,将 charts install linux 上搭建的 ...

  2. C语言编程入门之--第三章编写第一个C语言程序

    第三章 编写第一个C语言程序 导读:一般学一门计算机语言的第一堂上机课(“上机”顾名思义,上了计算机),就是往屏幕输出“hello world”,本章也不例外. 1.1 Hello,World! 这一 ...

  3. chapter01作业

    1. 请用命令查出ifconfig命令程序的绝对路径 [root@localhost chen]# which ifconfig /usr/sbin/ifconfig 2.请用命令展示以下命令哪些是内 ...

  4. C# 委托(delegate)、泛型委托和Lambda表达式

    目录 # 什么是委托 # 委托声明.实例化和调用 1.声明 2.委托的实例化 3.委托实例的调用 4.委托完整的简单示例 #泛型委托 1.Func委托 2.Action委托 3.Predicate委托 ...

  5. request获取url链接和参数

            //Returns the part of this request's URL from the protocol name up to the query string in th ...

  6. 【算法】【查找】二分法 Bisection

    #include<stdio.h> int main(){ ,,,,,,,,,,,,,,}; ; //长度 ; //要查找到的值 int Bisection(int x,int* a,in ...

  7. alluxio源码解析-层次化存储(4)

    层次化存储-特性介绍: https://www.alluxio.org/docs/1.6/cn/Tiered-Storage-on-Alluxio.html 引入分层存储后,Alluxio管理的数据块 ...

  8. Fabric项目学习总结

    1.Hyperledger Fabric的基本架构 2.PKI机制

  9. 监控JVM

    WAS配置visualVM 在was控制台:找到应用程序服务器--java和进程管理--进程定义--JAVA虚拟机/通用JVM 参数 ,对应英文Application servers > ser ...

  10. php Basic HTTP与Digest HTTP 应用

    Basic HTTP 认证范例 <?php //Basic HTTP 认证 if (!isset($_SERVER['PHP_AUTH_USER'])) { header('WWW-Authen ...