2019牛客暑期多校训练营(第八场)D Distance(定期重构+BFS)

链接:https://ac.nowcoder.com/acm/contest/888/D
来源:牛客网
 

题目描述

 

Gromah and LZR have entered the fourth level. There is a blank cube with size n×m×hn\times m\times hn×m×h hanging on the wall.

 

Gromah soon finds a list beside the cube, there are qq_{}q​ instructions in the list and each instruction is in one of the following two formats:

 

1. (1,x,y,z)(1, x, y, z)_{}(1,x,y,z)​, meaning to add a tag on position (x,y,z)(x, y, z)_{}(x,y,z)​ in the cube

2. (2,x,y,z)(2,x, y, z)_{}(2,x,y,z)​, meaning to determine the minimum Manhattan Distance to the given position (x,y,z)(x, y, z)_{}(x,y,z)​ among all tagged positions

 

Manhattan Distance between two positions (x1,y1,z1),(x2,y2,z2)(x_1, y_1, z_1), (x_2, y_2, z_2)(x1​,y1​,z1​),(x2​,y2​,z2​) is defined as ∣x1−x2∣+∣y1−y2∣+∣z1−z2∣|x_1 - x_2| + |y_1 - y_2| + |z_1 - z_2|∣x1​−x2​∣+∣y1​−y2​∣+∣z1​−z2​∣.

 

LZR also finds a note board saying that the password of this level is the sequence made up of all results of the instructions in format 2.

 

Please help them get all results of the instructions in format 2.

输入描述:

 

The first line contains four positive integers n,m,h,qn,m,h,q_{}n,m,h,q​, denoting the sizes in three dimensions of the cube and the number of instructions.

 

Following qq_{}q​ lines each contains four positive integers op,x,y,zop,x, y, z_{}op,x,y,z​, where op=1op=1_{}op=1​ means to add a tag on (x,y,z)(x,y,z)_{}(x,y,z)​ while op=2op=2_{}op=2​ means to make a query on (x,y,z)(x,y,z)_{}(x,y,z)​.

 

 

1≤n×m×h,q≤105,1≤x≤n,1≤y≤m,1≤z≤h1 \le n\times m\times h, q\le 10^5, 1 \le x \le n, 1 \le y \le m, 1 \le z \le h1≤n×m×h,q≤105,1≤x≤n,1≤y≤m,1≤z≤h

 

It is guaranteed that the first instruction is in format 1 and that no position will be tagged more than once.

 

输出描述:

For each instruction in format 2, output the answer in one line.

示例1

输入

复制

3 3 3 4
1 1 1 1
2 2 3 3
1 3 1 1
2 3 3 2

输出

复制

5
3

说明

 

For the first query, there is only one tagged position (1,1,1)(1,1,1)_{}(1,1,1)​ currently, so the answer is ∣1−2∣+∣1−3∣+∣1−3∣=5|1-2| + |1-3| + |1-3| = 5_{}∣1−2∣+∣1−3∣+∣1−3∣=5​.

 

For the second query, (3,1,1)(3,1,1)_{}(3,1,1)​ is the nearest tagged position, so the answer is ∣3−3∣+∣1−3∣+∣1−2∣=3|3-3| + |1-3| + |1-2| = 3_{}∣3−3∣+∣1−3∣+∣1−2∣=3​.

 

         dis[i]数组表示已经处理过后某些点,到达目标i的最短距离是多少,这样每更新一次dis期望复杂的即使n*m*h(三维空间大小)。

         再设置一个vector,用来储存自从上一个BFS更新dis数组以来没还有经过处理的点的集合。这样对于查询新的目标点v的答案就会在dis[id(v)],以及暴力遍历这个vector所有点和v的距离后产生。

         我们设一个数E,假设vectot每存到E个点的时候,把这些点进行一次bfs更新dis数组,我们就可以得到期望复杂度

         q( n*m*h/E + E)

         可以明显得到E在根下n*m*h的时候期望复杂度最小,且满足题目要求。这样的话就能解出了。

#include<bits/stdc++.h>
using namespace std;
int dis[100005];
int n, m, h, q;
const int E = 330;
struct node { int x, y, z; };
int id(int x, int y, int z) {
	return (x - 1)*m*h + (y - 1)*h + z;
}
int id(node k) {
	return id(k.x, k.y, k.z);
}
bool check(int x, int y, int z) {
	return x >= 1 && x <= n && y >= 1 && y <= m && z >= 1 && z <= h;
}
int fx[6][3] = { 1,0,0,-1,0,0,0,1,0,0,-1,0,0,0,1,0,0,-1 };
vector<node>wait;
void bfs() {
	queue<node>q;
	for (auto v : wait) {
		q.push(v);
		dis[id(v)] = 0;
	}
	wait.clear();
	while (!q.empty()) {
		node u = q.front(); q.pop();
		for (int i = 0; i < 6; i++) {
			int x = u.x + fx[i][0];
			int y = u.y + fx[i][1];
			int z = u.z + fx[i][2];
			if (check(x, y, z) && dis[id(x, y, z)] > dis[id(u)] + 1) {
				dis[id(x, y, z)] = dis[id(u)] + 1;
				q.push(node{ x,y,z });
			}
		}
	}
}
int main() {
	ios::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	memset(dis, 0x3f3f3f3f, sizeof dis);
	cin >> n >> m >> h >> q;
	while (q--) {
		int op, x, y, z;
		cin >> op >> x >> y >> z;
		if (op == 1) {
			wait.push_back(node{ x,y,z });
			if (wait.size() == E) {
				bfs();
			}
		}
		else {
			int res = dis[id(x, y, z)];
			for (auto v : wait) {
				res = min(res, abs(v.x - x) + abs(v.y - y) + abs(v.z - z));
			}
			cout << res << "\n";
		}
	}
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值