Submission #914141

# Submission time Handle Problem Language Result Execution time Memory
914141 2024-01-21T09:27:57 Z Shayan86 Reconstruction Project (JOI22_reconstruction) C++14
100 / 100
1426 ms 46548 KB
#include <bits/stdc++.h>
using namespace std;

#pragma GCC optimize("O3,unroll-loops")
// #pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
// Ofast, O0, O1, O2, O3, unroll-loops, fast-math, trapv

typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;

#define Mp          make_pair
#define sep         ' '
#define endl        '\n'
#define F	        first
#define S	        second
#define pb          push_back
#define all(x)      (x).begin(),(x).end()
#define kill(res)	cout << res << '\n', exit(0);
#define set_dec(x)	cout << fixed << setprecision(x);
#define fast_io     ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define file_io     freopen("input.txt", "r", stdin) ; freopen("output.txt", "w", stdout);

const ll N = 507;
const ll M = 1e5 + 50;
const ll Q = 1e6 + 50;
const ll Mod = 1e9 + 7;

int n, m, q, lt[M], rt[M], qu[Q], par[N], sz[N];
ll zp[Q], sum[Q];

vector<pair<int, pii>> edge;

int getPar(int v){
    return (par[v] == v ? v : par[v] = getPar(par[v]));
}

bool Union(int u, int v){
    u = getPar(u);
    v = getPar(v);
    if(u == v) return false;
    if(sz[v] < sz[u]) swap(u, v);
    sz[v] += sz[u]; par[u] = v;
    return true;
}

int main(){
    fast_io;

    cin >> n >> m;

    int u, v, w;
    for(int i = 1; i <= m; i++){
        cin >> u >> v >> w;
        edge.pb({w, {u, v}});
    }
    sort(all(edge));

    cin >> q;
    for(int i = 0; i < q; i++) cin >> qu[i];

    for(int i = 0; i < m; i++){
        for(int j = 1; j <= n; j++){
            sz[j] = 1; par[j] = j;
        }

        int u = edge[i].S.F;
        int v = edge[i].S.S;
        int w = edge[i].F;

        lt[i] = -1;
        for(int j = i-1; j >= 0; j--){
            bool ch = Union(edge[j].S.F, edge[j].S.S);
            if(ch && getPar(u) == getPar(v)){
                lt[i] = j; break;
            }
        }
    }
    for(int i = 0; i < m; i++){
        for(int j = 1; j <= n; j++){
            sz[j] = 1; par[j] = j;
        }

        int u = edge[i].S.F;
        int v = edge[i].S.S;
        int w = edge[i].F;

        rt[i] = m;
        for(int j = i+1; j < m; j++){
            bool ch = Union(edge[j].S.F, edge[j].S.S);
            if(ch && getPar(u) == getPar(v)){
                rt[i] = j; break;
            }
        }
    }

    //for(int i = 0; i < m; i++) cout << lt[i] << sep << rt[i] << endl;

    for(int i = 0; i < m; i++){
        int p1 = 0;
        if(lt[i] != -1) p1 = lower_bound(qu, qu + q, (edge[i].F + edge[lt[i]].F + 1)/2) - qu;

        int p2 = lower_bound(qu, qu + q, edge[i].F) - qu;

        int p3 = q;
        if(rt[i] != m) p3 = lower_bound(qu, qu + q, (edge[i].F + edge[rt[i]].F - 1) / 2 + 1) - qu;

        //cout << edge[i].F << sep << edge[i].S.F << sep << edge[i].S.S << sep << p1 << sep << p2 << sep << p3 << endl;
        sum[p1] += edge[i].F;
        sum[p2] -= 2*edge[i].F;
        sum[p3] += edge[i].F;
        zp[p1]--;
        zp[p2] += 2;
        zp[p3]--;
    }

    for(int i = 1; i < q; i++){
        sum[i] += sum[i-1]; zp[i] += zp[i-1];
    }

    for(int i = 0; i < q; i++){
        ll res = sum[i] + zp[i] * qu[i];
        cout << res << endl;
        //cout << res << sep << sum[i] << sep << zp[i] << endl;
    }

}

Compilation message

reconstruction.cpp: In function 'int main()':
reconstruction.cpp:69:13: warning: unused variable 'w' [-Wunused-variable]
   69 |         int w = edge[i].F;
      |             ^
