Submission #828890

# Submission time Handle Problem Language Result Execution time Memory
828890 2023-08-17T18:36:16 Z fatemetmhr Fountain Parks (IOI21_parks) C++17
55 / 100
850 ms 104156 KB
// Be Name Khoda //

//#pragma GCC optimize ("O3")
//#pragma GCC target("avx2")
//#pragma GCC optimize("unroll-loops,O3")

#include "parks.h"
#include <bits/stdc++.h>

using namespace std;


mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());


typedef long long ll;

#define debug(x)  cerr << "(" << (#x) << "): " << (x) << endl;
#define all(x)    x.begin(), x.end()
#define MAX(x, y) ((x) > (y) ? (x) : (y))
#define MIN(x, y) ((x) < (y) ? (x) : (y))
#define fi        first
#define se        second
#define pb        push_back
#define mp        make_pair

const int maxn5 = 1e6 + 10;


int n, q[maxn5], cnt[maxn5], ti, par[maxn5];
bool mark[maxn5];
vector <int> ret[2], ed[2], edg[maxn5];
vector <pair<int, int>> ver, toadd;
pair<pair <int, int>, pair<int, int>> adj[maxn5];
map <pair<int, int>, int> av, num;

bool bad(int i, int j, int x, int y){
    if(i == x){
        return (num[mp(i - 1, (j + y) / 2)] > 0 || num[mp(i + 1, (j + y) / 2)] > 0);
    }
    return (num[mp((i + x) / 2, j - 1)] > 0 || num[mp((i + x) / 2, j + 1)] > 0);
}

void dfs0(int v, int x, int y){
    par[v] = ti;
    av.erase(mp(x, y));
    if(av.find(mp(x - 2, y)) != av.end() && !bad(x, y, x - 2, y)){
        int u = av[mp(x - 2, y)];
        ed[0].pb(v);
        ed[1].pb(u);
        num[mp(x - 1, y - 1)]++;
        num[mp(x - 1, y + 1)]++;
        dfs0(u, x - 2, y);
    }
    else if(av.find(mp(x - 2, y)) != av.end())
        toadd.pb(mp(v, av[mp(x - 2, y)]));
    if(av.find(mp(x + 2, y)) != av.end() && !bad(x, y, x + 2, y)){
        int u = av[mp(x + 2, y)];
        ed[0].pb(v);
        ed[1].pb(u);
        num[mp(x + 1, y - 1)]++;
        num[mp(x + 1, y + 1)]++;
        dfs0(u, x + 2, y);
    }
    else if(av.find(mp(x + 2, y)) != av.end())
        toadd.pb(mp(v, av[mp(x + 2, y)]));
    if(av.find(mp(x, y - 2)) != av.end() && !bad(x, y, x, y - 2)){
        int u = av[mp(x, y - 2)];
        ed[0].pb(v);
        ed[1].pb(u);
        num[mp(x - 1, y - 1)]++;
        num[mp(x + 1, y - 1)]++;
        dfs0(u, x, y - 2);
    }
    else if(av.find(mp(x, y - 2)) != av.end())
        toadd.pb(mp(v, av[mp(x, y - 2)]));
    if(av.find(mp(x, y + 2)) != av.end() && !bad(x, y, x, y + 2)){
        int u = av[mp(x, y + 2)];
        ed[0].pb(v);
        ed[1].pb(u);
        num[mp(x - 1, y + 1)]++;
        num[mp(x + 1, y + 1)]++;
        dfs0(u, x, y + 2);
    }
    else if(av.find(mp(x, y + 2)) != av.end())
        toadd.pb(mp(v, av[mp(x, y + 2)]));
}

void dfs2(int v){
    mark[v] = true;
    for(auto u : edg[v]) if(!mark[u]){
        mark[u] = true;
        //debug(v);
        //debug(u);
        ret[0][v] = ver[u - (n - 1)].fi;
        ret[1][v] = ver[u - (n - 1)].se;
        for(auto z : edg[u]) if(!mark[z])
            dfs2(z);
        return;
    }
}

int get_par(int a){return par[a] == -1 ? a : par[a] = get_par(par[a]);}


