Submission #1059992

# Submission time Handle Problem Language Result Execution time Memory
1059992 2024-08-15T09:55:35 Z mispertion Swapping Cities (APIO20_swap) C++17
100 / 100
546 ms 69052 KB
#include "swap.h"
#include <bits/stdc++.h>
#include <vector>

#pragma gcc optimize("unroll-loops")
#pragma gcc optimize("o3")
#pragma gcc optimize("Ofast")

using namespace std;

const int N = 1e5 + 5;
const int infi = INT_MAX;

typedef pair<int, int> pii;

#define mp make_pair
#define sz(x) (int)x.size()
#define pb push_back
#define F first
#define S second

struct dsu{
    int pw[N];
    vector<pii> p[N], cnt1[N], cnt2[N], sz[N];
    vector<int> st[N];

    dsu(int n){
        for(int i = 1; i <= n; i++){
            st[i] = {i};
            p[i] = {{-1, i}};
            sz[i] = {{-1, 1}};
            cnt1[i] = {{-1, 0}};
            cnt2[i] = {{-1, 0}};
            pw[i] = 0;
        }
    }
    dsu(){

    }

    int getp(int v, int t){
        int pos = (--upper_bound(p[v].begin(), p[v].end(),mp(t, infi))) - p[v].begin();
        return p[v][pos].S;
    }

    void upd(int v, int nw, int t){
        if(nw == 1)
            cnt1[v].pb({t, cnt1[v].back().S - 1});
        if(nw == 2)
            cnt2[v].pb({t, cnt2[v].back().S - 1});
        if(nw == 0)
            cnt1[v].pb({t, cnt1[v].back().S + 1});
        if(nw == 1)
            cnt2[v].pb({t, cnt2[v].back().S + 1});
    }

    void uni(int a, int b, int t){
        int n1 = pw[a];
        int n2 = pw[b];
        //cout << n1 << ' ' << n2 << '\n';
        pw[a]++;
        pw[b]++;
        a = getp(a, t);
        b = getp(b, t);
        if(a == b){
            upd(a, n1, t);
            upd(a, n2, t);
            return;
        }
        if(sz(st[a]) > sz(st[b]))
            swap(a, b);
        sz[b].pb({t, sz[b].back().S + sz[a].back().S});
        for(auto e : st[a]){
            st[b].pb(e);
            p[e].pb({t, b});
        }
        st[a].clear();
        cnt1[b].pb({t, cnt1[a].back().S + cnt1[b].back().S});
        cnt2[b].pb({t, cnt2[a].back().S + cnt2[b].back().S});
        upd(b, n1, t);
        upd(b, n2, t);
    }
};

int n, m;
vector<pair<int, pair<int, int>>> edges;
dsu ds;

void init(int N, int M, std::vector<int> U, std::vector<int> V, std::vector<int> W) {
    n = N;
    m = M;
    for(int i = 0; i < m; i++){
        U[i]++;
        V[i]++;
        edges.pb({W[i], {V[i], U[i]}});
    }
    sort(edges.begin(), edges.end());
    ds = dsu(n);
    for(int i = 0; i < m; i++){
        ds.uni(edges[i].S.F, edges[i].S.S, i);
        /*cout << "comp: ";
        for(int j = 1; j <= n; j++){
            cout << ds.getp(j, infi) << ' ';
        }
        cout << '\n';
        cout << "cnt1: ";
        for(int i = 1; i <= n; i++){
            cout << ds.cnt1[ds.getp(i, infi)].back().S << ' ';
        }
        cout << '\n';
        cout << "cnt2: ";
        for(int i = 1; i <= n; i++){
            cout << ds.cnt2[ds.getp(i, infi)].back().S << ' ';
        }
        cout << '\n';*/
    }
}

