답안 #596853

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
596853 2022-07-15T07:47:53 Z 이동현(#8450) Reconstruction Project (JOI22_reconstruction) C++17
70 / 100
5000 ms 16332 KB
#include <bits/stdc++.h>

using namespace std;

const int MS = (int)1e5 + 4;
int n, m, q;
vector<int> way[MS];
int l[MS], r[MS];
int rst[504], rstN, lev[MS], rev[MS];
int lst[504], lstN;

int pr[504];
struct Dsu{
    int n;
    Dsu(){}
    Dsu(int n):n(n + 4){
        for(int i = 1; i <= n; ++i){
            pr[i] = i;
        }
    }
    inline int fd(int x){
        return (x == pr[x] ? x : pr[x] = fd(pr[x]));
    }
    inline bool unite(int x, int y){
        x = fd(x), y = fd(y);
        if(x == y) return false;
        pr[x] = y;
        return true;
    }
};

signed main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    memset(lev, -1, sizeof(lev)), memset(rev, -1, sizeof(rev));
    cin >> n >> m;
    for(int i = 0; i < m; ++i){
        way[i].resize(3);
        cin >> way[i][1] >> way[i][2] >> way[i][0];
        l[i] = -1, r[i] = m;
    }
    sort(way, way + m);
    for(int i = 0; i < m; ++i){
        Dsu gr(n);
        gr.unite(way[i][1], way[i][2]);
        int pos = lstN;
        while(pos){
            if(!gr.unite(way[lst[pos]][1], way[lst[pos]][2])){
                break;
            }
            --pos;
        }
        if(pos){
            lev[i] = lst[pos];
            l[i] = lev[i] + 1;
            for(int k = pos; k < lstN; ++k){
                lst[k] = lst[k + 1];
            }
            lst[lstN] = i;
        }
        else{
            l[i] = -1;
            lst[++lstN] = i;
        }
    }
    lstN = 0;
    for(int i = m - 1; i >= 0; --i){
        if(rstN >= n - 1){
            r[i] = rst[1] - 1;
        }
        Dsu gr(n);
        gr.unite(way[i][1], way[i][2]);
        int pos = rstN;
        while(pos){
            if(!gr.unite(way[rst[pos]][1], way[rst[pos]][2])){
                break;
            }
            --pos;
        }
        if(pos){
            rev[i] = rst[pos];
            r[i] = rev[i] - 1;
            for(int k = pos; k < rstN; ++k){
                rst[k] = rst[k + 1];
            }
            rst[rstN] = i;
        }
        else{
            r[i] = m;
            rst[++rstN] = i;
        }
    }
    cin >> q;
    int rp = 0;
    while(q--){
        int x; cin >> x;
        while(rp < m && way[rp][0] < x){
            if(rev[rp] == -1){
                --rstN;
            }
            else{
                for(int i = rstN; ; --i){
                    if(i > 1 && rst[i - 1] < rev[rp]){
                        rst[i] = rst[i - 1];
                    }
                    else{
                        rst[i] = rev[rp];
                        break;
                    }
                }
            }
            if(lev[rp] == -1){
                lst[++lstN] = rp;
            }
            else{
                int pos = lstN;
                while(lst[pos] != lev[rp]){
                    --pos;
                }
                for(int i = pos; i < lstN; ++i){
                    lst[i] = lst[i + 1];
                }
                lst[lstN] = rp;
            }
            ++rp;
        }
        int lid = lstN, rid = rstN, cnt = 0;
        lst[lid + 1] = (int)2e9, rst[rid + 1] = -100;
        long long ans = 0;
        while(cnt < n - 1){
            if(lid > 0 && r[lst[lid]] < rst[rid + 1]){
                --lid; continue;
            }
            if(rid > 0 && l[rst[rid]] > lst[lid + 1]){
                --rid; continue;
            }
            if(!rid || (lid && x - way[lst[lid]][0] < way[rst[rid]][0] - x)){
                ans += x - way[lst[lid]][0];
                --lid;
            }
            else{
                ans += way[rst[rid]][0] - x;
                --rid;
            }
            ++cnt;
        }
        cout << ans << '\n';
    }
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 3412 KB Output is correct
2 Correct 2 ms 3412 KB Output is correct
3 Correct 2 ms 3412 KB Output is correct
4 Correct 2 ms 3412 KB Output is correct
5 Correct 2 ms 3412 KB Output is correct
6 Correct 2 ms 3412 KB Output is correct
7 Correct 2 ms 3412 KB Output is correct
8 Correct 2 ms 3412 KB Output is correct
9 Correct 2 ms 3412 KB Output is correct
10 Correct 2 ms 3412 KB Output is correct
11 Correct 2 ms 3412 KB Output is correct
12 Correct 2 ms 3412 KB Output is correct
13 Correct 2 ms 3412 KB Output is correct
14 Correct 2 ms 3412 KB Output is correct
15 Correct 2 ms 3412 KB Output is correct
16 Correct 2 ms 3392 KB Output is correct
17 Correct 2 ms 3412 KB Output is correct
18 Correct 2 ms 3412 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 3412 KB Output is correct
2 Correct 2 ms 3412 KB Output is correct
3 Correct 2 ms 3412 KB Output is correct
4 Correct 2 ms 3412 KB Output is correct
5 Correct 2 ms 3412 KB Output is correct
6 Correct 2 ms 3412 KB Output is correct
7 Correct 2 ms 3412 KB Output is correct
8 Correct 2 ms 3412 KB Output is correct
9 Correct 2 ms 3412 KB Output is correct
10 Correct 2 ms 3412 KB Output is correct
11 Correct 2 ms 3412 KB Output is correct
12 Correct 2 ms 3412 KB Output is correct
13 Correct 2 ms 3412 KB Output is correct
14 Correct 2 ms 3412 KB Output is correct
15 Correct 2 ms 3412 KB Output is correct
16 Correct 2 ms 3392 KB Output is correct
17 Correct 2 ms 3412 KB Output is correct
18 Correct 2 ms 3412 KB Output is correct
19 Correct 2 ms 3412 KB Output is correct
20 Correct 1660 ms 7348 KB Output is correct
21 Correct 800 ms 7372 KB Output is correct
22 Correct 870 ms 7348 KB Output is correct
23 Correct 957 ms 7244 KB Output is correct
24 Correct 1282 ms 7352 KB Output is correct
25 Correct 1646 ms 7348 KB Output is correct
26 Correct 1722 ms 7348 KB Output is correct
27 Correct 1729 ms 7348 KB Output is correct
28 Correct 1706 ms 7352 KB Output is correct
29 Correct 965 ms 7344 KB Output is correct
30 Correct 1712 ms 7348 KB Output is correct
31 Correct 1696 ms 7352 KB Output is correct
32 Correct 1744 ms 7348 KB Output is correct
33 Correct 1701 ms 7348 KB Output is correct
34 Correct 1204 ms 7344 KB Output is correct
35 Correct 1674 ms 7360 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 3412 KB Output is correct
2 Correct 2 ms 3412 KB Output is correct
3 Correct 2 ms 3412 KB Output is correct
4 Execution timed out 5051 ms 14712 KB Time limit exceeded
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 3412 KB Output is correct
2 Correct 2 ms 3412 KB Output is correct
3 Correct 2 ms 3412 KB Output is correct
4 Correct 2 ms 3412 KB Output is correct
5 Correct 2 ms 3412 KB Output is correct
6 Correct 2 ms 3412 KB Output is correct
7 Correct 2 ms 3412 KB Output is correct
8 Correct 2 ms 3412 KB Output is correct
9 Correct 2 ms 3412 KB Output is correct
10 Correct 2 ms 3412 KB Output is correct
11 Correct 2 ms 3412 KB Output is correct
12 Correct 2 ms 3412 KB Output is correct
13 Correct 2 ms 3412 KB Output is correct
14 Correct 2 ms 3412 KB Output is correct
15 Correct 2 ms 3412 KB Output is correct
16 Correct 2 ms 3392 KB Output is correct
17 Correct 2 ms 3412 KB Output is correct
18 Correct 2 ms 3412 KB Output is correct
19 Correct 2 ms 3412 KB Output is correct
20 Correct 2539 ms 15744 KB Output is correct
21 Correct 2442 ms 15856 KB Output is correct
22 Correct 2626 ms 15848 KB Output is correct
23 Correct 2634 ms 15836 KB Output is correct
24 Correct 2606 ms 15764 KB Output is correct
25 Correct 2600 ms 15676 KB Output is correct
26 Correct 2432 ms 15728 KB Output is correct
27 Correct 2252 ms 15740 KB Output is correct
28 Correct 2658 ms 15764 KB Output is correct
29 Correct 2618 ms 15736 KB Output is correct
30 Correct 2254 ms 15768 KB Output is correct
31 Correct 2699 ms 15724 KB Output is correct
32 Correct 1306 ms 16332 KB Output is correct
33 Correct 2823 ms 15436 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 3412 KB Output is correct
2 Correct 2 ms 3412 KB Output is correct
3 Correct 2 ms 3412 KB Output is correct
4 Correct 2 ms 3412 KB Output is correct
5 Correct 2 ms 3412 KB Output is correct
6 Correct 2 ms 3412 KB Output is correct
7 Correct 2 ms 3412 KB Output is correct
8 Correct 2 ms 3412 KB Output is correct
9 Correct 2 ms 3412 KB Output is correct
10 Correct 2 ms 3412 KB Output is correct
11 Correct 2 ms 3412 KB Output is correct
12 Correct 2 ms 3412 KB Output is correct
13 Correct 2 ms 3412 KB Output is correct
14 Correct 2 ms 3412 KB Output is correct
15 Correct 2 ms 3412 KB Output is correct
16 Correct 2 ms 3392 KB Output is correct
17 Correct 2 ms 3412 KB Output is correct
18 Correct 2 ms 3412 KB Output is correct
19 Correct 2 ms 3412 KB Output is correct
20 Correct 1660 ms 7348 KB Output is correct
21 Correct 800 ms 7372 KB Output is correct
22 Correct 870 ms 7348 KB Output is correct
23 Correct 957 ms 7244 KB Output is correct
24 Correct 1282 ms 7352 KB Output is correct
25 Correct 1646 ms 7348 KB Output is correct
26 Correct 1722 ms 7348 KB Output is correct
27 Correct 1729 ms 7348 KB Output is correct
28 Correct 1706 ms 7352 KB Output is correct
29 Correct 965 ms 7344 KB Output is correct
30 Correct 1712 ms 7348 KB Output is correct
31 Correct 1696 ms 7352 KB Output is correct
32 Correct 1744 ms 7348 KB Output is correct
33 Correct 1701 ms 7348 KB Output is correct
34 Correct 1204 ms 7344 KB Output is correct
35 Correct 1674 ms 7360 KB Output is correct
36 Correct 1866 ms 7612 KB Output is correct
37 Correct 989 ms 7828 KB Output is correct
38 Correct 982 ms 7820 KB Output is correct
39 Correct 1150 ms 7732 KB Output is correct
40 Correct 1384 ms 7744 KB Output is correct
41 Correct 1808 ms 7696 KB Output is correct
42 Correct 1835 ms 7836 KB Output is correct
43 Correct 1863 ms 7720 KB Output is correct
44 Correct 1734 ms 7796 KB Output is correct
45 Correct 914 ms 7820 KB Output is correct
46 Correct 1814 ms 7756 KB Output is correct
47 Correct 1839 ms 7860 KB Output is correct
48 Correct 1876 ms 7708 KB Output is correct
49 Correct 1819 ms 7912 KB Output is correct
50 Correct 1225 ms 7852 KB Output is correct
51 Correct 1786 ms 7732 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 3412 KB Output is correct
2 Correct 2 ms 3412 KB Output is correct
3 Correct 2 ms 3412 KB Output is correct
4 Correct 2 ms 3412 KB Output is correct
5 Correct 2 ms 3412 KB Output is correct
6 Correct 2 ms 3412 KB Output is correct
7 Correct 2 ms 3412 KB Output is correct
8 Correct 2 ms 3412 KB Output is correct
9 Correct 2 ms 3412 KB Output is correct
10 Correct 2 ms 3412 KB Output is correct
11 Correct 2 ms 3412 KB Output is correct
12 Correct 2 ms 3412 KB Output is correct
13 Correct 2 ms 3412 KB Output is correct
14 Correct 2 ms 3412 KB Output is correct
15 Correct 2 ms 3412 KB Output is correct
16 Correct 2 ms 3392 KB Output is correct
17 Correct 2 ms 3412 KB Output is correct
18 Correct 2 ms 3412 KB Output is correct
19 Correct 2 ms 3412 KB Output is correct
20 Correct 1660 ms 7348 KB Output is correct
21 Correct 800 ms 7372 KB Output is correct
22 Correct 870 ms 7348 KB Output is correct
23 Correct 957 ms 7244 KB Output is correct
24 Correct 1282 ms 7352 KB Output is correct
25 Correct 1646 ms 7348 KB Output is correct
26 Correct 1722 ms 7348 KB Output is correct
27 Correct 1729 ms 7348 KB Output is correct
28 Correct 1706 ms 7352 KB Output is correct
29 Correct 965 ms 7344 KB Output is correct
30 Correct 1712 ms 7348 KB Output is correct
31 Correct 1696 ms 7352 KB Output is correct
32 Correct 1744 ms 7348 KB Output is correct
33 Correct 1701 ms 7348 KB Output is correct
34 Correct 1204 ms 7344 KB Output is correct
35 Correct 1674 ms 7360 KB Output is correct
36 Correct 2 ms 3412 KB Output is correct
37 Correct 2 ms 3412 KB Output is correct
38 Correct 2 ms 3412 KB Output is correct
39 Execution timed out 5051 ms 14712 KB Time limit exceeded
40 Halted 0 ms 0 KB -