题目链接

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. Java NIO学习系列六:Java中的IO模型

    前文中我们总结了linux系统中的5中IO模型,并且着重介绍了其中的4种IO模型: 阻塞I/O(blocking IO) 非阻塞I/O(nonblocking IO) I/O多路复用(IO multi ...

  2. 提升10倍生产力:IDEA远程一键部署SpringBoot到Docker

    作者:陶章好 juejin.im/post/5d026212f265da1b8608828b 推荐阅读(点击即可跳转阅读) 1. SpringBoot内容聚合 2. 面试题内容聚合 3. 设计模式内容 ...

  3. Linux平台 Oracle 19c RAC安装Part1:准备工作

    一.实施前期准备工作 1.1 服务器安装操作系统 1.2 Oracle安装介质 1.3 共享存储规划 1.4 网络规范分配 二.安装前期准备工作 2.1 各节点系统时间校对 2.2 各节点关闭防火墙和 ...

  4. 理解SVG中的 viewport,viewBox, preserveAspectRatio

    _ 阅读目录 一:理解viewport 二:理解viewBox 三:理解 preserveAspectRatio 回到顶部 一:理解viewport 该属性表示的是SVG可见区域的大小.或者也可以叫画 ...

  5. Java统计代码行数

    package test; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; im ...

  6. 01-Spring Security框架学习

    目录 01-Spring Security框架学习 简介 Spring Security 是什么 Spring Security 解决那些问题 Spring Security 的优点 历史背景 Spr ...

  7. Power Designer导出模型的sql加注释-Oracle语句

    第一步:Database-->Edit Current DBMS 第二步: 然后分别将 Script-->Objects-->Table-->TableComment Scri ...

  8. nodeJs跨域设置(express,koa2,eggJs)

    原生跨域 var http=require('http'); var server = http.createServer(function (req,res) { res.setHeader('Ac ...

  9. 使用pandoc简单教程

    使用pandoc作为过滤器 {#step-4-using-pandoc-as-a-filter} 类型 pandoc 并按Enter键.你应该看到光标就在那里,等着你输入一些东西.输入: Hello ...

  10. Go最火的Gin框架简单入门

    Gin 介绍 Gin 是一个 Golang 写的 web 框架,具有高性能的优点,,基于 httprouter,它提供了类似martini但更好性能(路由性能约快40倍)的API服务.官方地址:htt ...