Submission #970346

# Submission time Handle Problem Language Result Execution time Memory
970346 2024-04-26T11:41:11 Z mychecksedad Fountain Parks (IOI21_parks) C++17
30 / 100
394 ms 50912 KB
#include "parks.h"
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define all(x) x.begin(), x.end()
#define en cout << '\n';
#define ll long long int
const int N = 3e5+10;

struct Dsu{
    vector<int> s, p;
    Dsu(int n){
        s.resize(n, 1);
        p.resize(n);
        for(int i = 0; i < n; ++i) p[i] = i;
    }
    int find(int v){
        if(p[v] == v) return v;
        return p[v] = find(p[v]);
    }
    void merge(int a, int b){
        a = find(a);
        b = find(b);
        if(a != b){
            if(s[a] > s[b]) swap(a, b);
            p[a] = b;
            s[b] += s[a];
        }
    }
};
int A[4][2] = {{2, 0}, {0, 2}, {-2, 0}, {0, -2}};
        

int construct_roads(std::vector<int> x, std::vector<int> y) {
    if (x.size() == 1) {
    build({}, {}, {}, {});
        return 1;
    }
    int n = x.size();
    vector<int> u, v, a, b;

    Dsu d(n);
    bool other = 0;
    vector<pair<int, int>> L, R, M;
    for(int i = 0; i < n; ++i){
        if(x[i] == 2) L.pb({y[i], i}); 
        else if(x[i] == 6) R.pb({y[i], i});
        else if(x[i] == 4) M.pb({y[i], i});
        else{
            other = 1;
        }
    }
    if(other){
        vector<array<int, 3>> F;
        for(int i = 0; i < n; ++i) F.pb({x[i], y[i], i}); 
        sort(all(F));
        map<pair<int, int>, int> m;
        map<pair<int, int>, bool> used;
        for(int i = 0; i < n; ++i) m[{x[i], y[i]}] = i + 1;
        for(int pos = 0; pos < n; ++pos){
            int i = F[pos][2];
            for(int j = 0; j < 2; ++j){
                int nx = A[j][0] + x[i], ny = A[j][1] + y[i];
                int val = m[{nx, ny}];
                if(val != 0 && d.find(val - 1) != d.find(i)){
                    d.merge(val - 1, i);
                    u.pb(i);
                    v.pb(val - 1);
                    int fx = A[j][0] == 0 ? x[i] - 1 : x[i] + A[j][0] / 2;
                    int fy = A[j][1] == 0 ? y[i] - 1 : y[i] + A[j][1] / 2;
                    if(used[{fx, fy}]){
                        if(A[j][0] == 0) fx = x[i] + 1;
                        else fy = y[i] + 1;
                    }
                    a.pb(fx);
                    b.pb(fy);
                    used[{fx, fy}] = 1;
                }
            }
        }
    }else{
        sort(all(L));
        sort(all(R));
        sort(all(M));
        for(int i = 0; i + 1 < L.size(); ++i){
            if(L[i].first < L[i + 1].first - 2) continue;
            u.pb(L[i].second);
            v.pb(L[i + 1].second);
            a.pb(1);
            b.pb(L[i + 1].first + L[i].first>>1);
            d.merge(L[i].second, L[i + 1].second);
        }
        for(int i = 0; i + 1 < R.size(); ++i){
            if(R[i].first < R[i + 1].first - 2) continue;
            u.pb(R[i].second);
            v.pb(R[i + 1].second);
            a.pb(7);
            b.pb(R[i + 1].first + R[i].first>>1);
            d.merge(R[i].second, R[i + 1].second);
        }
        for(int i = 0; i + 1 < M.size(); ++i){
            if(M[i].first < M[i + 1].first - 2) continue;
            u.pb(M[i].second);
            v.pb(M[i + 1].second);
            a.pb((M[i].first/2)%2 == 0 ? 5 : 3);
            b.pb(M[i + 1].first + M[i].first>>1);
            d.merge(M[i].second, M[i + 1].second);
        }

        int p = 0, last = -5;
        for(int i = 0; i < M.size(); ++i){
            while(p < R.size() && R[p].first < M[i].first) ++p;
            if(p < R.size() && R[p].first == M[i].first){
                if(last + 2 == M[i].first) continue;
                last = M[i].first;
                u.pb(R[p].second);
                v.pb(M[i].second);
                a.pb(5);
                b.pb(M[i].first + ((M[i].first/2) % 2 == 0 ? -1 : 1));
                d.merge(R[p].second, M[i].second);
            }
        }
        p = 0, last = -5;
        for(int i = 0; i < M.size(); ++i){
            while(p < L.size() && L[p].first < M[i].first) ++p;
            if(p < L.size() && L[p].first == M[i].first){
                if(last + 2 == M[i].first) continue;
                last = M[i].first;
                u.pb(L[p].second);
                v.pb(M[i].second);
                a.pb(3);
                b.pb(M[i].first + ((M[i].first/2) % 2 == 0 ? 1 : -1));
                d.merge(L[p].second, M[i].second);
            }
        }
    }

    if(d.s[d.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:30: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   85 |         for(int i = 0; i + 1 < L.size(); ++i){
      |                        ~~~~~~^~~~~~~~~~
parks.cpp:90:33: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   90 |             b.pb(L[i + 1].first + L[i].first>>1);
parks.cpp:93:30: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   93 |         for(int i = 0; i + 1 < R.size(); ++i){
      |                        ~~~~~~^~~~~~~~~~
parks.cpp:98:33: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   98 |             b.pb(R[i + 1].first + R[i].first>>1);
parks.cpp:101:30: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  101 |         for(int i = 0; i + 1 < M.size(); ++i){
      |                        ~~~~~~^~~~~~~~~~
parks.cpp:106:33: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  106 |             b.pb(M[i + 1].first + M[i].first>>1);
parks.cpp:111:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  111 |         for(int i = 0; i < M.size(); ++i){
      |                        ~~^~~~~~~~~~
parks.cpp:112:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  112 |             while(p < R.size() && R[p].first < M[i].first) ++p;
      |                   ~~^~~~~~~~~~
parks.cpp:113:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  113 |             if(p < R.size() && R[p].first == M[i].first){
      |                ~~^~~~~~~~~~
parks.cpp:124:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  124 |         for(int i = 0; i < M.size(); ++i){
      |                        ~~^~~~~~~~~~
parks.cpp:125:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  125 |             while(p < L.size() && L[p].first < M[i].first) ++p;
      |                   ~~^~~~~~~~~~
parks.cpp:126:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  126 |             if(p < L.size() && L[p].first == M[i].first){
      |                ~~^~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 600 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 344 KB Output is correct
9 Correct 48 ms 8652 KB Output is correct
10 Correct 7 ms 1300 KB Output is correct
11 Correct 24 ms 4820 KB Output is correct
12 Correct 8 ms 1764 KB Output is correct
13 Correct 9 ms 3028 KB Output is correct
14 Correct 1 ms 344 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 51 ms 9160 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 600 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 344 KB Output is correct
9 Correct 48 ms 8652 KB Output is correct
10 Correct 7 ms 1300 KB Output is correct
11 Correct 24 ms 4820 KB Output is correct
12 Correct 8 ms 1764 KB Output is correct
13 Correct 9 ms 3028 KB Output is correct
14 Correct 1 ms 344 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 51 ms 9160 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 1 ms 360 KB Output is correct
19 Correct 1 ms 356 KB Output is correct
20 Correct 1 ms 356 KB Output is correct
21 Correct 1 ms 360 KB Output is correct
22 Correct 0 ms 360 KB Output is correct
23 Correct 133 ms 20300 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 2 ms 356 KB Output is correct
26 Correct 2 ms 604 KB Output is correct
27 Correct 1 ms 616 KB Output is correct
28 Correct 46 ms 8540 KB Output is correct
29 Correct 65 ms 12368 KB Output is correct
30 Correct 92 ms 16704 KB Output is correct
31 Correct 137 ms 20144 KB Output is correct
32 Correct 0 ms 360 KB Output is correct
33 Correct 1 ms 360 KB Output is correct
34 Correct 0 ms 352 KB Output is correct
35 Correct 1 ms 452 KB Output is correct
36 Correct 1 ms 356 KB Output is correct
37 Correct 0 ms 352 KB Output is correct
38 Correct 1 ms 360 KB Output is correct
39 Correct 0 ms 360 KB Output is correct
40 Correct 1 ms 360 KB Output is correct
41 Correct 1 ms 352 KB Output is correct
42 Correct 1 ms 352 KB Output is correct
43 Correct 1 ms 444 KB Output is correct
44 Correct 2 ms 604 KB Output is correct
45 Correct 48 ms 9408 KB Output is correct
46 Correct 70 ms 13880 KB Output is correct
47 Correct 75 ms 14032 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 600 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 344 KB Output is correct
9 Correct 48 ms 8652 KB Output is correct
10 Correct 7 ms 1300 KB Output is correct
11 Correct 24 ms 4820 KB Output is correct
12 Correct 8 ms 1764 KB Output is correct
13 Correct 9 ms 3028 KB Output is correct
14 Correct 1 ms 344 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 51 ms 9160 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 1 ms 360 KB Output is correct
19 Correct 1 ms 356 KB Output is correct
20 Correct 1 ms 356 KB Output is correct
21 Correct 1 ms 360 KB Output is correct
22 Correct 0 ms 360 KB Output is correct
23 Correct 133 ms 20300 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 2 ms 356 KB Output is correct
26 Correct 2 ms 604 KB Output is correct
27 Correct 1 ms 616 KB Output is correct
28 Correct 46 ms 8540 KB Output is correct
29 Correct 65 ms 12368 KB Output is correct
30 Correct 92 ms 16704 KB Output is correct
31 Correct 137 ms 20144 KB Output is correct
32 Correct 0 ms 360 KB Output is correct
33 Correct 1 ms 360 KB Output is correct
34 Correct 0 ms 352 KB Output is correct
35 Correct 1 ms 452 KB Output is correct
36 Correct 1 ms 356 KB Output is correct
37 Correct 0 ms 352 KB Output is correct
38 Correct 1 ms 360 KB Output is correct
39 Correct 0 ms 360 KB Output is correct
40 Correct 1 ms 360 KB Output is correct
41 Correct 1 ms 352 KB Output is correct
42 Correct 1 ms 352 KB Output is correct
43 Correct 1 ms 444 KB Output is correct
44 Correct 2 ms 604 KB Output is correct
45 Correct 48 ms 9408 KB Output is correct
46 Correct 70 ms 13880 KB Output is correct
47 Correct 75 ms 14032 KB Output is correct
48 Correct 0 ms 360 KB Output is correct
49 Correct 0 ms 360 KB Output is correct
50 Correct 1 ms 360 KB Output is correct
51 Correct 0 ms 360 KB Output is correct
52 Correct 0 ms 360 KB Output is correct
53 Correct 0 ms 360 KB Output is correct
54 Correct 1 ms 360 KB Output is correct
55 Correct 118 ms 20796 KB Output is correct
56 Correct 0 ms 348 KB Output is correct
57 Correct 1 ms 616 KB Output is correct
58 Correct 4 ms 1116 KB Output is correct
59 Correct 3 ms 1120 KB Output is correct
60 Correct 56 ms 10752 KB Output is correct
61 Correct 86 ms 14264 KB Output is correct
62 Correct 105 ms 17468 KB Output is correct
63 Correct 128 ms 21012 KB Output is correct
64 Correct 0 ms 600 KB Output is correct
65 Correct 0 ms 356 KB Output is correct
66 Correct 0 ms 348 KB Output is correct
67 Correct 103 ms 18344 KB Output is correct
68 Correct 99 ms 18144 KB Output is correct
69 Correct 98 ms 18240 KB Output is correct
70 Correct 1 ms 604 KB Output is correct
71 Correct 3 ms 860 KB Output is correct
72 Correct 48 ms 9296 KB Output is correct
73 Correct 75 ms 13372 KB Output is correct
74 Correct 104 ms 18608 KB Output is correct
75 Correct 108 ms 19900 KB Output is correct
76 Correct 102 ms 18164 KB Output is correct
77 Correct 1 ms 608 KB Output is correct
78 Correct 2 ms 1096 KB Output is correct
79 Correct 49 ms 9740 KB Output is correct
80 Correct 82 ms 14360 KB Output is correct
81 Correct 105 ms 18980 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 600 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 344 KB Output is correct
9 Correct 48 ms 8652 KB Output is correct
10 Correct 7 ms 1300 KB Output is correct
11 Correct 24 ms 4820 KB Output is correct
12 Correct 8 ms 1764 KB Output is correct
13 Correct 9 ms 3028 KB Output is correct
14 Correct 1 ms 344 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 51 ms 9160 KB Output is correct
17 Correct 1 ms 344 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 312 ms 44464 KB Output is correct
21 Correct 360 ms 50912 KB Output is correct
22 Correct 341 ms 44424 KB Output is correct
23 Correct 341 ms 43324 KB Output is correct
24 Correct 227 ms 46676 KB Output is correct
25 Correct 303 ms 49968 KB Output is correct
26 Correct 299 ms 49832 KB Output is correct
27 Correct 394 ms 49872 KB Output is correct
28 Correct 364 ms 50192 KB Output is correct
29 Correct 360 ms 50140 KB Output is correct
30 Correct 349 ms 49860 KB Output is correct
31 Correct 0 ms 348 KB Output is correct
32 Incorrect 17 ms 3724 KB Tree @(185725, 20413) appears more than once: for edges on positions 632 and 633
33 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 600 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 344 KB Output is correct
9 Correct 48 ms 8652 KB Output is correct
10 Correct 7 ms 1300 KB Output is correct
11 Correct 24 ms 4820 KB Output is correct
12 Correct 8 ms 1764 KB Output is correct
13 Correct 9 ms 3028 KB Output is correct
14 Correct 1 ms 344 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 51 ms 9160 KB Output is correct
17 Correct 366 ms 50776 KB Output is correct
18 Correct 364 ms 50836 KB Output is correct
19 Correct 333 ms 47548 KB Output is correct
20 Correct 327 ms 41232 KB Output is correct
21 Correct 280 ms 41544 KB Output is correct
22 Correct 0 ms 600 KB Output is correct
23 Incorrect 38 ms 7720 KB Tree @(185191, 20673) appears more than once: for edges on positions 1216 and 1217
24 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 600 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 344 KB Output is correct
9 Correct 48 ms 8652 KB Output is correct
10 Correct 7 ms 1300 KB Output is correct
11 Correct 24 ms 4820 KB Output is correct
12 Correct 8 ms 1764 KB Output is correct
13 Correct 9 ms 3028 KB Output is correct
14 Correct 1 ms 344 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 51 ms 9160 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 1 ms 360 KB Output is correct
19 Correct 1 ms 356 KB Output is correct
20 Correct 1 ms 356 KB Output is correct
21 Correct 1 ms 360 KB Output is correct
22 Correct 0 ms 360 KB Output is correct
23 Correct 133 ms 20300 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 2 ms 356 KB Output is correct
26 Correct 2 ms 604 KB Output is correct
27 Correct 1 ms 616 KB Output is correct
28 Correct 46 ms 8540 KB Output is correct
29 Correct 65 ms 12368 KB Output is correct
30 Correct 92 ms 16704 KB Output is correct
31 Correct 137 ms 20144 KB Output is correct
32 Correct 0 ms 360 KB Output is correct
33 Correct 1 ms 360 KB Output is correct
34 Correct 0 ms 352 KB Output is correct
35 Correct 1 ms 452 KB Output is correct
36 Correct 1 ms 356 KB Output is correct
37 Correct 0 ms 352 KB Output is correct
38 Correct 1 ms 360 KB Output is correct
39 Correct 0 ms 360 KB Output is correct
40 Correct 1 ms 360 KB Output is correct
41 Correct 1 ms 352 KB Output is correct
42 Correct 1 ms 352 KB Output is correct
43 Correct 1 ms 444 KB Output is correct
44 Correct 2 ms 604 KB Output is correct
45 Correct 48 ms 9408 KB Output is correct
46 Correct 70 ms 13880 KB Output is correct
47 Correct 75 ms 14032 KB Output is correct
48 Correct 0 ms 360 KB Output is correct
49 Correct 0 ms 360 KB Output is correct
50 Correct 1 ms 360 KB Output is correct
51 Correct 0 ms 360 KB Output is correct
52 Correct 0 ms 360 KB Output is correct
53 Correct 0 ms 360 KB Output is correct
54 Correct 1 ms 360 KB Output is correct
55 Correct 118 ms 20796 KB Output is correct
56 Correct 0 ms 348 KB Output is correct
57 Correct 1 ms 616 KB Output is correct
58 Correct 4 ms 1116 KB Output is correct
59 Correct 3 ms 1120 KB Output is correct
60 Correct 56 ms 10752 KB Output is correct
61 Correct 86 ms 14264 KB Output is correct
62 Correct 105 ms 17468 KB Output is correct
63 Correct 128 ms 21012 KB Output is correct
64 Correct 0 ms 600 KB Output is correct
65 Correct 0 ms 356 KB Output is correct
66 Correct 0 ms 348 KB Output is correct
67 Correct 103 ms 18344 KB Output is correct
68 Correct 99 ms 18144 KB Output is correct
69 Correct 98 ms 18240 KB Output is correct
70 Correct 1 ms 604 KB Output is correct
71 Correct 3 ms 860 KB Output is correct
72 Correct 48 ms 9296 KB Output is correct
73 Correct 75 ms 13372 KB Output is correct
74 Correct 104 ms 18608 KB Output is correct
75 Correct 108 ms 19900 KB Output is correct
76 Correct 102 ms 18164 KB Output is correct
77 Correct 1 ms 608 KB Output is correct
78 Correct 2 ms 1096 KB Output is correct
79 Correct 49 ms 9740 KB Output is correct
80 Correct 82 ms 14360 KB Output is correct
81 Correct 105 ms 18980 KB Output is correct
82 Correct 1 ms 344 KB Output is correct
83 Correct 1 ms 348 KB Output is correct
84 Correct 0 ms 348 KB Output is correct
85 Correct 312 ms 44464 KB Output is correct
86 Correct 360 ms 50912 KB Output is correct
87 Correct 341 ms 44424 KB Output is correct
88 Correct 341 ms 43324 KB Output is correct
89 Correct 227 ms 46676 KB Output is correct
90 Correct 303 ms 49968 KB Output is correct
91 Correct 299 ms 49832 KB Output is correct
92 Correct 394 ms 49872 KB Output is correct
93 Correct 364 ms 50192 KB Output is correct
94 Correct 360 ms 50140 KB Output is correct
95 Correct 349 ms 49860 KB Output is correct
96 Correct 0 ms 348 KB Output is correct
97 Incorrect 17 ms 3724 KB Tree @(185725, 20413) appears more than once: for edges on positions 632 and 633
98 Halted 0 ms 0 KB -