int getMinimumFuelCapacity(int X, int Y) {
    X++;
    Y++;
    int lo = -1, hi = m;
    while(lo + 1 < hi){
        int m = (lo + hi) / 2;
        int x = ds.getp(X, m), y = ds.getp(Y, m);
        if(x != y){
            lo = m;
            continue;
        }
        int c1 = (--upper_bound(ds.cnt1[x].begin(), ds.cnt1[x].end(),mp(m, infi)))->S;
        int c2 = (--upper_bound(ds.cnt2[x].begin(), ds.cnt2[x].end(),mp(m, infi)))->S;
        int sz = (--upper_bound(ds.sz[x].begin(), ds.sz[x].end(),mp(m, infi)))->S;
        if(c1 == 2 && c2 == sz - 2){
            lo = m;
            continue;
        }
        hi = m;
    }
    if(hi == m)
        return -1;
    return edges[hi].F;
}

Compilation message

swap.cpp:5: warning: ignoring '#pragma gcc optimize' [-Wunknown-pragmas]
    5 | #pragma gcc optimize("unroll-loops")
      | 
swap.cpp:6: warning: ignoring '#pragma gcc optimize' [-Wunknown-pragmas]
    6 | #pragma gcc optimize("o3")
      | 
swap.cpp:7: warning: ignoring '#pragma gcc optimize' [-Wunknown-pragmas]
    7 | #pragma gcc optimize("Ofast")
      |
