Submission #1059802

# Submission time Handle Problem Language Result Execution time Memory
1059802 2024-08-15T08:20:12 Z mispertion Swapping Cities (APIO20_swap) C++17
50 / 100
2000 ms 54424 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 pb push_back
#define F first
#define S second

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

    dsu(int n){
        for(int i = 1; i <= n; 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 pr = (--upper_bound(p[v].begin(), p[v].end(),mp(t, infi)))->S;
        if(pr == v)
            return v;
        return getp(pr, t);
    }

    int getpch(int v, int t){
        int pr = p[v].back().S;
        if(pr == v)
            return v;
        p[v].pb({t, getpch(pr, t)});
        return p[v].back().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 = getpch(a, t);
        b = getpch(b, t);
        if(a == b){
            upd(a, n1, t);
            upd(a, n2, t);
            return;
        }
        if(sz[a] > sz[b])
            swap(a, b);
        p[a].pb({t, b});
        sz[b].pb({t, sz[a].back().S + sz[b].back().S});
        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 19804 KB Output is correct
2 Correct 10 ms 19832 KB Output is correct
3 Correct 10 ms 19800 KB Output is correct
4 Correct 10 ms 20132 KB Output is correct
5 Correct 10 ms 20208 KB Output is correct
6 Correct 10 ms 20060 KB Output is correct
7 Correct 10 ms 20168 KB Output is correct
8 Correct 10 ms 20060 KB Output is correct
9 Correct 90 ms 40296 KB Output is correct
10 Correct 111 ms 44716 KB Output is correct
11 Correct 108 ms 44488 KB Output is correct
12 Correct 116 ms 45948 KB Output is correct
13 Correct 86 ms 45768 KB Output is correct
14 Correct 130 ms 40408 KB Output is correct
15 Correct 456 ms 46876 KB Output is correct
16 Correct 504 ms 46164 KB Output is correct
17 Correct 487 ms 47664 KB Output is correct
18 Correct 318 ms 47332 KB Output is correct
19 Correct 241 ms 27408 KB Output is correct
20 Correct 463 ms 48160 KB Output is correct
21 Correct 462 ms 47232 KB Output is correct
22 Correct 483 ms 49196 KB Output is correct
23 Correct 320 ms 48676 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 19804 KB Output is correct
2 Correct 10 ms 19832 KB Output is correct
3 Correct 427 ms 41648 KB Output is correct
4 Correct 418 ms 42504 KB Output is correct
5 Correct 444 ms 41944 KB Output is correct
6 Correct 408 ms 42416 KB Output is correct
7 Correct 437 ms 42304 KB Output is correct
8 Correct 413 ms 41588 KB Output is correct
9 Correct 425 ms 42044 KB Output is correct
10 Correct 427 ms 41344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 19804 KB Output is correct
2 Correct 10 ms 19832 KB Output is correct
3 Correct 10 ms 19800 KB Output is correct
4 Correct 10 ms 20132 KB Output is correct
5 Correct 10 ms 20208 KB Output is correct
6 Correct 10 ms 20060 KB Output is correct
7 Correct 10 ms 20168 KB Output is correct
8 Correct 10 ms 20060 KB Output is correct
9 Correct 11 ms 19800 KB Output is correct
10 Correct 10 ms 20092 KB Output is correct
11 Correct 10 ms 20060 KB Output is correct
12 Correct 12 ms 20112 KB Output is correct
13 Correct 11 ms 20056 KB Output is correct
14 Correct 10 ms 20212 KB Output is correct
15 Correct 11 ms 20288 KB Output is correct
16 Correct 11 ms 20060 KB Output is correct
17 Correct 10 ms 20060 KB Output is correct
18 Correct 11 ms 20060 KB Output is correct
19 Correct 10 ms 20124 KB Output is correct
20 Correct 10 ms 20168 KB Output is correct
21 Correct 11 ms 20056 KB Output is correct
22 Correct 11 ms 20060 KB Output is correct
23 Correct 12 ms 20060 KB Output is correct
24 Correct 12 ms 20316 KB Output is correct
25 Correct 10 ms 20192 KB Output is correct
26 Correct 12 ms 20316 KB Output is correct
27 Correct 10 ms 20164 KB Output is correct
28 Correct 11 ms 20316 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 19800 KB Output is correct
2 Correct 10 ms 19804 KB Output is correct
3 Correct 10 ms 19832 KB Output is correct
4 Correct 10 ms 19800 KB Output is correct
5 Correct 10 ms 20132 KB Output is correct
6 Correct 10 ms 20208 KB Output is correct
7 Correct 10 ms 20060 KB Output is correct
8 Correct 10 ms 20168 KB Output is correct
9 Correct 10 ms 20060 KB Output is correct
10 Correct 90 ms 40296 KB Output is correct
11 Correct 111 ms 44716 KB Output is correct
12 Correct 108 ms 44488 KB Output is correct
13 Correct 116 ms 45948 KB Output is correct
14 Correct 86 ms 45768 KB Output is correct
15 Correct 10 ms 20092 KB Output is correct
16 Correct 10 ms 20060 KB Output is correct
17 Correct 12 ms 20112 KB Output is correct
18 Correct 11 ms 20056 KB Output is correct
19 Correct 10 ms 20212 KB Output is correct
20 Correct 11 ms 20288 KB Output is correct
21 Correct 11 ms 20060 KB Output is correct
22 Correct 10 ms 20060 KB Output is correct
23 Correct 11 ms 20060 KB Output is correct
24 Correct 10 ms 20124 KB Output is correct
25 Correct 10 ms 20168 KB Output is correct
26 Correct 11 ms 20056 KB Output is correct
27 Correct 11 ms 20060 KB Output is correct
28 Correct 12 ms 20060 KB Output is correct
29 Correct 12 ms 20316 KB Output is correct
30 Correct 10 ms 20192 KB Output is correct
31 Correct 12 ms 20316 KB Output is correct
32 Correct 10 ms 20164 KB Output is correct
33 Correct 11 ms 20316 KB Output is correct
34 Correct 20 ms 23324 KB Output is correct
35 Correct 161 ms 45764 KB Output is correct
36 Correct 114 ms 45768 KB Output is correct
37 Correct 123 ms 45764 KB Output is correct
38 Correct 109 ms 45420 KB Output is correct
39 Correct 111 ms 45152 KB Output is correct
40 Correct 135 ms 42700 KB Output is correct
41 Correct 114 ms 45760 KB Output is correct
42 Correct 128 ms 45768 KB Output is correct
43 Correct 92 ms 47300 KB Output is correct
44 Correct 113 ms 45000 KB Output is correct
45 Correct 116 ms 46784 KB Output is correct
46 Correct 128 ms 45832 KB Output is correct
47 Correct 135 ms 45768 KB Output is correct
48 Correct 129 ms 46460 KB Output is correct
49 Correct 55 ms 30656 KB Output is correct
50 Correct 58 ms 29640 KB Output is correct
51 Correct 101 ms 42440 KB Output is correct
52 Correct 152 ms 51904 KB Output is correct
53 Correct 162 ms 53236 KB Output is correct
54 Correct 183 ms 54424 KB Output is correct
55 Correct 102 ms 47420 KB Output is correct
56 Correct 147 ms 52676 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 19804 KB Output is correct
2 Correct 10 ms 19832 KB Output is correct
3 Correct 10 ms 19800 KB Output is correct
4 Correct 10 ms 20132 KB Output is correct
5 Correct 10 ms 20208 KB Output is correct
6 Correct 10 ms 20060 KB Output is correct
7 Correct 10 ms 20168 KB Output is correct
8 Correct 10 ms 20060 KB Output is correct
9 Correct 90 ms 40296 KB Output is correct
10 Correct 111 ms 44716 KB Output is correct
11 Correct 108 ms 44488 KB Output is correct
12 Correct 116 ms 45948 KB Output is correct
13 Correct 86 ms 45768 KB Output is correct
14 Correct 130 ms 40408 KB Output is correct
15 Correct 456 ms 46876 KB Output is correct
16 Correct 504 ms 46164 KB Output is correct
17 Correct 487 ms 47664 KB Output is correct
18 Correct 318 ms 47332 KB Output is correct
19 Correct 427 ms 41648 KB Output is correct
20 Correct 418 ms 42504 KB Output is correct
21 Correct 444 ms 41944 KB Output is correct
22 Correct 408 ms 42416 KB Output is correct
23 Correct 437 ms 42304 KB Output is correct
24 Correct 413 ms 41588 KB Output is correct
25 Correct 425 ms 42044 KB Output is correct
26 Correct 427 ms 41344 KB Output is correct
27 Correct 10 ms 20092 KB Output is correct
28 Correct 10 ms 20060 KB Output is correct
29 Correct 12 ms 20112 KB Output is correct
30 Correct 11 ms 20056 KB Output is correct
31 Correct 10 ms 20212 KB Output is correct
32 Correct 11 ms 20288 KB Output is correct
33 Correct 11 ms 20060 KB Output is correct
34 Correct 10 ms 20060 KB Output is correct
35 Correct 11 ms 20060 KB Output is correct
36 Correct 20 ms 23324 KB Output is correct
37 Correct 161 ms 45764 KB Output is correct
38 Correct 114 ms 45768 KB Output is correct
39 Correct 123 ms 45764 KB Output is correct
40 Correct 109 ms 45420 KB Output is correct
41 Correct 111 ms 45152 KB Output is correct
42 Correct 135 ms 42700 KB Output is correct
43 Correct 114 ms 45760 KB Output is correct
44 Correct 128 ms 45768 KB Output is correct
45 Correct 92 ms 47300 KB Output is correct
46 Correct 113 ms 45000 KB Output is correct
47 Correct 53 ms 23312 KB Output is correct
48 Correct 459 ms 48468 KB Output is correct
49 Correct 477 ms 48388 KB Output is correct
50 Correct 510 ms 48364 KB Output is correct
51 Correct 515 ms 48184 KB Output is correct
52 Correct 541 ms 46708 KB Output is correct
53 Correct 540 ms 39920 KB Output is correct
54 Correct 472 ms 49196 KB Output is correct
55 Correct 469 ms 48432 KB Output is correct
56 Execution timed out 2081 ms 48840 KB Time limit exceeded
57 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 11 ms 19800 KB Output is correct
2 Correct 10 ms 19804 KB Output is correct
3 Correct 10 ms 19832 KB Output is correct
4 Correct 10 ms 19800 KB Output is correct
5 Correct 10 ms 20132 KB Output is correct
6 Correct 10 ms 20208 KB Output is correct
7 Correct 10 ms 20060 KB Output is correct
8 Correct 10 ms 20168 KB Output is correct
9 Correct 10 ms 20060 KB Output is correct
10 Correct 90 ms 40296 KB Output is correct
11 Correct 111 ms 44716 KB Output is correct
12 Correct 108 ms 44488 KB Output is correct
13 Correct 116 ms 45948 KB Output is correct
14 Correct 86 ms 45768 KB Output is correct
15 Correct 130 ms 40408 KB Output is correct
16 Correct 456 ms 46876 KB Output is correct
17 Correct 504 ms 46164 KB Output is correct
18 Correct 487 ms 47664 KB Output is correct
19 Correct 318 ms 47332 KB Output is correct
20 Correct 427 ms 41648 KB Output is correct
21 Correct 418 ms 42504 KB Output is correct
22 Correct 444 ms 41944 KB Output is correct
23 Correct 408 ms 42416 KB Output is correct
24 Correct 437 ms 42304 KB Output is correct
25 Correct 413 ms 41588 KB Output is correct
26 Correct 425 ms 42044 KB Output is correct
27 Correct 427 ms 41344 KB Output is correct
28 Correct 10 ms 20092 KB Output is correct
29 Correct 10 ms 20060 KB Output is correct
30 Correct 12 ms 20112 KB Output is correct
31 Correct 11 ms 20056 KB Output is correct
32 Correct 10 ms 20212 KB Output is correct
33 Correct 11 ms 20288 KB Output is correct
34 Correct 11 ms 20060 KB Output is correct
35 Correct 10 ms 20060 KB Output is correct
36 Correct 11 ms 20060 KB Output is correct
37 Correct 20 ms 23324 KB Output is correct
38 Correct 161 ms 45764 KB Output is correct
39 Correct 114 ms 45768 KB Output is correct
40 Correct 123 ms 45764 KB Output is correct
41 Correct 109 ms 45420 KB Output is correct
42 Correct 111 ms 45152 KB Output is correct
43 Correct 135 ms 42700 KB Output is correct
44 Correct 114 ms 45760 KB Output is correct
45 Correct 128 ms 45768 KB Output is correct
46 Correct 92 ms 47300 KB Output is correct
47 Correct 113 ms 45000 KB Output is correct
48 Correct 53 ms 23312 KB Output is correct
49 Correct 459 ms 48468 KB Output is correct
50 Correct 477 ms 48388 KB Output is correct
51 Correct 510 ms 48364 KB Output is correct
52 Correct 515 ms 48184 KB Output is correct
53 Correct 541 ms 46708 KB Output is correct
54 Correct 540 ms 39920 KB Output is correct
55 Correct 472 ms 49196 KB Output is correct
56 Correct 469 ms 48432 KB Output is correct
57 Execution timed out 2081 ms 48840 KB Time limit exceeded
58 Halted 0 ms 0 KB -