69682023-12-22 00:41:40TuruTamasPac-Mancpp17Accepted 100/1001.057s79624 KiB
#include <bits/stdc++.h>
using namespace std;

#ifdef DEBUG
ifstream in_file("minta/be1.txt");
#define input in_file
#define INTHENAMEOFGOD
#else
#define input cin
#define INTHENAMEOFGOD \
    ios::sync_with_stdio(0); \
    cin.tie(0); \
    cout.tie(0);
#endif
typedef long long ll;
typedef vector<ll> vi;
typedef vector<vi> vvi;
typedef vector<bool> vb;
typedef pair<ll, ll> pii;
typedef array<ll, 3> pt;

ll N, x;
vector<pt> ps, points, sp;
set<pt> layer;
map<pt, bool> m;
set<int> ax;

void dfs(pt x) {
    m[x] = true;
    for (int axis : ax) {
        pt next = x;
        next[axis]++;
        if (layer.count(next) && !m[next]) {
            dfs(next);
        }
        next[axis] -= 2;
        if (layer.count(next) && !m[next]) {
            dfs(next);
        }
    }
}

void validate_layer(int i) {
    ax = {0, 1, 2};
    ax.erase(i);
    m.clear();
    dfs(*layer.begin());
    for (pt p : layer) {
        if (!m[p]) {
            cout << "NO\n";
            exit(0);
        }
    }

    sp.clear();
    for (pt p : layer) {
        sp.push_back(p);
    }

    int a = *ax.begin();
    int b = *++ax.begin();
    for (int i = 0; i < 2; i++) {
        sort(sp.begin(), sp.end(), [&](pt l, pt r) {
            if (l[a] == r[a])
                return l[b] < r[b];
            return l[a] < r[a];
        });

        ll row = -1;
        pt last;
        for (pt p : sp) {
            if (p[a] != row)
                row = p[a];
            else if (p[b] != last[b]+1) {
                cout << "NO\n";
                exit(0);
            }
            last = p;
        }

        swap(a, b);
    }
}

void dfs3d(pt x) {
    m[x] = true;
    for (int axis : ax) {
        pt next = x;
        next[axis]++;
        if (layer.count(next) && !m[next])
            dfs3d(next);
        next[axis] -= 2;
        if (layer.count(next) && !m[next])
            dfs3d(next);
    }
}

int main() {
    INTHENAMEOFGOD
    input >> N;
    points.resize(N);
    ps.resize(N);
    for (ll n = 0; n < N; n++)
        input >> points[n][0];
    for (ll n = 0; n < N; n++)
        input >> points[n][1];
    for (ll n = 0; n < N; n++)
        input >> points[n][2];

    for (pt p : points)
        layer.insert(p);
    ax = {0, 1, 2};
    dfs3d(points[0]);
    for (pt p : points) {
        if (!m[p]) {
            cout << "NO\n";
            exit(0);
        }
    }
    layer.clear();

    copy_n(points.begin(), N, ps.begin());
    for (int axis = 0; axis < 3; axis++) {
        sort(ps.begin(), ps.end(), [&](pt a, pt b) {
            return a[axis] < b[axis];
        });
        ll layer_ind = ps[0][axis];
        layer.clear();
        for (pt p : ps) {
            if (p[axis] != layer_ind) {
                validate_layer(axis);
                layer_ind = p[axis];
                layer.clear();
            }
            layer.insert(p);
        }
        validate_layer(axis);
    }
    cout << "YES\n";
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted3ms1976 KiB
2Accepted3ms2204 KiB
3Accepted3ms2276 KiB
subtask218/18
4Accepted3ms2652 KiB
5Accepted3ms2748 KiB
6Accepted3ms3112 KiB
7Accepted3ms3188 KiB
8Accepted3ms3440 KiB
9Accepted3ms3512 KiB
10Accepted3ms3556 KiB
11Accepted4ms3620 KiB
12Accepted3ms3684 KiB
13Accepted3ms3900 KiB
subtask319/19
14Accepted7ms5204 KiB
15Accepted3ms4008 KiB
16Accepted18ms6556 KiB
17Accepted34ms8116 KiB
18Accepted61ms8244 KiB
19Accepted59ms8120 KiB
20Accepted3ms4132 KiB
21Accepted16ms6552 KiB
22Accepted26ms8760 KiB
23Accepted28ms9060 KiB
24Accepted45ms8952 KiB
25Accepted25ms8912 KiB
26Accepted3ms4532 KiB
subtask424/24
27Accepted79ms26220 KiB
28Accepted10ms6032 KiB
29Accepted879ms69740 KiB
30Accepted859ms74324 KiB
31Accepted873ms69656 KiB
32Accepted860ms69732 KiB
33Accepted7ms5392 KiB
34Accepted476ms59028 KiB
35Accepted856ms65900 KiB
36Accepted619ms63888 KiB
37Accepted899ms64184 KiB
38Accepted352ms66652 KiB
39Accepted6ms5276 KiB
subtask522/22
40Accepted78ms26220 KiB
41Accepted3ms4488 KiB
42Accepted109ms21616 KiB
43Accepted510ms67056 KiB
44Accepted1.036s66204 KiB
45Accepted1.036s67112 KiB
46Accepted3ms4492 KiB
47Accepted3ms4492 KiB
48Accepted74ms15824 KiB
49Accepted477ms55412 KiB
50Accepted490ms65048 KiB
51Accepted786ms66964 KiB
52Accepted425ms67808 KiB
53Accepted3ms4604 KiB
subtask617/17
54Accepted83ms26256 KiB
55Accepted531ms69924 KiB
56Accepted446ms62412 KiB
57Accepted529ms63480 KiB
58Accepted680ms66420 KiB
59Accepted1.057s67584 KiB
60Accepted345ms70316 KiB
61Accepted460ms58616 KiB
62Accepted493ms67448 KiB
63Accepted695ms67836 KiB
64Accepted1.047s66676 KiB
65Accepted411ms67068 KiB
66Accepted490ms79624 KiB