D-Distance_2019牛客暑期多校训练营(第八场)
题目链接
题意
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牛客暑期多校训练营(第八场)的更多相关文章
- 2019牛客暑期多校训练营(第九场)A:Power of Fibonacci(斐波拉契幂次和)
题意:求Σfi^m%p. zoj上p是1e9+7,牛客是1e9: 对于这两个,分别有不同的做法. 前者利用公式,公式里面有sqrt(5),我们只需要二次剩余求即可. 后者mod=1e9,5才 ...
- 2019牛客暑期多校训练营(第一场)A题【单调栈】(补题)
链接:https://ac.nowcoder.com/acm/contest/881/A来源:牛客网 题目描述 Two arrays u and v each with m distinct elem ...
- 2019牛客暑期多校训练营(第一场) B Integration (数学)
链接:https://ac.nowcoder.com/acm/contest/881/B 来源:牛客网 Integration 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/C++ 5242 ...
- 2019牛客暑期多校训练营(第一场) A Equivalent Prefixes ( st 表 + 二分+分治)
链接:https://ac.nowcoder.com/acm/contest/881/A 来源:牛客网 Equivalent Prefixes 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/ ...
- 2019牛客暑期多校训练营(第二场)F.Partition problem
链接:https://ac.nowcoder.com/acm/contest/882/F来源:牛客网 Given 2N people, you need to assign each of them ...
- 2019牛客暑期多校训练营(第一场)A Equivalent Prefixes(单调栈/二分+分治)
链接:https://ac.nowcoder.com/acm/contest/881/A来源:牛客网 Two arrays u and v each with m distinct elements ...
- [状态压缩,折半搜索] 2019牛客暑期多校训练营(第九场)Knapsack Cryptosystem
链接:https://ac.nowcoder.com/acm/contest/889/D来源:牛客网 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/C++ 262144K,其他语言52428 ...
- 2019牛客暑期多校训练营(第二场)J-Subarray(思维)
>传送门< 前言 这题我前前后后看了三遍,每次都是把网上相关的博客和通过代码认真看了再思考,然并卵,最后终于第三遍也就是现在终于看懂了,其实懂了之后发现其实没有那么难,但是的的确确需要思维 ...
- J-Subarray_2019牛客暑期多校训练营(第二场)
题意 有一个只由1,-1组成的数组,给出所有连续的1所在位置,求满足1的个数大于-1的个数的子区间的数量 题解 参考博客:https://www.cnblogs.com/Yinku/p/1122149 ...
- 2019牛客暑期多校训练营(第一场)-A (单调栈)
题目链接:https://ac.nowcoder.com/acm/contest/881/A 题意:给定两个长度均为n的数组a和b,求最大的p使得(a1,ap)和(b1,bp)等价,等价的定义为其任意 ...
随机推荐
- Django REST framework的使用简单介绍
官方文档:https://www.django-rest-framework.org/ GitHub源码:https://github.com/encode/django-rest-framework ...
- ArrayList源码分析--jdk1.8
ArrayList概述 1. ArrayList是可以动态扩容和动态删除冗余容量的索引序列,基于数组实现的集合. 2. ArrayList支持随机访问.克隆.序列化,元素有序且可以重复. 3. ...
- C语言数组排序——冒泡排序、选择排序、插入排序
一.冒泡排序 原理解析:(以从小到大排序为例)在一排数字中,将第一个与第二个比较大小,如果后面的数比前面的小,则交换他们的位置. 然后比较第二.第三个……直到比较第n-1个和第n个,此时,每一次比较都 ...
- UVA11388 GCD LCM
(链接点这儿) 题目: The GCD of two positive integers is the largest integer that divides both the integers w ...
- 聊聊目标管理之 OKR
这篇文章我们不谈技术,聊点轻松的,那聊什么呢?聊一下最近很火的目标管理 OKR.不知道小伙伴你们的公司什么情况,我的公司今年开始推行 OKR,用了大半年的时间,感觉效果还不错,上周六又参加了一天的复盘 ...
- 带你剖析WebGis的世界奥秘----点和线的世界
前言 昨天写了好久的博文我没保存,今天在来想继续写居然没了,气死人啊这种情况你们见到过没,所以今天重新写,我还是切换到了HTML格式的书写上.废话不多说了,我们现在就进入主题,上周我仔细研究了WebG ...
- SQL获取客户端网卡电脑名称等信息
Select SYSTEM_USER 当前用户名, USER_NAME() 当前所有者,db_Name() 当前数据库,@@SPID 当前进程号,(select top 1 FileName from ...
- Java源码之ConcurrentHashMap
⑴背景 ConcurrentHashMap是线程安全高效的HashMap.而HashMap在多线程情况下强行使用HashMap的put方法可能会导致程序死循环,使CPU使用率达到100%.(http: ...
- Jvm内存泄漏
内存泄漏和内存溢出的关系 内存泄露:指程序中动态分配内存给一些临时对象,但是对象不会被GC所回收,它始终占用内存.即被分配的对象可达但已无用. 内存溢出:指程序运行过程中无法申请到足够的内存而导致的一 ...
- Flutter学习笔记(20)--FloatingActionButton、PopupMenuButton、SimpleDialog、AlertDialog、SnackBar
如需转载,请注明出处:Flutter学习笔记(20)--FloatingActionButton.PopupMenuButton.SimpleDialog.AlertDialog.SnackBar F ...