Submission #1057543

# Submission time Handle Problem Language Result Execution time Memory
1057543 2024-08-13T21:21:01 Z Zicrus Fountain Parks (IOI21_parks) C++17
55 / 100
761 ms 191660 KB
#include <bits/stdc++.h>
#include "parks.h"
using namespace std;
 
typedef long long ll;
 
ll n;
 
vector<ll> lnk;
vector<ll> sz;
 
ll find(ll a) {
    if (a != lnk[a]) lnk[a] = find(lnk[a]);
    return lnk[a];
}
 
bool unite(ll a, ll b) {
    a = find(a); b = find(b);
    if (a == b) return false;
    if (sz[a] > sz[b]) swap(a, b);
    lnk[a] = b;
    sz[b] += sz[a];
    return true;
}
 
int construct_roads(vector<int> x, vector<int> y) {
    n = x.size();
    mt19937 mt(743);
    vector<pair<pair<ll, ll>, ll>> coords(n);
    for (int i = 0; i < n; i++) coords[i] = {{x[i], y[i]}, i};
    shuffle(coords.begin(), coords.end(), mt);
    for (int i = 0; i < n; i++) {
        x[i] = coords[i].first.first;
        y[i] = coords[i].first.second;
    }
    lnk = vector<ll>(n);
    sz = vector<ll>(n, 1);
    for (int i = 0; i < n; i++) lnk[i] = i;
    unordered_map<ll, ll> pts;
    vector<pair<ll, ll>> edges;
    unordered_set<ll> hasEdge;
    for (int i = 0; i < n; i++) {
        ll hash = ((ll)x[i] << 31) | (ll)y[i];
        pts[hash] = i;
    }
    for (ll i = 0; i < n; i++) {
        ll hashL = (((ll)x[i]-2) << 31) | ((ll)y[i]);
        if (pts.count(hashL)) {
            ll hash = (min(i, pts[hashL]) << 31) | max(i, pts[hashL]);
            if (!hasEdge.count(hash)) {
                edges.push_back({i, pts[hashL]});
                hasEdge.insert(hash);
            }
        }
        ll hashR = (((ll)x[i]+2) << 31) | ((ll)y[i]);
        if (pts.count(hashR)) {
            ll hash = (min(i, pts[hashR]) << 31) | max(i, pts[hashR]);
            if (!hasEdge.count(hash)) {
                edges.push_back({i, pts[hashR]});
                hasEdge.insert(hash);
            }
        }
        ll hashD = (((ll)x[i]) << 31) | ((ll)y[i]-2);
        if (pts.count(hashD)) {
            ll hash = (min(i, pts[hashD]) << 31) | max(i, pts[hashD]);
            if (!hasEdge.count(hash)) {
                edges.push_back({i, pts[hashD]});
                hasEdge.insert(hash);
            }
        }
        ll hashU = (((ll)x[i]) << 31) | ((ll)y[i]+2);
        if (pts.count(hashU)) {
            ll hash = (min(i, pts[hashU]) << 31) | max(i, pts[hashU]);
            if (!hasEdge.count(hash)) {
                edges.push_back({i, pts[hashU]});
                hasEdge.insert(hash);
            }
        }
    }
 
    unordered_map<ll, ll> poss;
    unordered_set<ll> twos;
    unordered_map<ll, ll> lnk, lnkU;
    vector<pair<ll, ll>> edgeHashes(edges.size());
    for (int i = 0; i < edges.size(); i++) {
        auto e = edges[i];
        ll midX = (x[e.first] + x[e.second]) / 2;
        ll midY = (y[e.first] + y[e.second]) / 2;
        ll leftX = midX - (midY - y[e.second]);
        ll leftY = midY + (midX - x[e.second]);
        ll rightX = midX + (midY - y[e.second]);
        ll rightY = midY - (midX - x[e.second]);
        ll hashL = (leftX << 31) | leftY;
        ll hashR = (rightX << 31) | rightY;
        if (twos.count(hashL)) twos.erase(hashL);
        poss[hashL]++;
        if (poss[hashL] >= 2) twos.insert(hashL);
        if (twos.count(hashR)) twos.erase(hashR);
        poss[hashR]++;
        if (poss[hashR] >= 2) twos.insert(hashR);
        lnk[hashL] ^= i;
        lnk[hashR] ^= i;
        lnkU[hashL] = i;
        lnkU[hashR] = i;
        edgeHashes[i] = {hashL, hashR};
    }
 
    vector<int> a(edges.size()), b(edges.size());
    queue<ll> q;
    for (auto &e : poss) {
        if (e.second != 1) continue;
        q.push(e.first);
    }
    unordered_set<ll> used;
    while (used.size() < edges.size()) {
        while (!q.empty()) {
            ll hash = q.front(); q.pop();
            if (poss[hash] != 1 || used.count(hash)) continue;
            ll id = lnk[hash];
            a[id] = hash >> 31;
            b[id] = hash & ~(1 << 31);
            used.insert(id);
            if (twos.count(edgeHashes[id].first)) twos.erase(edgeHashes[id].first);
            if (--poss[edgeHashes[id].first] == 1) q.push(edgeHashes[id].first);
            if (poss[edgeHashes[id].first] >= 2) twos.insert(edgeHashes[id].first);
            if (twos.count(edgeHashes[id].second)) twos.erase(edgeHashes[id].second);
            if (--poss[edgeHashes[id].second] == 1) q.push(edgeHashes[id].second);
            if (poss[edgeHashes[id].second] >= 2) twos.insert(edgeHashes[id].second);
            lnk[edgeHashes[id].first] ^= id;
            lnk[edgeHashes[id].second] ^= id;
        }
        if (used.size() < edges.size() && twos.empty()) {
            throw;
            for (auto &e : poss) {
                if (e.second == 1) q.push(e.first);
                if (e.second >= 2) twos.insert(e.first);
            }
        }
        else if (used.size() < edges.size() && !twos.empty()) {
            ll v = *twos.begin();
            twos.erase(v);
            for (int i = 0; i < 1; i = 10) {
                i = lnkU[v];
                if (used.count(i)) continue;
                auto e = edges[i];
                ll midX = (x[e.first] + x[e.second]) / 2;
                ll midY = (y[e.first] + y[e.second]) / 2;
                if (abs(midX - (v >> 31)) + abs(midY - (v & ~(1 << 31))) > 1) continue;
                a[i] = v >> 31;
                b[i] = v & ~(1 << 31);
                poss[v] = 0;
                ll otherX = a[i] + 2 * (midX - a[i]);
                ll otherY = b[i] + 2 * (midY - b[i]);
                ll otherHash = (otherX << 31) | otherY;
                if (twos.count(otherHash)) twos.erase(otherHash);
                poss[otherHash]--;
                if (poss[otherHash] >= 2) twos.insert(otherHash);
                lnk[otherHash] ^= i;
                while (poss[otherHash] == 1) {
                    ll edge = lnk[otherHash];
                    auto e = edges[edge];
                    ll midX = (x[e.first] + x[e.second]) / 2;
                    ll midY = (y[e.first] + y[e.second]) / 2;
                    a[edge] = otherHash >> 31;
                    b[edge] = otherHash & ~(1 << 31);
                    ll otherX = a[edge] + 2 * (midX - a[edge]);
                    ll otherY = b[edge] + 2 * (midY - b[edge]);
                    otherHash = (otherX << 31) | otherY;
                    if (twos.count(otherHash)) twos.erase(otherHash);
                    poss[otherHash]--;
                    if (poss[otherHash] >= 2) twos.insert(otherHash);
                    lnk[otherHash] ^= edge;
                    used.insert(edge);
                }
                used.insert(i);
                break;
            }
        }
    }
 
    vector<int> u, v;
    for (int i = 0; i < edges.size(); i++) {
        u.push_back(coords[edges[i].first].second);
        v.push_back(coords[edges[i].second].second);
        unite(edges[i].first, edges[i].second);
    }
 
    if (sz[find(0)] < n) return 0;
 
    build(u, v, a, b);
    return 1;
}

