답안 #951560

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
951560 2024-03-22T06:22:50 Z Darren0724 Reconstruction Project (JOI22_reconstruction) C++17
7 / 100
5000 ms 14788 KB
#pragma GCC optimize("Ofast","O3","unroll-loops")
#pragma GCC target("avx","avx2")
#include <bits/stdc++.h>
using namespace std;
#define LCBorz ios_base::sync_with_stdio(false); cin.tie(0);
#define ll long long 
#define all(x) x.begin(), x.end()
#define endl '\n'
const int N=505;
const int M=1000005;
const ll INF=1e18;
struct DSU{
    vector<int> pa,sz;
    DSU(int n){
        pa.resize(n);
        sz.resize(n,1);
        iota(all(pa),0);
    }
    int Find(int k){
        return (k==pa[k]?k:pa[k]=Find(pa[k]));
    }
    void onion(int a,int b){
        a=Find(a),b=Find(b);
        if(a==b)return;
        if(sz[a]>sz[b]){
            swap(a,b);
        }
        pa[a]=b;
        sz[b]+=sz[a];
    }
    int same(int a,int b){
        return Find(a)==Find(b);
    }
};  
int32_t main() {
    LCBorz;
    int n,m;cin>>n>>m;
    vector<array<int,3>> v(m);
    for(int i=0;i<m;i++){
        cin>>v[i][1]>>v[i][2]>>v[i][0];
    }
    sort(all(v));
    int ptr=0;
    int q;cin>>q;
    for(int q1=0;q1<q;q1++){
        int x;cin>>x;
        while(ptr<m&&v[ptr][0]<=x){
            ptr++;
        }
        DSU dsu(n+1);
        ll ans=0;
        int l=ptr-1,r=ptr;
        while(l>=0&&r<m){
            if(abs(x-v[l][0])<abs(x-v[r][0])){
                if(!dsu.same(v[l][1],v[l][2])){
                    dsu.onion(v[l][1],v[l][2]);
                    ans+=abs(x-v[l][0]);
                }
                l--;
            }
            else{
                if(!dsu.same(v[r][1],v[r][2])){
                    dsu.onion(v[r][1],v[r][2]);
                    ans+=abs(x-v[r][0]);
                }
                r++;
            }   
        }
        while(l>=0){
            if(!dsu.same(v[l][1],v[l][2])){
                dsu.onion(v[l][1],v[l][2]);
                ans+=abs(x-v[l][0]);
            }
            l--;
        }
        while(r<m){
            if(!dsu.same(v[r][1],v[r][2])){
                dsu.onion(v[r][1],v[r][2]);
                ans+=abs(x-v[r][0]);
            }
            r++;
        }
        cout<<ans<<endl;
    }   


    
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 456 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 0 ms 452 KB Output is correct
6 Correct 0 ms 344 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 1 ms 356 KB Output is correct
12 Correct 1 ms 344 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 1 ms 600 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 1 ms 344 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 456 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 0 ms 452 KB Output is correct
6 Correct 0 ms 344 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 1 ms 356 KB Output is correct
12 Correct 1 ms 344 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 1 ms 600 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 1 ms 344 KB Output is correct
19 Correct 0 ms 604 KB Output is correct
20 Correct 34 ms 2400 KB Output is correct
21 Correct 41 ms 2640 KB Output is correct
22 Correct 35 ms 2504 KB Output is correct
23 Correct 44 ms 2612 KB Output is correct
24 Correct 33 ms 2848 KB Output is correct
25 Correct 35 ms 2604 KB Output is correct
26 Correct 47 ms 2784 KB Output is correct
27 Correct 34 ms 2620 KB Output is correct
28 Correct 34 ms 2640 KB Output is correct
29 Correct 36 ms 2648 KB Output is correct
30 Correct 35 ms 2648 KB Output is correct
31 Correct 37 ms 2640 KB Output is correct
32 Correct 35 ms 2500 KB Output is correct
33 Correct 34 ms 2600 KB Output is correct
34 Correct 48 ms 2640 KB Output is correct
35 Correct 41 ms 2608 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 456 KB Output is correct
4 Execution timed out 5036 ms 3460 KB Time limit exceeded
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 456 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 0 ms 452 KB Output is correct
6 Correct 0 ms 344 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 1 ms 356 KB Output is correct
12 Correct 1 ms 344 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 1 ms 600 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 1 ms 344 KB Output is correct
19 Correct 1 ms 448 KB Output is correct
20 Execution timed out 5025 ms 14788 KB Time limit exceeded
21 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 456 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 0 ms 452 KB Output is correct
6 Correct 0 ms 344 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 1 ms 356 KB Output is correct
12 Correct 1 ms 344 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 1 ms 600 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 1 ms 344 KB Output is correct
19 Correct 0 ms 604 KB Output is correct
20 Correct 34 ms 2400 KB Output is correct
21 Correct 41 ms 2640 KB Output is correct
22 Correct 35 ms 2504 KB Output is correct
23 Correct 44 ms 2612 KB Output is correct
24 Correct 33 ms 2848 KB Output is correct
25 Correct 35 ms 2604 KB Output is correct
26 Correct 47 ms 2784 KB Output is correct
27 Correct 34 ms 2620 KB Output is correct
28 Correct 34 ms 2640 KB Output is correct
29 Correct 36 ms 2648 KB Output is correct
30 Correct 35 ms 2648 KB Output is correct
31 Correct 37 ms 2640 KB Output is correct
32 Correct 35 ms 2500 KB Output is correct
33 Correct 34 ms 2600 KB Output is correct
34 Correct 48 ms 2640 KB Output is correct
35 Correct 41 ms 2608 KB Output is correct
36 Execution timed out 5022 ms 2944 KB Time limit exceeded
37 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 456 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 0 ms 452 KB Output is correct
6 Correct 0 ms 344 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 1 ms 356 KB Output is correct
12 Correct 1 ms 344 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 1 ms 600 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 1 ms 344 KB Output is correct
19 Correct 0 ms 604 KB Output is correct
20 Correct 34 ms 2400 KB Output is correct
21 Correct 41 ms 2640 KB Output is correct
22 Correct 35 ms 2504 KB Output is correct
23 Correct 44 ms 2612 KB Output is correct
24 Correct 33 ms 2848 KB Output is correct
25 Correct 35 ms 2604 KB Output is correct
26 Correct 47 ms 2784 KB Output is correct
27 Correct 34 ms 2620 KB Output is correct
28 Correct 34 ms 2640 KB Output is correct
29 Correct 36 ms 2648 KB Output is correct
30 Correct 35 ms 2648 KB Output is correct
31 Correct 37 ms 2640 KB Output is correct
32 Correct 35 ms 2500 KB Output is correct
33 Correct 34 ms 2600 KB Output is correct
34 Correct 48 ms 2640 KB Output is correct
35 Correct 41 ms 2608 KB Output is correct
36 Correct 0 ms 348 KB Output is correct
37 Correct 0 ms 348 KB Output is correct
38 Correct 0 ms 456 KB Output is correct
39 Execution timed out 5036 ms 3460 KB Time limit exceeded
40 Halted 0 ms 0 KB -