int construct_roads(std::vector<int> x, std::vector<int> y) {
    n = x.size();
    for(int i = 0; i < n; i++){
        av[mp(x[i], y[i])] = i;
    }
    for(int i = 0; i < n; i++) if(av.find(mp(x[i], y[i])) != av.end()){
        ti = i;
        dfs0(i, x[i], y[i]);
        par[i] = -1;
    }
    shuffle(all(toadd), rng);
    for(auto [u, v] : toadd){
        int a = get_par(u);
        int b = get_par(v);
        if(a == b)
            continue;
        ed[0].pb(u);
        ed[1].pb(v);
        par[a] = b;
    }
    if(int(ed[0].size()) < n - 1)
        return 0;

    for(int i = 0; i < n - 1; i++){
        if(x[ed[0][i]] == x[ed[1][i]]){
            adj[i].fi = mp(x[ed[0][i]] - 1, (y[ed[0][i]] + y[ed[1][i]]) / 2);
            adj[i].se = mp(x[ed[0][i]] + 1, (y[ed[0][i]] + y[ed[1][i]]) / 2);
        }
        else{
            adj[i].fi = mp((x[ed[0][i]] + x[ed[1][i]]) / 2, y[ed[0][i]] - 1);
            adj[i].se = mp((x[ed[0][i]] + x[ed[1][i]]) / 2, y[ed[0][i]] + 1);
        }
        ver.pb(adj[i].fi);
        ver.pb(adj[i].se);
    }
    sort(all(ver));
    ver.resize(unique(all(ver)) - ver.begin());
    for(int i = 0; i < n - 1; i++){
        int pt = lower_bound(all(ver), adj[i].fi) - ver.begin();
        edg[i].pb(pt + n - 1);
        pt = lower_bound(all(ver), adj[i].se) - ver.begin();
        edg[i].pb(pt + n - 1);
        edg[edg[i][0]].pb(i);
        edg[edg[i][1]].pb(i);
        cnt[edg[i][0]]++;
        cnt[edg[i][1]]++;
    }
    /*
    for(int i = 0; i < int(ver.size()) + n - 1; i++){
        //debug(i);
        for(auto u : edg[i])
            cout << u << ' ';
        cout << endl;
    }
    //*/
    int l = 0, r = 0;
    for(int i = 0; i < int(ver.size()); i++) if(edg[i + n - 1].size() == 1){
        q[r++] = i + n - 1;
        mark[i + n - 1] = true;
    }
    ret[0].resize(n - 1);
    ret[1].resize(n - 1);
    //debug(r);
    while(l < r){
        int v = q[l++];
        for(auto u : edg[v]) if(!mark[u]){
            mark[u] = true;
            //debug(v);
            //debug(u);
            for(auto z : edg[u]){
                cnt[z]--;
                if(!mark[z] && cnt[z] == 1){
                    q[r++] = z;
                    mark[z] = true;
                }
            }
            ret[0][u] = ver[v - (n - 1)].fi;
            ret[1][u] = ver[v - (n - 1)].se;
        }
    }
    //return 0;
    //cout << ed[0].size() << ' ' << ed[1].size() << ' ' << ret[0].size() << ' ' << ret[1].size() << endl;
    for(int i = 0; i < n - 1; i++) if(!mark[i])
        dfs2(i);
    build(ed[0], ed[1], ret[0], ret[1]);
    return 1;
}


