Compilation message

parks.cpp: In function 'int construct_roads(std::vector<int>, std::vector<int>)':
parks.cpp:85:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   85 |     for (int i = 0; i < edges.size(); i++) {
      |                     ~~^~~~~~~~~~~~~~
parks.cpp:182:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  182 |     for (int i = 0; i < edges.size(); i++) {
      |                     ~~^~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 211 ms 55656 KB Output is correct
10 Correct 11 ms 5336 KB Output is correct
11 Correct 84 ms 29316 KB Output is correct
12 Correct 18 ms 8172 KB Output is correct
13 Correct 61 ms 24016 KB Output is correct
14 Correct 1 ms 856 KB Output is correct
15 Correct 2 ms 1372 KB Output is correct
16 Correct 253 ms 55528 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 211 ms 55656 KB Output is correct
10 Correct 11 ms 5336 KB Output is correct
11 Correct 84 ms 29316 KB Output is correct
12 Correct 18 ms 8172 KB Output is correct
13 Correct 61 ms 24016 KB Output is correct
14 Correct 1 ms 856 KB Output is correct
15 Correct 2 ms 1372 KB Output is correct
16 Correct 253 ms 55528 KB Output is correct
17 Correct 1 ms 344 KB Output is correct
18 Correct 0 ms 344 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 344 KB Output is correct
23 Correct 761 ms 109972 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 2 ms 1116 KB Output is correct
26 Correct 4 ms 1796 KB Output is correct
27 Correct 6 ms 2396 KB Output is correct
28 Correct 187 ms 44316 KB Output is correct
29 Correct 446 ms 70944 KB Output is correct
30 Correct 512 ms 88640 KB Output is correct
31 Correct 734 ms 109428 KB Output is correct
32 Correct 1 ms 344 KB Output is correct
33 Correct 0 ms 348 KB Output is correct
34 Correct 0 ms 348 KB Output is correct
35 Correct 0 ms 348 KB Output is correct
36 Correct 0 ms 348 KB Output is correct
37 Correct 0 ms 432 KB Output is correct
38 Correct 0 ms 348 KB Output is correct
39 Correct 0 ms 348 KB Output is correct
40 Correct 0 ms 348 KB Output is correct
41 Correct 0 ms 348 KB Output is correct
42 Correct 0 ms 348 KB Output is correct
43 Correct 3 ms 1256 KB Output is correct
44 Correct 3 ms 1628 KB Output is correct
45 Correct 191 ms 45904 KB Output is correct
46 Correct 377 ms 69056 KB Output is correct
47 Correct 394 ms 68980 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 211 ms 55656 KB Output is correct
10 Correct 11 ms 5336 KB Output is correct
11 Correct 84 ms 29316 KB Output is correct
12 Correct 18 ms 8172 KB Output is correct
13 Correct 61 ms 24016 KB Output is correct
14 Correct 1 ms 856 KB Output is correct
15 Correct 2 ms 1372 KB Output is correct
16 Correct 253 ms 55528 KB Output is correct
17 Correct 1 ms 344 KB Output is correct
18 Correct 0 ms 344 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 344 KB Output is correct
23 Correct 761 ms 109972 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 2 ms 1116 KB Output is correct
26 Correct 4 ms 1796 KB Output is correct
27 Correct 6 ms 2396 KB Output is correct
28 Correct 187 ms 44316 KB Output is correct
29 Correct 446 ms 70944 KB Output is correct
30 Correct 512 ms 88640 KB Output is correct
31 Correct 734 ms 109428 KB Output is correct
32 Correct 1 ms 344 KB Output is correct
33 Correct 0 ms 348 KB Output is correct
34 Correct 0 ms 348 KB Output is correct
35 Correct 0 ms 348 KB Output is correct
36 Correct 0 ms 348 KB Output is correct
37 Correct 0 ms 432 KB Output is correct
38 Correct 0 ms 348 KB Output is correct
39 Correct 0 ms 348 KB Output is correct
40 Correct 0 ms 348 KB Output is correct
41 Correct 0 ms 348 KB Output is correct
42 Correct 0 ms 348 KB Output is correct
43 Correct 3 ms 1256 KB Output is correct
44 Correct 3 ms 1628 KB Output is correct
45 Correct 191 ms 45904 KB Output is correct
46 Correct 377 ms 69056 KB Output is correct
47 Correct 394 ms 68980 KB Output is correct
48 Correct 1 ms 344 KB Output is correct
49 Correct 0 ms 344 KB Output is correct
50 Correct 0 ms 348 KB Output is correct
51 Correct 0 ms 348 KB Output is correct
52 Correct 0 ms 348 KB Output is correct
53 Correct 0 ms 348 KB Output is correct
54 Correct 0 ms 348 KB Output is correct
55 Runtime error 580 ms 191660 KB Execution killed with signal 6
56 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 211 ms 55656 KB Output is correct
10 Correct 11 ms 5336 KB Output is correct
11 Correct 84 ms 29316 KB Output is correct
12 Correct 18 ms 8172 KB Output is correct
13 Correct 61 ms 24016 KB Output is correct
14 Correct 1 ms 856 KB Output is correct
15 Correct 2 ms 1372 KB Output is correct
16 Correct 253 ms 55528 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 510 ms 87916 KB Output is correct
21 Correct 539 ms 87420 KB Output is correct
22 Correct 502 ms 87932 KB Output is correct
23 Correct 381 ms 85948 KB Output is correct
24 Correct 59 ms 22472 KB Output is correct
25 Correct 393 ms 106028 KB Output is correct
26 Correct 488 ms 105320 KB Output is correct
27 Correct 495 ms 110900 KB Output is correct
28 Correct 496 ms 110848 KB Output is correct
29 Correct 457 ms 111672 KB Output is correct
30 Correct 447 ms 110820 KB Output is correct
31 Correct 0 ms 348 KB Output is correct
32 Correct 22 ms 6600 KB Output is correct
33 Correct 31 ms 11620 KB Output is correct
34 Correct 489 ms 87420 KB Output is correct
35 Correct 12 ms 4568 KB Output is correct
36 Correct 76 ms 21092 KB Output is correct
37 Correct 183 ms 41660 KB Output is correct
38 Correct 140 ms 35880 KB Output is correct
39 Correct 205 ms 48832 KB Output is correct
40 Correct 305 ms 60604 KB Output is correct
41 Correct 433 ms 74940 KB Output is correct
42 Correct 489 ms 89016 KB Output is correct
43 Correct 0 ms 432 KB Output is correct
44 Correct 0 ms 348 KB Output is correct
45 Correct 0 ms 348 KB Output is correct
46 Correct 0 ms 348 KB Output is correct
47 Correct 0 ms 348 KB Output is correct
48 Correct 0 ms 440 KB Output is correct
49 Correct 0 ms 348 KB Output is correct
50 Correct 0 ms 348 KB Output is correct
51 Correct 0 ms 348 KB Output is correct
52 Correct 0 ms 348 KB Output is correct
53 Correct 0 ms 348 KB Output is correct
54 Correct 2 ms 1116 KB Output is correct
55 Correct 3 ms 1628 KB Output is correct
56 Correct 203 ms 46016 KB Output is correct
57 Correct 352 ms 68848 KB Output is correct
58 Correct 344 ms 68664 KB Output is correct
59 Correct 0 ms 348 KB Output is correct
60 Correct 0 ms 348 KB Output is correct
61 Correct 0 ms 348 KB Output is correct
62 Correct 605 ms 111336 KB Output is correct
63 Correct 614 ms 111048 KB Output is correct
64 Correct 600 ms 111028 KB Output is correct
65 Correct 4 ms 2140 KB Output is correct
66 Correct 9 ms 3932 KB Output is correct
67 Correct 204 ms 45704 KB Output is correct
68 Correct 403 ms 69564 KB Output is correct
69 Correct 533 ms 91660 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 211 ms 55656 KB Output is correct
10 Correct 11 ms 5336 KB Output is correct
11 Correct 84 ms 29316 KB Output is correct
12 Correct 18 ms 8172 KB Output is correct
13 Correct 61 ms 24016 KB Output is correct
14 Correct 1 ms 856 KB Output is correct
15 Correct 2 ms 1372 KB Output is correct
16 Correct 253 ms 55528 KB Output is correct
17 Correct 643 ms 111508 KB Output is correct
18 Correct 593 ms 111440 KB Output is correct
19 Correct 513 ms 87336 KB Output is correct
20 Correct 557 ms 99788 KB Output is correct
21 Correct 447 ms 92148 KB Output is correct
22 Correct 1 ms 344 KB Output is correct
23 Correct 53 ms 14540 KB Output is correct
24 Correct 30 ms 9772 KB Output is correct
25 Correct 124 ms 32512 KB Output is correct
26 Correct 246 ms 54716 KB Output is correct
27 Correct 218 ms 49144 KB Output is correct
28 Correct 297 ms 59940 KB Output is correct
29 Correct 396 ms 76660 KB Output is correct
30 Correct 506 ms 88432 KB Output is correct
31 Correct 578 ms 98196 KB Output is correct
32 Correct 687 ms 101888 KB Output is correct
33 Correct 567 ms 111176 KB Output is correct
34 Correct 7 ms 2700 KB Output is correct
35 Correct 14 ms 4272 KB Output is correct
36 Correct 214 ms 47812 KB Output is correct
37 Correct 446 ms 73656 KB Output is correct
38 Correct 592 ms 95956 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 211 ms 55656 KB Output is correct
10 Correct 11 ms 5336 KB Output is correct
11 Correct 84 ms 29316 KB Output is correct
12 Correct 18 ms 8172 KB Output is correct
13 Correct 61 ms 24016 KB Output is correct
14 Correct 1 ms 856 KB Output is correct
15 Correct 2 ms 1372 KB Output is correct
16 Correct 253 ms 55528 KB Output is correct
17 Correct 1 ms 344 KB Output is correct
18 Correct 0 ms 344 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 344 KB Output is correct
23 Correct 761 ms 109972 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 2 ms 1116 KB Output is correct
26 Correct 4 ms 1796 KB Output is correct
27 Correct 6 ms 2396 KB Output is correct
28 Correct 187 ms 44316 KB Output is correct
29 Correct 446 ms 70944 KB Output is correct
30 Correct 512 ms 88640 KB Output is correct
31 Correct 734 ms 109428 KB Output is correct
32 Correct 1 ms 344 KB Output is correct
33 Correct 0 ms 348 KB Output is correct
34 Correct 0 ms 348 KB Output is correct
35 Correct 0 ms 348 KB Output is correct
36 Correct 0 ms 348 KB Output is correct
37 Correct 0 ms 432 KB Output is correct
38 Correct 0 ms 348 KB Output is correct
39 Correct 0 ms 348 KB Output is correct
40 Correct 0 ms 348 KB Output is correct
41 Correct 0 ms 348 KB Output is correct
42 Correct 0 ms 348 KB Output is correct
43 Correct 3 ms 1256 KB Output is correct
44 Correct 3 ms 1628 KB Output is correct
45 Correct 191 ms 45904 KB Output is correct
46 Correct 377 ms 69056 KB Output is correct
47 Correct 394 ms 68980 KB Output is correct
48 Correct 1 ms 344 KB Output is correct
49 Correct 0 ms 344 KB Output is correct
50 Correct 0 ms 348 KB Output is correct
51 Correct 0 ms 348 KB Output is correct
52 Correct 0 ms 348 KB Output is correct
53 Correct 0 ms 348 KB Output is correct
54 Correct 0 ms 348 KB Output is correct
55 Runtime error 580 ms 191660 KB Execution killed with signal 6
56 Halted 0 ms 0 KB -