69252023-12-19 22:46:31111Pac-Mancpp17Accepted 100/1001.769s199324 KiB
#include <bits/stdc++.h>
using namespace std;

#define int long long
#define double long double

#define pii pair<int, int>

void bad() {
	cout << "NO" << '\n';
	exit(0);
}

signed main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
#ifdef CB
	freopen("in.txt", "r", stdin);
//	freopen("out.txt", "w", stdout);
#endif
	int N;
	cin >> N;
	vector<int> X(N), Y(N), Z(N);
	for (int i = 0; i < N; i++) {
		cin >> X[i];
	}
	for (int i = 0; i < N; i++) {
		cin >> Y[i];
	}
	for (int i = 0; i < N; i++) {
		cin >> Z[i];
	}
	map<tuple<int, int, int>, int> s;
	for (int i = 0; i < N; i++) {
		s[{X[i], Y[i], Z[i]}] = i;
	}
	map<pii, vector<int>> xy, yz, zx;
	for (int i = 0; i < N; i++) {
		xy[{X[i], Y[i]}].push_back(Z[i]);
		yz[{Y[i], Z[i]}].push_back(X[i]);
		zx[{Z[i], X[i]}].push_back(Y[i]);
	}
	for (auto& [p, v] : xy) {
		sort(v.begin(), v.end());
	}
	for (auto& [p, v] : yz) {
		sort(v.begin(), v.end());
	}
	for (auto& [p, v] : zx) {
		sort(v.begin(), v.end());
	}
	// same line
	for (int i = 0; i < N; i++) {
		vector<pair<vector<int>&, int>> a = {
			{xy[{X[i], Y[i]}], Z[i]},
			{yz[{Y[i], Z[i]}], X[i]},
			{zx[{Z[i], X[i]}], Y[i]},
		};
		for (auto [v, x] : a) {
			auto t = lower_bound(v.begin(), v.end(), x);
			if (t != v.begin()) {
				if (*prev(t) != *t - 1) {
					bad();
				}
			}
			if (next(t) != v.end()) {
				if (*next(t) != *t + 1) {
					bad();
				}
			}
		}
	}
	// same plane
	map<int, map<int, pii>> xp, yp, zp;
	for (int i = 0; i < N; i++) {
		xp[X[i]][Y[i]];
		yp[Y[i]][Z[i]];
		zp[Z[i]][X[i]];
	}
	for (auto& [x, m] : xp) {
		pii a = {INT_MAX, INT_MIN};
		for (auto t = m.begin(); t != m.end(); t++) {
			t->second = a;
			a.first = min(a.first, xy[{x, t->first}].front());
			a.second = max(a.second, xy[{x, t->first}].back());
		}
	}
	for (auto& [y, m] : yp) {
		pii a = {INT_MAX, INT_MIN};
		for (auto t = m.begin(); t != m.end(); t++) {
			t->second = a;
			a.first = min(a.first, yz[{y, t->first}].front());
			a.second = max(a.second, yz[{y, t->first}].back());
		}
	}
	for (auto& [z, m] : zp) {
		pii a = {INT_MAX, INT_MIN};
		for (auto t = m.begin(); t != m.end(); t++) {
			t->second = a;
			a.first = min(a.first, zx[{z, t->first}].front());
			a.second = max(a.second, zx[{z, t->first}].back());
		}
	}
	map<int, map<int, pii>> xs, ys, zs;
	for (int i = 0; i < N; i++) {
		xs[X[i]][Y[i]];
		ys[Y[i]][Z[i]];
		zs[Z[i]][X[i]];
	}
	for (auto& [x, m] : xs) {
		pii a = {INT_MAX, INT_MIN};
		for (auto t = m.rbegin(); t != m.rend(); t++) {
			t->second = a;
			a.first = min(a.first, xy[{x, t->first}].front());
			a.second = max(a.second, xy[{x, t->first}].back());
		}
	}
	for (auto& [y, m] : ys) {
		pii a = {INT_MAX, INT_MIN};
		for (auto t = m.rbegin(); t != m.rend(); t++) {
			t->second = a;
			a.first = min(a.first, yz[{y, t->first}].front());
			a.second = max(a.second, yz[{y, t->first}].back());
		}
	}
	for (auto& [z, m] : zs) {
		pii a = {INT_MAX, INT_MIN};
		for (auto t = m.rbegin(); t != m.rend(); t++) {
			t->second = a;
			a.first = min(a.first, zx[{z, t->first}].front());
			a.second = max(a.second, zx[{z, t->first}].back());
		}
	}
	for (int i = 0; i < N; i++) {
		if (xp[X[i]][Y[i]].first < Z[i]) {
			if (!s.count({X[i], Y[i] - 1, Z[i]}) && !s.count({X[i], Y[i], Z[i] - 1})) {
				bad();
			}
		}
		if (xp[X[i]][Y[i]].second > Z[i]) {
			if (!s.count({X[i], Y[i] - 1, Z[i]}) && !s.count({X[i], Y[i], Z[i] + 1})) {
				bad();
			}
		}
		if (xs[X[i]][Y[i]].first < Z[i]) {
			if (!s.count({X[i], Y[i] + 1, Z[i]}) && !s.count({X[i], Y[i], Z[i] - 1})) {
				bad();
			}
		}
		if (xs[X[i]][Y[i]].second > Z[i]) {
			if (!s.count({X[i], Y[i] + 1, Z[i]}) && !s.count({X[i], Y[i], Z[i] + 1})) {
				bad();
			}
		}
		if (yp[Y[i]][Z[i]].first < X[i]) {
			if (!s.count({X[i] - 1, Y[i], Z[i]}) && !s.count({X[i], Y[i], Z[i] - 1})) {
				bad();
			}
		}
		if (yp[Y[i]][Z[i]].second > X[i]) {
			if (!s.count({X[i] + 1, Y[i], Z[i]}) && !s.count({X[i], Y[i], Z[i] - 1})) {
				bad();
			}
		}
		if (ys[Y[i]][Z[i]].first < X[i]) {
			if (!s.count({X[i] - 1, Y[i], Z[i]}) && !s.count({X[i], Y[i], Z[i] + 1})) {
				bad();
			}
		}
		if (ys[Y[i]][Z[i]].second > X[i]) {
			if (!s.count({X[i] + 1, Y[i], Z[i]}) && !s.count({X[i], Y[i], Z[i] + 1})) {
				bad();
			}
		}
		if (zp[Z[i]][X[i]].first < Y[i]) {
			if (!s.count({X[i] - 1, Y[i], Z[i]}) && !s.count({X[i], Y[i] - 1, Z[i]})) {
				bad();
			}
		}
		if (zp[Z[i]][X[i]].second > Y[i]) {
			if (!s.count({X[i] - 1, Y[i], Z[i]}) && !s.count({X[i], Y[i] + 1, Z[i]})) {
				bad();
			}
		}
		if (zs[Z[i]][X[i]].first < Y[i]) {
			if (!s.count({X[i] + 1, Y[i], Z[i]}) && !s.count({X[i], Y[i] - 1, Z[i]})) {
				bad();
			}
		}
		if (zs[Z[i]][X[i]].second > Y[i]) {
			if (!s.count({X[i] + 1, Y[i], Z[i]}) && !s.count({X[i], Y[i] + 1, Z[i]})) {
				bad();
			}
		}
	}
	// check connectivity
	vector<int> v(N);
	auto dfs = [&](auto dfs, int i) {
		if (v[i]) {
			return;
		}
		v[i] = true;
		if (s.count({X[i] + 1, Y[i], Z[i]})) {
			dfs(dfs, s[{X[i] + 1, Y[i], Z[i]}]);
		}
		if (s.count({X[i] - 1, Y[i], Z[i]})) {
			dfs(dfs, s[{X[i] - 1, Y[i], Z[i]}]);
		}
		if (s.count({X[i], Y[i] + 1, Z[i]})) {
			dfs(dfs, s[{X[i], Y[i] + 1, Z[i]}]);
		}
		if (s.count({X[i], Y[i] - 1, Z[i]})) {
			dfs(dfs, s[{X[i], Y[i] - 1, Z[i]}]);
		}
		if (s.count({X[i], Y[i], Z[i] + 1})) {
			dfs(dfs, s[{X[i], Y[i], Z[i] + 1}]);
		}
		if (s.count({X[i], Y[i], Z[i] - 1})) {
			dfs(dfs, s[{X[i], Y[i], Z[i] - 1}]);
		}
	};
	dfs(dfs, 0);
	if (count(v.begin(), v.end(), true) != N) {
		bad();
	}
	cout << "YES" << '\n';
	return 0;
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted3ms1960 KiB
2Accepted3ms2188 KiB
3Accepted3ms2648 KiB
subtask218/18
4Accepted3ms2716 KiB
5Accepted3ms3036 KiB
6Accepted3ms3284 KiB
7Accepted3ms3280 KiB
8Accepted3ms3464 KiB
9Accepted3ms3572 KiB
10Accepted3ms3820 KiB
11Accepted3ms3760 KiB
12Accepted3ms3760 KiB
13Accepted3ms4052 KiB
subtask319/19
14Accepted16ms8916 KiB
15Accepted3ms4228 KiB
16Accepted9ms6192 KiB
17Accepted10ms6392 KiB
18Accepted64ms9412 KiB
19Accepted61ms9148 KiB
20Accepted3ms4260 KiB
21Accepted7ms5632 KiB
22Accepted13ms6532 KiB
23Accepted12ms6644 KiB
24Accepted12ms6608 KiB
25Accepted61ms9244 KiB
26Accepted3ms4264 KiB
subtask424/24
27Accepted195ms51212 KiB
28Accepted17ms8160 KiB
29Accepted1.106s107792 KiB
30Accepted1.111s115116 KiB
31Accepted1.157s107760 KiB
32Accepted1.1s108896 KiB
33Accepted4ms5552 KiB
34Accepted219ms51816 KiB
35Accepted1.161s102008 KiB
36Accepted208ms52028 KiB
37Accepted1.243s100988 KiB
38Accepted465ms77400 KiB
39Accepted4ms5380 KiB
subtask522/22
40Accepted186ms39560 KiB
41Accepted4ms5040 KiB
42Accepted43ms14344 KiB
43Accepted150ms34296 KiB
44Accepted1.291s71172 KiB
45Accepted1.332s72496 KiB
46Accepted3ms5032 KiB
47Accepted3ms4800 KiB
48Accepted28ms11256 KiB
49Accepted140ms31372 KiB
50Accepted149ms34404 KiB
51Accepted162ms34156 KiB
52Accepted1.264s73272 KiB
53Accepted3ms4780 KiB
subtask617/17
54Accepted423ms90884 KiB
55Accepted1.769s199324 KiB
56Accepted175ms36604 KiB
57Accepted165ms34872 KiB
58Accepted158ms35572 KiB
59Accepted1.291s72948 KiB
60Accepted1.149s159152 KiB
61Accepted177ms37316 KiB
62Accepted163ms35312 KiB
63Accepted163ms35304 KiB
64Accepted1.279s72204 KiB
65Accepted1.286s72592 KiB
66Accepted221ms71264 KiB