reconstruction.cpp:86:13: warning: unused variable 'w' [-Wunused-variable]
   86 |         int w = edge[i].F;
      |             ^
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4440 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 1 ms 4508 KB Output is correct
4 Correct 1 ms 4444 KB Output is correct
5 Correct 1 ms 4444 KB Output is correct
6 Correct 1 ms 4444 KB Output is correct
7 Correct 1 ms 4700 KB Output is correct
8 Correct 1 ms 4444 KB Output is correct
9 Correct 1 ms 4444 KB Output is correct
10 Correct 1 ms 4444 KB Output is correct
11 Correct 1 ms 4440 KB Output is correct
12 Correct 1 ms 4440 KB Output is correct
13 Correct 1 ms 4444 KB Output is correct
14 Correct 1 ms 4444 KB Output is correct
15 Correct 1 ms 4444 KB Output is correct
16 Correct 1 ms 4444 KB Output is correct
17 Correct 1 ms 4444 KB Output is correct
18 Correct 1 ms 4444 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4440 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 1 ms 4508 KB Output is correct
4 Correct 1 ms 4444 KB Output is correct
5 Correct 1 ms 4444 KB Output is correct
6 Correct 1 ms 4444 KB Output is correct
7 Correct 1 ms 4700 KB Output is correct
8 Correct 1 ms 4444 KB Output is correct
9 Correct 1 ms 4444 KB Output is correct
10 Correct 1 ms 4444 KB Output is correct
11 Correct 1 ms 4440 KB Output is correct
12 Correct 1 ms 4440 KB Output is correct
13 Correct 1 ms 4444 KB Output is correct
14 Correct 1 ms 4444 KB Output is correct
15 Correct 1 ms 4444 KB Output is correct
16 Correct 1 ms 4444 KB Output is correct
17 Correct 1 ms 4444 KB Output is correct
18 Correct 1 ms 4444 KB Output is correct
19 Correct 1 ms 4444 KB Output is correct
20 Correct 1237 ms 8344 KB Output is correct
21 Correct 905 ms 8536 KB Output is correct
22 Correct 965 ms 8296 KB Output is correct
23 Correct 1092 ms 8380 KB Output is correct
24 Correct 1228 ms 8468 KB Output is correct
25 Correct 1241 ms 8400 KB Output is correct
26 Correct 1242 ms 8392 KB Output is correct
27 Correct 1234 ms 8376 KB Output is correct
28 Correct 1208 ms 8188 KB Output is correct
29 Correct 723 ms 8244 KB Output is correct
30 Correct 1294 ms 8308 KB Output is correct
31 Correct 1247 ms 8500 KB Output is correct
32 Correct 1247 ms 8564 KB Output is correct
33 Correct 1235 ms 8356 KB Output is correct
34 Correct 594 ms 8384 KB Output is correct
35 Correct 1230 ms 8388 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4444 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 1 ms 4444 KB Output is correct
4 Correct 1232 ms 44312 KB Output is correct
5 Correct 1235 ms 44444 KB Output is correct
6 Correct 1261 ms 44392 KB Output is correct
7 Correct 662 ms 46436 KB Output is correct
8 Correct 466 ms 46344 KB Output is correct
9 Correct 444 ms 46548 KB Output is correct
10 Correct 1221 ms 44492 KB Output is correct
11 Correct 453 ms 46400 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4440 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 1 ms 4508 KB Output is correct
4 Correct 1 ms 4444 KB Output is correct
5 Correct 1 ms 4444 KB Output is correct
6 Correct 1 ms 4444 KB Output is correct
7 Correct 1 ms 4700 KB Output is correct
8 Correct 1 ms 4444 KB Output is correct
9 Correct 1 ms 4444 KB Output is correct
10 Correct 1 ms 4444 KB Output is correct
11 Correct 1 ms 4440 KB Output is correct
12 Correct 1 ms 4440 KB Output is correct
13 Correct 1 ms 4444 KB Output is correct
14 Correct 1 ms 4444 KB Output is correct
15 Correct 1 ms 4444 KB Output is correct
16 Correct 1 ms 4444 KB Output is correct
17 Correct 1 ms 4444 KB Output is correct
18 Correct 1 ms 4444 KB Output is correct
19 Correct 1 ms 4444 KB Output is correct
20 Correct 146 ms 41808 KB Output is correct
21 Correct 147 ms 41940 KB Output is correct
22 Correct 145 ms 41812 KB Output is correct
23 Correct 157 ms 42064 KB Output is correct
24 Correct 151 ms 41824 KB Output is correct
25 Correct 152 ms 41804 KB Output is correct
26 Correct 168 ms 41948 KB Output is correct
27 Correct 150 ms 42204 KB Output is correct
28 Correct 177 ms 41812 KB Output is correct
29 Correct 160 ms 41964 KB Output is correct
30 Correct 150 ms 42064 KB Output is correct
31 Correct 149 ms 41684 KB Output is correct
32 Correct 156 ms 42500 KB Output is correct
33 Correct 160 ms 42156 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4440 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 1 ms 4508 KB Output is correct
4 Correct 1 ms 4444 KB Output is correct
5 Correct 1 ms 4444 KB Output is correct
6 Correct 1 ms 4444 KB Output is correct
7 Correct 1 ms 4700 KB Output is correct
8 Correct 1 ms 4444 KB Output is correct
9 Correct 1 ms 4444 KB Output is correct
10 Correct 1 ms 4444 KB Output is correct
11 Correct 1 ms 4440 KB Output is correct
12 Correct 1 ms 4440 KB Output is correct
13 Correct 1 ms 4444 KB Output is correct
14 Correct 1 ms 4444 KB Output is correct
15 Correct 1 ms 4444 KB Output is correct
16 Correct 1 ms 4444 KB Output is correct
17 Correct 1 ms 4444 KB Output is correct
18 Correct 1 ms 4444 KB Output is correct
19 Correct 1 ms 4444 KB Output is correct
20 Correct 1237 ms 8344 KB Output is correct
21 Correct 905 ms 8536 KB Output is correct
22 Correct 965 ms 8296 KB Output is correct
23 Correct 1092 ms 8380 KB Output is correct
24 Correct 1228 ms 8468 KB Output is correct
25 Correct 1241 ms 8400 KB Output is correct
26 Correct 1242 ms 8392 KB Output is correct
27 Correct 1234 ms 8376 KB Output is correct
28 Correct 1208 ms 8188 KB Output is correct
29 Correct 723 ms 8244 KB Output is correct
30 Correct 1294 ms 8308 KB Output is correct
31 Correct 1247 ms 8500 KB Output is correct
32 Correct 1247 ms 8564 KB Output is correct
33 Correct 1235 ms 8356 KB Output is correct
34 Correct 594 ms 8384 KB Output is correct
35 Correct 1230 ms 8388 KB Output is correct
36 Correct 1243 ms 8692 KB Output is correct
37 Correct 917 ms 8948 KB Output is correct
38 Correct 979 ms 8808 KB Output is correct
39 Correct 1107 ms 9016 KB Output is correct
40 Correct 1278 ms 8896 KB Output is correct
41 Correct 1235 ms 9124 KB Output is correct
42 Correct 1296 ms 8944 KB Output is correct
43 Correct 1263 ms 8716 KB Output is correct
44 Correct 1260 ms 9028 KB Output is correct
45 Correct 747 ms 8900 KB Output is correct
46 Correct 1248 ms 8696 KB Output is correct
47 Correct 1270 ms 8916 KB Output is correct
48 Correct 1251 ms 8740 KB Output is correct
49 Correct 1287 ms 8916 KB Output is correct
50 Correct 594 ms 9048 KB Output is correct
51 Correct 1279 ms 8992 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4440 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 1 ms 4508 KB Output is correct
4 Correct 1 ms 4444 KB Output is correct
5 Correct 1 ms 4444 KB Output is correct
6 Correct 1 ms 4444 KB Output is correct
7 Correct 1 ms 4700 KB Output is correct
8 Correct 1 ms 4444 KB Output is correct
9 Correct 1 ms 4444 KB Output is correct
10 Correct 1 ms 4444 KB Output is correct
11 Correct 1 ms 4440 KB Output is correct
12 Correct 1 ms 4440 KB Output is correct
13 Correct 1 ms 4444 KB Output is correct
14 Correct 1 ms 4444 KB Output is correct
15 Correct 1 ms 4444 KB Output is correct
16 Correct 1 ms 4444 KB Output is correct
17 Correct 1 ms 4444 KB Output is correct
18 Correct 1 ms 4444 KB Output is correct
19 Correct 1 ms 4444 KB Output is correct
20 Correct 1237 ms 8344 KB Output is correct
21 Correct 905 ms 8536 KB Output is correct
22 Correct 965 ms 8296 KB Output is correct
23 Correct 1092 ms 8380 KB Output is correct
24 Correct 1228 ms 8468 KB Output is correct
25 Correct 1241 ms 8400 KB Output is correct
26 Correct 1242 ms 8392 KB Output is correct
27 Correct 1234 ms 8376 KB Output is correct
28 Correct 1208 ms 8188 KB Output is correct
29 Correct 723 ms 8244 KB Output is correct
30 Correct 1294 ms 8308 KB Output is correct
31 Correct 1247 ms 8500 KB Output is correct
32 Correct 1247 ms 8564 KB Output is correct
33 Correct 1235 ms 8356 KB Output is correct
34 Correct 594 ms 8384 KB Output is correct
35 Correct 1230 ms 8388 KB Output is correct
36 Correct 1 ms 4444 KB Output is correct
37 Correct 1 ms 4444 KB Output is correct
38 Correct 1 ms 4444 KB Output is correct
39 Correct 1232 ms 44312 KB Output is correct
40 Correct 1235 ms 44444 KB Output is correct
41 Correct 1261 ms 44392 KB Output is correct
42 Correct 662 ms 46436 KB Output is correct
43 Correct 466 ms 46344 KB Output is correct
44 Correct 444 ms 46548 KB Output is correct
45 Correct 1221 ms 44492 KB Output is correct
46 Correct 453 ms 46400 KB Output is correct
47 Correct 1 ms 4444 KB Output is correct
48 Correct 146 ms 41808 KB Output is correct
49 Correct 147 ms 41940 KB Output is correct
50 Correct 145 ms 41812 KB Output is correct
51 Correct 157 ms 42064 KB Output is correct
52 Correct 151 ms 41824 KB Output is correct
53 Correct 152 ms 41804 KB Output is correct
54 Correct 168 ms 41948 KB Output is correct
55 Correct 150 ms 42204 KB Output is correct
56 Correct 177 ms 41812 KB Output is correct
57 Correct 160 ms 41964 KB Output is correct
58 Correct 150 ms 42064 KB Output is correct
59 Correct 149 ms 41684 KB Output is correct
60 Correct 156 ms 42500 KB Output is correct
61 Correct 160 ms 42156 KB Output is correct
62 Correct 1243 ms 8692 KB Output is correct
63 Correct 917 ms 8948 KB Output is correct
64 Correct 979 ms 8808 KB Output is correct
65 Correct 1107 ms 9016 KB Output is correct
66 Correct 1278 ms 8896 KB Output is correct
67 Correct 1235 ms 9124 KB Output is correct
68 Correct 1296 ms 8944 KB Output is correct
69 Correct 1263 ms 8716 KB Output is correct
70 Correct 1260 ms 9028 KB Output is correct
71 Correct 747 ms 8900 KB Output is correct
72 Correct 1248 ms 8696 KB Output is correct
73 Correct 1270 ms 8916 KB Output is correct
74 Correct 1251 ms 8740 KB Output is correct
75 Correct 1287 ms 8916 KB Output is correct
76 Correct 594 ms 9048 KB Output is correct
77 Correct 1279 ms 8992 KB Output is correct
78 Correct 1426 ms 43508 KB Output is correct
79 Correct 1101 ms 45508 KB Output is correct
80 Correct 1126 ms 44444 KB Output is correct
81 Correct 1265 ms 44424 KB Output is correct
82 Correct 1422 ms 43700 KB Output is correct
83 Correct 1387 ms 43752 KB Output is correct
84 Correct 1403 ms 43308 KB Output is correct
85 Correct 1390 ms 43452 KB Output is correct
86 Correct 1364 ms 43476 KB Output is correct
87 Correct 834 ms 45236 KB Output is correct
88 Correct 1384 ms 43416 KB Output is correct
89 Correct 1408 ms 43556 KB Output is correct
90 Correct 1382 ms 43464 KB Output is correct
91 Correct 1406 ms 43460 KB Output is correct
92 Correct 745 ms 46352 KB Output is correct
93 Correct 1375 ms 45268 KB Output is correct