# Verdict Execution time Memory Grader output
1 Correct 10 ms 24668 KB Output is correct
2 Correct 10 ms 24664 KB Output is correct
3 Correct 12 ms 24668 KB Output is correct
4 Correct 17 ms 24628 KB Output is correct
5 Correct 11 ms 24924 KB Output is correct
6 Correct 10 ms 24924 KB Output is correct
7 Correct 17 ms 24924 KB Output is correct
8 Correct 10 ms 24924 KB Output is correct
9 Correct 127 ms 53832 KB Output is correct
10 Correct 146 ms 60100 KB Output is correct
11 Correct 153 ms 59332 KB Output is correct
12 Correct 152 ms 61380 KB Output is correct
13 Correct 98 ms 51808 KB Output is correct
14 Correct 123 ms 53728 KB Output is correct
15 Correct 287 ms 62292 KB Output is correct
16 Correct 269 ms 61192 KB Output is correct
17 Correct 271 ms 63532 KB Output is correct
18 Correct 298 ms 53540 KB Output is correct
19 Correct 107 ms 33740 KB Output is correct
20 Correct 255 ms 63484 KB Output is correct
21 Correct 292 ms 62336 KB Output is correct
22 Correct 277 ms 64816 KB Output is correct
23 Correct 278 ms 55028 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 24668 KB Output is correct
2 Correct 10 ms 24664 KB Output is correct
3 Correct 412 ms 49532 KB Output is correct
4 Correct 467 ms 50592 KB Output is correct
5 Correct 414 ms 50008 KB Output is correct
6 Correct 405 ms 50464 KB Output is correct
7 Correct 418 ms 50244 KB Output is correct
8 Correct 411 ms 49448 KB Output is correct
9 Correct 400 ms 49992 KB Output is correct
10 Correct 421 ms 49276 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 24668 KB Output is correct
2 Correct 10 ms 24664 KB Output is correct
3 Correct 12 ms 24668 KB Output is correct
4 Correct 17 ms 24628 KB Output is correct
5 Correct 11 ms 24924 KB Output is correct
6 Correct 10 ms 24924 KB Output is correct
7 Correct 17 ms 24924 KB Output is correct
8 Correct 10 ms 24924 KB Output is correct
9 Correct 10 ms 24668 KB Output is correct
10 Correct 11 ms 24924 KB Output is correct
11 Correct 15 ms 25180 KB Output is correct
12 Correct 11 ms 24924 KB Output is correct
13 Correct 12 ms 24924 KB Output is correct
14 Correct 12 ms 24828 KB Output is correct
15 Correct 11 ms 24920 KB Output is correct
16 Correct 11 ms 24924 KB Output is correct
17 Correct 11 ms 25176 KB Output is correct
18 Correct 11 ms 24924 KB Output is correct
19 Correct 11 ms 24920 KB Output is correct
20 Correct 11 ms 24804 KB Output is correct
21 Correct 11 ms 24924 KB Output is correct
22 Correct 10 ms 24668 KB Output is correct
23 Correct 11 ms 24924 KB Output is correct
24 Correct 11 ms 24944 KB Output is correct
25 Correct 15 ms 25116 KB Output is correct
26 Correct 11 ms 24924 KB Output is correct
27 Correct 11 ms 24924 KB Output is correct
28 Correct 11 ms 24920 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 24668 KB Output is correct
2 Correct 10 ms 24668 KB Output is correct
3 Correct 10 ms 24664 KB Output is correct
4 Correct 12 ms 24668 KB Output is correct
5 Correct 17 ms 24628 KB Output is correct
6 Correct 11 ms 24924 KB Output is correct
7 Correct 10 ms 24924 KB Output is correct
8 Correct 17 ms 24924 KB Output is correct
9 Correct 10 ms 24924 KB Output is correct
10 Correct 127 ms 53832 KB Output is correct
11 Correct 146 ms 60100 KB Output is correct
12 Correct 153 ms 59332 KB Output is correct
13 Correct 152 ms 61380 KB Output is correct
14 Correct 98 ms 51808 KB Output is correct
15 Correct 11 ms 24924 KB Output is correct
16 Correct 15 ms 25180 KB Output is correct
17 Correct 11 ms 24924 KB Output is correct
18 Correct 12 ms 24924 KB Output is correct
19 Correct 12 ms 24828 KB Output is correct
20 Correct 11 ms 24920 KB Output is correct
21 Correct 11 ms 24924 KB Output is correct
22 Correct 11 ms 25176 KB Output is correct
23 Correct 11 ms 24924 KB Output is correct
24 Correct 11 ms 24920 KB Output is correct
25 Correct 11 ms 24804 KB Output is correct
26 Correct 11 ms 24924 KB Output is correct
27 Correct 10 ms 24668 KB Output is correct
28 Correct 11 ms 24924 KB Output is correct
29 Correct 11 ms 24944 KB Output is correct
30 Correct 15 ms 25116 KB Output is correct
31 Correct 11 ms 24924 KB Output is correct
32 Correct 11 ms 24924 KB Output is correct
33 Correct 11 ms 24920 KB Output is correct
34 Correct 22 ms 29208 KB Output is correct
35 Correct 153 ms 61384 KB Output is correct
36 Correct 151 ms 61308 KB Output is correct
37 Correct 162 ms 60612 KB Output is correct
38 Correct 188 ms 59256 KB Output is correct
39 Correct 138 ms 58820 KB Output is correct
40 Correct 122 ms 55752 KB Output is correct
41 Correct 185 ms 61652 KB Output is correct
42 Correct 145 ms 61880 KB Output is correct
43 Correct 94 ms 51228 KB Output is correct
44 Correct 137 ms 58500 KB Output is correct
45 Correct 114 ms 52160 KB Output is correct
46 Correct 153 ms 61128 KB Output is correct
47 Correct 142 ms 57028 KB Output is correct
48 Correct 114 ms 54468 KB Output is correct
49 Correct 52 ms 32800 KB Output is correct
50 Correct 47 ms 30920 KB Output is correct
51 Correct 90 ms 46980 KB Output is correct
52 Correct 196 ms 63932 KB Output is correct
53 Correct 175 ms 60096 KB Output is correct
54 Correct 176 ms 67264 KB Output is correct
55 Correct 96 ms 51392 KB Output is correct
56 Correct 143 ms 59536 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 24668 KB Output is correct
2 Correct 10 ms 24664 KB Output is correct
3 Correct 12 ms 24668 KB Output is correct
4 Correct 17 ms 24628 KB Output is correct
5 Correct 11 ms 24924 KB Output is correct
6 Correct 10 ms 24924 KB Output is correct
7 Correct 17 ms 24924 KB Output is correct
8 Correct 10 ms 24924 KB Output is correct
9 Correct 127 ms 53832 KB Output is correct
10 Correct 146 ms 60100 KB Output is correct
11 Correct 153 ms 59332 KB Output is correct
12 Correct 152 ms 61380 KB Output is correct
13 Correct 98 ms 51808 KB Output is correct
14 Correct 123 ms 53728 KB Output is correct
15 Correct 287 ms 62292 KB Output is correct
16 Correct 269 ms 61192 KB Output is correct
17 Correct 271 ms 63532 KB Output is correct
18 Correct 298 ms 53540 KB Output is correct
19 Correct 412 ms 49532 KB Output is correct
20 Correct 467 ms 50592 KB Output is correct
21 Correct 414 ms 50008 KB Output is correct
22 Correct 405 ms 50464 KB Output is correct
23 Correct 418 ms 50244 KB Output is correct
24 Correct 411 ms 49448 KB Output is correct
25 Correct 400 ms 49992 KB Output is correct
26 Correct 421 ms 49276 KB Output is correct
27 Correct 11 ms 24924 KB Output is correct
28 Correct 15 ms 25180 KB Output is correct
29 Correct 11 ms 24924 KB Output is correct
30 Correct 12 ms 24924 KB Output is correct
31 Correct 12 ms 24828 KB Output is correct
32 Correct 11 ms 24920 KB Output is correct
33 Correct 11 ms 24924 KB Output is correct
34 Correct 11 ms 25176 KB Output is correct
35 Correct 11 ms 24924 KB Output is correct
36 Correct 22 ms 29208 KB Output is correct
37 Correct 153 ms 61384 KB Output is correct
38 Correct 151 ms 61308 KB Output is correct
39 Correct 162 ms 60612 KB Output is correct
40 Correct 188 ms 59256 KB Output is correct
41 Correct 138 ms 58820 KB Output is correct
42 Correct 122 ms 55752 KB Output is correct
43 Correct 185 ms 61652 KB Output is correct
44 Correct 145 ms 61880 KB Output is correct
45 Correct 94 ms 51228 KB Output is correct
46 Correct 137 ms 58500 KB Output is correct
47 Correct 32 ms 29204 KB Output is correct
48 Correct 261 ms 64088 KB Output is correct
49 Correct 268 ms 63348 KB Output is correct
50 Correct 268 ms 63272 KB Output is correct
51 Correct 272 ms 62524 KB Output is correct
52 Correct 253 ms 59764 KB Output is correct
53 Correct 227 ms 50876 KB Output is correct
54 Correct 269 ms 64524 KB Output is correct
55 Correct 269 ms 64812 KB Output is correct
56 Correct 356 ms 54308 KB Output is correct
57 Correct 306 ms 61456 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 24668 KB Output is correct
2 Correct 10 ms 24668 KB Output is correct
3 Correct 10 ms 24664 KB Output is correct
4 Correct 12 ms 24668 KB Output is correct
5 Correct 17 ms 24628 KB Output is correct
6 Correct 11 ms 24924 KB Output is correct
7 Correct 10 ms 24924 KB Output is correct
8 Correct 17 ms 24924 KB Output is correct
9 Correct 10 ms 24924 KB Output is correct
10 Correct 127 ms 53832 KB Output is correct
11 Correct 146 ms 60100 KB Output is correct
12 Correct 153 ms 59332 KB Output is correct
13 Correct 152 ms 61380 KB Output is correct
14 Correct 98 ms 51808 KB Output is correct
15 Correct 123 ms 53728 KB Output is correct
16 Correct 287 ms 62292 KB Output is correct
17 Correct 269 ms 61192 KB Output is correct
18 Correct 271 ms 63532 KB Output is correct
19 Correct 298 ms 53540 KB Output is correct
20 Correct 412 ms 49532 KB Output is correct
21 Correct 467 ms 50592 KB Output is correct
22 Correct 414 ms 50008 KB Output is correct
23 Correct 405 ms 50464 KB Output is correct
24 Correct 418 ms 50244 KB Output is correct
25 Correct 411 ms 49448 KB Output is correct
26 Correct 400 ms 49992 KB Output is correct
27 Correct 421 ms 49276 KB Output is correct
28 Correct 11 ms 24924 KB Output is correct
29 Correct 15 ms 25180 KB Output is correct
30 Correct 11 ms 24924 KB Output is correct
31 Correct 12 ms 24924 KB Output is correct
32 Correct 12 ms 24828 KB Output is correct
33 Correct 11 ms 24920 KB Output is correct
34 Correct 11 ms 24924 KB Output is correct
35 Correct 11 ms 25176 KB Output is correct
36 Correct 11 ms 24924 KB Output is correct
37 Correct 22 ms 29208 KB Output is correct
38 Correct 153 ms 61384 KB Output is correct
39 Correct 151 ms 61308 KB Output is correct
40 Correct 162 ms 60612 KB Output is correct
41 Correct 188 ms 59256 KB Output is correct
42 Correct 138 ms 58820 KB Output is correct
43 Correct 122 ms 55752 KB Output is correct
44 Correct 185 ms 61652 KB Output is correct
45 Correct 145 ms 61880 KB Output is correct
46 Correct 94 ms 51228 KB Output is correct
47 Correct 137 ms 58500 KB Output is correct
48 Correct 32 ms 29204 KB Output is correct
49 Correct 261 ms 64088 KB Output is correct
50 Correct 268 ms 63348 KB Output is correct
51 Correct 268 ms 63272 KB Output is correct
52 Correct 272 ms 62524 KB Output is correct
53 Correct 253 ms 59764 KB Output is correct
54 Correct 227 ms 50876 KB Output is correct
55 Correct 269 ms 64524 KB Output is correct
56 Correct 269 ms 64812 KB Output is correct
57 Correct 356 ms 54308 KB Output is correct
58 Correct 306 ms 61456 KB Output is correct
59 Correct 107 ms 33740 KB Output is correct
60 Correct 255 ms 63484 KB Output is correct
61 Correct 292 ms 62336 KB Output is correct
62 Correct 277 ms 64816 KB Output is correct
63 Correct 278 ms 55028 KB Output is correct
64 Correct 11 ms 24920 KB Output is correct
65 Correct 11 ms 24804 KB Output is correct
66 Correct 11 ms 24924 KB Output is correct
67 Correct 10 ms 24668 KB Output is correct
68 Correct 11 ms 24924 KB Output is correct
69 Correct 11 ms 24944 KB Output is correct
70 Correct 15 ms 25116 KB Output is correct
71 Correct 11 ms 24924 KB Output is correct
72 Correct 11 ms 24924 KB Output is correct
73 Correct 11 ms 24920 KB Output is correct
74 Correct 114 ms 52160 KB Output is correct
75 Correct 153 ms 61128 KB Output is correct
76 Correct 142 ms 57028 KB Output is correct
77 Correct 114 ms 54468 KB Output is correct
78 Correct 52 ms 32800 KB Output is correct
79 Correct 47 ms 30920 KB Output is correct
80 Correct 90 ms 46980 KB Output is correct
81 Correct 196 ms 63932 KB Output is correct
82 Correct 175 ms 60096 KB Output is correct
83 Correct 176 ms 67264 KB Output is correct
84 Correct 96 ms 51392 KB Output is correct
85 Correct 143 ms 59536 KB Output is correct
86 Correct 126 ms 35272 KB Output is correct
87 Correct 288 ms 63020 KB Output is correct
88 Correct 285 ms 63016 KB Output is correct
89 Correct 496 ms 55272 KB Output is correct
90 Correct 286 ms 33048 KB Output is correct
91 Correct 362 ms 36064 KB Output is correct
92 Correct 501 ms 48948 KB Output is correct
93 Correct 397 ms 64772 KB Output is correct
94 Correct 490 ms 61852 KB Output is correct
95 Correct 309 ms 69052 KB Output is correct
96 Correct 367 ms 54316 KB Output is correct
97 Correct 546 ms 58752 KB Output is correct