Submission #1050691

# Submission time Handle Problem Language Result Execution time Memory
1050691 2024-08-09T12:41:36 Z Zicrus Fountain Parks (IOI21_parks) C++17
15 / 100
3500 ms 95432 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(32523);
    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_map<ll, ll> lnk;
    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;
        poss[hashL]++;
        poss[hashR]++;
        lnk[hashL] ^= i;
        lnk[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);
    }
    ll cnt = 0;
    unordered_set<ll> used;
    while (cnt < edges.size()) {
        while (!q.empty()) {
            ll hash = q.front(); q.pop();
            if (poss[hash] != 1 || used.count(hash)) continue;
            cnt++;
            ll id = lnk[hash];
            a[id] = hash >> 31;
            b[id] = hash & ~(1 << 31);
            used.insert(id);
            if (--poss[edgeHashes[id].first] == 1) q.push(edgeHashes[id].first);
            if (--poss[edgeHashes[id].second] == 1) q.push(edgeHashes[id].second);
            lnk[edgeHashes[id].first] ^= id;
            lnk[edgeHashes[id].second] ^= id;
        }
        if (cnt < edges.size()) {
            ll v = 0;
            for (auto &e : poss) {
                if (e.second != 2) continue;
                v = e.first;
                break;
            }
            for (int i = 0; i < edges.size(); i++) {
                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);
                cnt++;
                used.insert(i);
                break;
            }

            //
            poss.clear();
            lnk.clear();
            for (int i = 0; i < edges.size(); i++) {
                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;
                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;
                poss[hashL]++;
                poss[hashR]++;
                lnk[hashL] ^= i;
                lnk[hashR] ^= i;
            }
            for (int i = 0; i < edges.size(); i++) {
                if (!used.count(i)) continue;
                ll hash = ((ll)a[i] << 31) | (ll)b[i];
                poss[hash] = 0;
            }
            for (auto &e : poss) {
                if (e.second != 1) continue;
                q.push(e.first);
            }
        }
    }
 
    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:84: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]
   84 |     for (int i = 0; i < edges.size(); i++) {
      |                     ~~^~~~~~~~~~~~~~
parks.cpp:109:16: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  109 |     while (cnt < edges.size()) {
      |            ~~~~^~~~~~~~~~~~~~
parks.cpp:123:17: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  123 |         if (cnt < edges.size()) {
      |             ~~~~^~~~~~~~~~~~~~
parks.cpp:130:31: 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]
  130 |             for (int i = 0; i < edges.size(); i++) {
      |                             ~~^~~~~~~~~~~~~~
parks.cpp:146:31: 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]
  146 |             for (int i = 0; i < edges.size(); i++) {
      |                             ~~^~~~~~~~~~~~~~
parks.cpp:162:31: 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]
  162 |             for (int i = 0; i < edges.size(); i++) {
      |                             ~~^~~~~~~~~~~~~~
parks.cpp:175: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]
  175 |     for (int i = 0; i < edges.size(); i++) {
      |                     ~~^~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 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 152 ms 45712 KB Output is correct
10 Correct 10 ms 4564 KB Output is correct
11 Correct 70 ms 24180 KB Output is correct
12 Correct 15 ms 6796 KB Output is correct
13 Correct 55 ms 19560 KB Output is correct
14 Correct 1 ms 600 KB Output is correct
15 Correct 2 ms 1116 KB Output is correct
16 Correct 154 ms 45708 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 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 152 ms 45712 KB Output is correct
10 Correct 10 ms 4564 KB Output is correct
11 Correct 70 ms 24180 KB Output is correct
12 Correct 15 ms 6796 KB Output is correct
13 Correct 55 ms 19560 KB Output is correct
14 Correct 1 ms 600 KB Output is correct
15 Correct 2 ms 1116 KB Output is correct
16 Correct 154 ms 45708 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 0 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 412 ms 95000 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 1 ms 860 KB Output is correct
26 Correct 3 ms 1524 KB Output is correct
27 Correct 4 ms 2064 KB Output is correct
28 Correct 132 ms 38080 KB Output is correct
29 Correct 250 ms 61560 KB Output is correct
30 Correct 318 ms 75900 KB Output is correct
31 Correct 436 ms 95432 KB Output is correct
32 Correct 0 ms 348 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 348 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 2 ms 1116 KB Output is correct
44 Correct 2 ms 1372 KB Output is correct
45 Correct 137 ms 38928 KB Output is correct
46 Correct 212 ms 57784 KB Output is correct
47 Correct 216 ms 57468 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 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 152 ms 45712 KB Output is correct
10 Correct 10 ms 4564 KB Output is correct
11 Correct 70 ms 24180 KB Output is correct
12 Correct 15 ms 6796 KB Output is correct
13 Correct 55 ms 19560 KB Output is correct
14 Correct 1 ms 600 KB Output is correct
15 Correct 2 ms 1116 KB Output is correct
16 Correct 154 ms 45708 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 0 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 412 ms 95000 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 1 ms 860 KB Output is correct
26 Correct 3 ms 1524 KB Output is correct
27 Correct 4 ms 2064 KB Output is correct
28 Correct 132 ms 38080 KB Output is correct
29 Correct 250 ms 61560 KB Output is correct
30 Correct 318 ms 75900 KB Output is correct
31 Correct 436 ms 95432 KB Output is correct
32 Correct 0 ms 348 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 348 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 2 ms 1116 KB Output is correct
44 Correct 2 ms 1372 KB Output is correct
45 Correct 137 ms 38928 KB Output is correct
46 Correct 212 ms 57784 KB Output is correct
47 Correct 216 ms 57468 KB Output is correct
48 Correct 0 ms 344 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 0 ms 348 KB Output is correct
55 Execution timed out 3603 ms 75792 KB Time limit exceeded
56 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 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 152 ms 45712 KB Output is correct
10 Correct 10 ms 4564 KB Output is correct
11 Correct 70 ms 24180 KB Output is correct
12 Correct 15 ms 6796 KB Output is correct
13 Correct 55 ms 19560 KB Output is correct
14 Correct 1 ms 600 KB Output is correct
15 Correct 2 ms 1116 KB Output is correct
16 Correct 154 ms 45708 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 273 ms 73596 KB Output is correct
21 Correct 316 ms 73132 KB Output is correct
22 Correct 301 ms 73088 KB Output is correct
23 Correct 259 ms 70336 KB Output is correct
24 Correct 55 ms 20940 KB Output is correct
25 Correct 275 ms 85376 KB Output is correct
26 Correct 302 ms 85640 KB Output is correct
27 Correct 320 ms 91148 KB Output is correct
28 Correct 323 ms 91136 KB Output is correct
29 Correct 325 ms 90884 KB Output is correct
30 Correct 326 ms 90888 KB Output is correct
31 Correct 0 ms 348 KB Output is correct
32 Correct 339 ms 5552 KB Output is correct
33 Correct 25 ms 10604 KB Output is correct
34 Correct 303 ms 73212 KB Output is correct
35 Correct 302 ms 3540 KB Output is correct
36 Execution timed out 3553 ms 16968 KB Time limit exceeded
37 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 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 152 ms 45712 KB Output is correct
10 Correct 10 ms 4564 KB Output is correct
11 Correct 70 ms 24180 KB Output is correct
12 Correct 15 ms 6796 KB Output is correct
13 Correct 55 ms 19560 KB Output is correct
14 Correct 1 ms 600 KB Output is correct
15 Correct 2 ms 1116 KB Output is correct
16 Correct 154 ms 45708 KB Output is correct
17 Correct 420 ms 91652 KB Output is correct
18 Correct 439 ms 91276 KB Output is correct
19 Correct 366 ms 73076 KB Output is correct
20 Execution timed out 3561 ms 63096 KB Time limit exceeded
21 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 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 152 ms 45712 KB Output is correct
10 Correct 10 ms 4564 KB Output is correct
11 Correct 70 ms 24180 KB Output is correct
12 Correct 15 ms 6796 KB Output is correct
13 Correct 55 ms 19560 KB Output is correct
14 Correct 1 ms 600 KB Output is correct
15 Correct 2 ms 1116 KB Output is correct
16 Correct 154 ms 45708 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 0 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 412 ms 95000 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 1 ms 860 KB Output is correct
26 Correct 3 ms 1524 KB Output is correct
27 Correct 4 ms 2064 KB Output is correct
28 Correct 132 ms 38080 KB Output is correct
29 Correct 250 ms 61560 KB Output is correct
30 Correct 318 ms 75900 KB Output is correct
31 Correct 436 ms 95432 KB Output is correct
32 Correct 0 ms 348 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 348 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 2 ms 1116 KB Output is correct
44 Correct 2 ms 1372 KB Output is correct
45 Correct 137 ms 38928 KB Output is correct
46 Correct 212 ms 57784 KB Output is correct
47 Correct 216 ms 57468 KB Output is correct
48 Correct 0 ms 344 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 0 ms 348 KB Output is correct
55 Execution timed out 3603 ms 75792 KB Time limit exceeded
56 Halted 0 ms 0 KB -