# Verdict Execution time Memory Grader output
1 Correct 13 ms 23764 KB Output is correct
2 Correct 12 ms 23764 KB Output is correct
3 Correct 12 ms 23764 KB Output is correct
4 Correct 11 ms 23732 KB Output is correct
5 Correct 11 ms 23772 KB Output is correct
6 Correct 12 ms 23784 KB Output is correct
7 Correct 11 ms 23764 KB Output is correct
8 Correct 11 ms 23720 KB Output is correct
9 Correct 235 ms 70256 KB Output is correct
10 Correct 28 ms 28580 KB Output is correct
11 Correct 119 ms 49260 KB Output is correct
12 Correct 37 ms 30988 KB Output is correct
13 Correct 78 ms 34320 KB Output is correct
14 Correct 12 ms 23912 KB Output is correct
15 Correct 13 ms 24164 KB Output is correct
16 Correct 205 ms 64992 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 23764 KB Output is correct
2 Correct 12 ms 23764 KB Output is correct
3 Correct 12 ms 23764 KB Output is correct
4 Correct 11 ms 23732 KB Output is correct
5 Correct 11 ms 23772 KB Output is correct
6 Correct 12 ms 23784 KB Output is correct
7 Correct 11 ms 23764 KB Output is correct
8 Correct 11 ms 23720 KB Output is correct
9 Correct 235 ms 70256 KB Output is correct
10 Correct 28 ms 28580 KB Output is correct
11 Correct 119 ms 49260 KB Output is correct
12 Correct 37 ms 30988 KB Output is correct
13 Correct 78 ms 34320 KB Output is correct
14 Correct 12 ms 23912 KB Output is correct
15 Correct 13 ms 24164 KB Output is correct
16 Correct 205 ms 64992 KB Output is correct
17 Correct 12 ms 23764 KB Output is correct
18 Correct 13 ms 23756 KB Output is correct
19 Correct 15 ms 23732 KB Output is correct
20 Correct 12 ms 23764 KB Output is correct
21 Correct 12 ms 23764 KB Output is correct
22 Correct 12 ms 23736 KB Output is correct
23 Correct 832 ms 78200 KB Output is correct
24 Correct 13 ms 23764 KB Output is correct
25 Correct 14 ms 24148 KB Output is correct
26 Correct 16 ms 24188 KB Output is correct
27 Correct 17 ms 24276 KB Output is correct
28 Correct 258 ms 45392 KB Output is correct
29 Correct 422 ms 56328 KB Output is correct
30 Correct 609 ms 67280 KB Output is correct
31 Correct 821 ms 78248 KB Output is correct
32 Correct 12 ms 23764 KB Output is correct
33 Correct 14 ms 23728 KB Output is correct
34 Correct 11 ms 23764 KB Output is correct
35 Correct 12 ms 23764 KB Output is correct
36 Correct 11 ms 23764 KB Output is correct
37 Correct 11 ms 23764 KB Output is correct
38 Correct 11 ms 23764 KB Output is correct
39 Correct 11 ms 23764 KB Output is correct
40 Correct 11 ms 23764 KB Output is correct
41 Correct 11 ms 23764 KB Output is correct
42 Correct 11 ms 23764 KB Output is correct
43 Correct 14 ms 24052 KB Output is correct
44 Correct 17 ms 24188 KB Output is correct
45 Correct 299 ms 51948 KB Output is correct
46 Correct 504 ms 64720 KB Output is correct
47 Correct 488 ms 64700 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 23764 KB Output is correct
2 Correct 12 ms 23764 KB Output is correct
3 Correct 12 ms 23764 KB Output is correct
4 Correct 11 ms 23732 KB Output is correct
5 Correct 11 ms 23772 KB Output is correct
6 Correct 12 ms 23784 KB Output is correct
7 Correct 11 ms 23764 KB Output is correct
8 Correct 11 ms 23720 KB Output is correct
9 Correct 235 ms 70256 KB Output is correct
10 Correct 28 ms 28580 KB Output is correct
11 Correct 119 ms 49260 KB Output is correct
12 Correct 37 ms 30988 KB Output is correct
13 Correct 78 ms 34320 KB Output is correct
14 Correct 12 ms 23912 KB Output is correct
15 Correct 13 ms 24164 KB Output is correct
16 Correct 205 ms 64992 KB Output is correct
17 Correct 12 ms 23764 KB Output is correct
18 Correct 13 ms 23756 KB Output is correct
19 Correct 15 ms 23732 KB Output is correct
20 Correct 12 ms 23764 KB Output is correct
21 Correct 12 ms 23764 KB Output is correct
22 Correct 12 ms 23736 KB Output is correct
23 Correct 832 ms 78200 KB Output is correct
24 Correct 13 ms 23764 KB Output is correct
25 Correct 14 ms 24148 KB Output is correct
26 Correct 16 ms 24188 KB Output is correct
27 Correct 17 ms 24276 KB Output is correct
28 Correct 258 ms 45392 KB Output is correct
29 Correct 422 ms 56328 KB Output is correct
30 Correct 609 ms 67280 KB Output is correct
31 Correct 821 ms 78248 KB Output is correct
32 Correct 12 ms 23764 KB Output is correct
33 Correct 14 ms 23728 KB Output is correct
34 Correct 11 ms 23764 KB Output is correct
35 Correct 12 ms 23764 KB Output is correct
36 Correct 11 ms 23764 KB Output is correct
37 Correct 11 ms 23764 KB Output is correct
38 Correct 11 ms 23764 KB Output is correct
39 Correct 11 ms 23764 KB Output is correct
40 Correct 11 ms 23764 KB Output is correct
41 Correct 11 ms 23764 KB Output is correct
42 Correct 11 ms 23764 KB Output is correct
43 Correct 14 ms 24052 KB Output is correct
44 Correct 17 ms 24188 KB Output is correct
45 Correct 299 ms 51948 KB Output is correct
46 Correct 504 ms 64720 KB Output is correct
47 Correct 488 ms 64700 KB Output is correct
48 Correct 11 ms 23764 KB Output is correct
49 Correct 13 ms 23892 KB Output is correct
50 Correct 11 ms 23764 KB Output is correct
51 Correct 11 ms 23812 KB Output is correct
52 Correct 11 ms 23716 KB Output is correct
53 Correct 12 ms 23764 KB Output is correct
54 Correct 11 ms 23704 KB Output is correct
55 Incorrect 850 ms 76888 KB a[86009] = 0 is not an odd integer
56 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 13 ms 23764 KB Output is correct
2 Correct 12 ms 23764 KB Output is correct
3 Correct 12 ms 23764 KB Output is correct
4 Correct 11 ms 23732 KB Output is correct
5 Correct 11 ms 23772 KB Output is correct
6 Correct 12 ms 23784 KB Output is correct
7 Correct 11 ms 23764 KB Output is correct
8 Correct 11 ms 23720 KB Output is correct
9 Correct 235 ms 70256 KB Output is correct
10 Correct 28 ms 28580 KB Output is correct
11 Correct 119 ms 49260 KB Output is correct
12 Correct 37 ms 30988 KB Output is correct
13 Correct 78 ms 34320 KB Output is correct
14 Correct 12 ms 23912 KB Output is correct
15 Correct 13 ms 24164 KB Output is correct
16 Correct 205 ms 64992 KB Output is correct
17 Correct 11 ms 23764 KB Output is correct
18 Correct 12 ms 23764 KB Output is correct
19 Correct 11 ms 23748 KB Output is correct
20 Correct 696 ms 74776 KB Output is correct
21 Correct 692 ms 74520 KB Output is correct
22 Correct 728 ms 74748 KB Output is correct
23 Correct 356 ms 89560 KB Output is correct
24 Correct 358 ms 40272 KB Output is correct
25 Correct 479 ms 54272 KB Output is correct
26 Correct 255 ms 54316 KB Output is correct
27 Correct 423 ms 93560 KB Output is correct
28 Correct 456 ms 93552 KB Output is correct
29 Correct 766 ms 93572 KB Output is correct
30 Correct 757 ms 93528 KB Output is correct
31 Correct 12 ms 23764 KB Output is correct
32 Correct 51 ms 27788 KB Output is correct
33 Correct 145 ms 31936 KB Output is correct
34 Correct 704 ms 74808 KB Output is correct
35 Correct 28 ms 25044 KB Output is correct
36 Correct 106 ms 29696 KB Output is correct
37 Correct 252 ms 35240 KB Output is correct
38 Correct 286 ms 45288 KB Output is correct
39 Correct 381 ms 53260 KB Output is correct
40 Correct 543 ms 61416 KB Output is correct
41 Correct 719 ms 69632 KB Output is correct
42 Correct 813 ms 77744 KB Output is correct
43 Correct 12 ms 23764 KB Output is correct
44 Correct 13 ms 23764 KB Output is correct
45 Correct 12 ms 23764 KB Output is correct
46 Correct 12 ms 23748 KB Output is correct
47 Correct 11 ms 23704 KB Output is correct
48 Correct 12 ms 23764 KB Output is correct
49 Correct 11 ms 23716 KB Output is correct
50 Correct 11 ms 23760 KB Output is correct
51 Correct 13 ms 23764 KB Output is correct
52 Correct 12 ms 23716 KB Output is correct
53 Correct 12 ms 23764 KB Output is correct
54 Correct 14 ms 24020 KB Output is correct
55 Correct 16 ms 24208 KB Output is correct
56 Correct 299 ms 51936 KB Output is correct
57 Correct 476 ms 64748 KB Output is correct
58 Correct 505 ms 64708 KB Output is correct
59 Correct 12 ms 23764 KB Output is correct
60 Correct 12 ms 23764 KB Output is correct
61 Correct 12 ms 23716 KB Output is correct
62 Correct 483 ms 102424 KB Output is correct
63 Correct 444 ms 104156 KB Output is correct
64 Correct 480 ms 99004 KB Output is correct
65 Correct 17 ms 24276 KB Output is correct
66 Correct 23 ms 24768 KB Output is correct
67 Correct 315 ms 51420 KB Output is correct
68 Correct 532 ms 65400 KB Output is correct
69 Correct 753 ms 79412 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 23764 KB Output is correct
2 Correct 12 ms 23764 KB Output is correct
3 Correct 12 ms 23764 KB Output is correct
4 Correct 11 ms 23732 KB Output is correct
5 Correct 11 ms 23772 KB Output is correct
6 Correct 12 ms 23784 KB Output is correct
7 Correct 11 ms 23764 KB Output is correct
8 Correct 11 ms 23720 KB Output is correct
9 Correct 235 ms 70256 KB Output is correct
10 Correct 28 ms 28580 KB Output is correct
11 Correct 119 ms 49260 KB Output is correct
12 Correct 37 ms 30988 KB Output is correct
13 Correct 78 ms 34320 KB Output is correct
14 Correct 12 ms 23912 KB Output is correct
15 Correct 13 ms 24164 KB Output is correct
16 Correct 205 ms 64992 KB Output is correct
17 Correct 436 ms 99576 KB Output is correct
18 Correct 427 ms 99812 KB Output is correct
19 Correct 694 ms 74588 KB Output is correct
20 Correct 720 ms 79576 KB Output is correct
21 Correct 567 ms 81316 KB Output is correct
22 Correct 12 ms 23716 KB Output is correct
23 Correct 97 ms 32708 KB Output is correct
24 Correct 50 ms 26376 KB Output is correct
25 Correct 152 ms 32792 KB Output is correct
26 Correct 291 ms 38776 KB Output is correct
27 Correct 345 ms 51884 KB Output is correct
28 Correct 444 ms 59112 KB Output is correct
29 Correct 642 ms 66260 KB Output is correct
30 Correct 696 ms 73112 KB Output is correct
31 Correct 794 ms 80368 KB Output is correct
32 Correct 505 ms 95736 KB Output is correct
33 Correct 453 ms 101756 KB Output is correct
34 Correct 18 ms 24404 KB Output is correct
35 Correct 27 ms 24912 KB Output is correct
36 Correct 317 ms 52156 KB Output is correct
37 Correct 554 ms 66752 KB Output is correct
38 Correct 744 ms 81080 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 23764 KB Output is correct
2 Correct 12 ms 23764 KB Output is correct
3 Correct 12 ms 23764 KB Output is correct
4 Correct 11 ms 23732 KB Output is correct
5 Correct 11 ms 23772 KB Output is correct
6 Correct 12 ms 23784 KB Output is correct
7 Correct 11 ms 23764 KB Output is correct
8 Correct 11 ms 23720 KB Output is correct
9 Correct 235 ms 70256 KB Output is correct
10 Correct 28 ms 28580 KB Output is correct
11 Correct 119 ms 49260 KB Output is correct
12 Correct 37 ms 30988 KB Output is correct
13 Correct 78 ms 34320 KB Output is correct
14 Correct 12 ms 23912 KB Output is correct
15 Correct 13 ms 24164 KB Output is correct
16 Correct 205 ms 64992 KB Output is correct
17 Correct 12 ms 23764 KB Output is correct
18 Correct 13 ms 23756 KB Output is correct
19 Correct 15 ms 23732 KB Output is correct
20 Correct 12 ms 23764 KB Output is correct
21 Correct 12 ms 23764 KB Output is correct
22 Correct 12 ms 23736 KB Output is correct
23 Correct 832 ms 78200 KB Output is correct
24 Correct 13 ms 23764 KB Output is correct
25 Correct 14 ms 24148 KB Output is correct
26 Correct 16 ms 24188 KB Output is correct
27 Correct 17 ms 24276 KB Output is correct
28 Correct 258 ms 45392 KB Output is correct
29 Correct 422 ms 56328 KB Output is correct
30 Correct 609 ms 67280 KB Output is correct
31 Correct 821 ms 78248 KB Output is correct
32 Correct 12 ms 23764 KB Output is correct
33 Correct 14 ms 23728 KB Output is correct
34 Correct 11 ms 23764 KB Output is correct
35 Correct 12 ms 23764 KB Output is correct
36 Correct 11 ms 23764 KB Output is correct
37 Correct 11 ms 23764 KB Output is correct
38 Correct 11 ms 23764 KB Output is correct
39 Correct 11 ms 23764 KB Output is correct
40 Correct 11 ms 23764 KB Output is correct
41 Correct 11 ms 23764 KB Output is correct
42 Correct 11 ms 23764 KB Output is correct
43 Correct 14 ms 24052 KB Output is correct
44 Correct 17 ms 24188 KB Output is correct
45 Correct 299 ms 51948 KB Output is correct
46 Correct 504 ms 64720 KB Output is correct
47 Correct 488 ms 64700 KB Output is correct
48 Correct 11 ms 23764 KB Output is correct
49 Correct 13 ms 23892 KB Output is correct
50 Correct 11 ms 23764 KB Output is correct
51 Correct 11 ms 23812 KB Output is correct
52 Correct 11 ms 23716 KB Output is correct
53 Correct 12 ms 23764 KB Output is correct
54 Correct 11 ms 23704 KB Output is correct
55 Incorrect 850 ms 76888 KB a[86009] = 0 is not an odd integer
56 Halted 0 ms 0 KB -