Submission #953832

# Submission time Handle Problem Language Result Execution time Memory
953832 2024-03-26T17:51:58 Z EJIC_B_KEDAX Reconstruction Project (JOI22_reconstruction) C++17
100 / 100
1562 ms 63184 KB
#ifdef LOCAL
    #define _GLIBCXX_DEBUG
#endif
#include <bits/stdc++.h>

#ifndef LOCAL
    #pragma GCC optimize("O3")
    // #pragma GCC optimize("Ofast")
    // #pragma GCC optimize("unroll-loops")
    #pragma GCC target("avx,avx2,bmi,bmi2,popcnt,lzcnt")
#endif
using namespace std;
using ll = long long;
using ld = long double;
#define x first
#define y second
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()

mt19937_64 mt(time(0));

void solve();
void init();

int32_t main() {
#ifndef LOCAL
    cin.tie(nullptr)->sync_with_stdio(false);
#endif
    cout << fixed << setprecision(30);
    init();
    int t = 1;
    // cin >> t;
    while (t--) {
        solve();
    }
}

void init() {}

const int N = 505, M = 100100, K = 350, Q = 1 << 20;
pair<int, pair<int, int>> e[M];
int dsu[N], x[Q];

int get(int a) {
    return dsu[a] == a ? a : dsu[a] = get(dsu[a]);
}

void merge(int a, int b) {
    dsu[get(a)] = get(b);
}

struct segment_tree {
    vector<ll> st;
    int size;
    segment_tree(int sz = Q) {
        size = sz;
        st.resize(2 * sz, 0);
    }
    void add(int l, int r, ll v) {
        l += size;
        r += size;
        while (l <= r) {
            if (l & 1) {
                st[l++] += v;
            }
            if (~r & 1) {
                st[r--] += v;
            }
            l >>= 1;
            r >>= 1;
        }
    }
    ll get(int i) {
        ll res = 0;
        i += size;
        while (i) {
            res += st[i];
            i >>= 1;
        }
        return res;
    }
};

void solve() {
    int n, m;
    cin >> n >> m;
    for (int i = 0; i < m; i++) {
        cin >> e[i].y.x >> e[i].y.y >> e[i].x; e[i].y.x--; e[i].y.y--;
    }
    int q;
    cin >> q;
    for (int i = 0; i < q; i++) {
        cin >> x[i];
    }
    sort(e, e + m);
    vector<int> v;
    segment_tree ff, ss;
    for (int i = 0; i < m; i++) {
        for (int j = 0; j < n; j++) {
            dsu[j] = j;
        }
        int l = -1, ind = -1;
        v.push_back(i);
        for (int j = v.size() - 1; j >= 0; j--) {
            if (get(e[v[j]].y.x) == get(e[v[j]].y.y)) {
                l = v[j];
                ind = j;
            } else {
                merge(e[v[j]].y.x, e[v[j]].y.y);
            }
        }
        if (ind != -1) {
            v.erase(v.begin() + ind);
        }
        int left = -1, right = q;
        while (right - left > 1) {
            int mid = (left + right) / 2;
            if (x[mid] >= e[i].x) {
                right = mid;
            } else {
                left = mid;
            }
        }
        if (l == -1) {
            ff.add(0, left, e[i].x);
            ss.add(0, left, -1);
        } else {
            int key = e[i].x - ((e[i].x - e[l].x - 1) / 2);
            int r = left;
            left = -1, right = q;
            while (right - left > 1) {
                int mid = (left + right) / 2;
                if (x[mid] >= key) {
                    right = mid;
                } else {
                    left = mid;
                }
            }
            ff.add(right, r, e[i].x);
            ss.add(right, r, -1);
        }
    }
    v.clear();
    for (int i = m - 1; i >= 0; i--) {
        for (int j = 0; j < n; j++) {
            dsu[j] = j;
        }
        int l = -1, ind = -1;
        v.push_back(i);
        for (int j = v.size() - 1; j >= 0; j--) {
            if (get(e[v[j]].y.x) == get(e[v[j]].y.y)) {
                l = v[j];
                ind = j;
            } else {
                merge(e[v[j]].y.x, e[v[j]].y.y);
            }
        }
        if (ind != -1) {
            v.erase(v.begin() + ind);
        }
        int left = -1, right = q;
        while (right - left > 1) {
            int mid = (left + right) / 2;
            if (x[mid] > e[i].x) {
                right = mid;
            } else {
                left = mid;
            }
        }
        if (l == -1) {
            ff.add(right, q - 1, -e[i].x);
            ss.add(right, q - 1, 1);
        } else {
            int key = e[i].x + ((e[l].x - e[i].x) / 2);
            int r = right;
            left = -1, right = q;
            while (right - left > 1) {
                int mid = (left + right) / 2;
                if (x[mid] > key) {
                    right = mid;
                } else {
                    left = mid;
                }
            }
            ff.add(r, left, -e[i].x);
            ss.add(r, left, 1);
        }
    }
    for (int i = 0; i < q; i++) {
        cout << ff.get(i) + x[i] * ss.get(i) << '\n';
    }
}
# Verdict Execution time Memory Grader output
1 Correct 8 ms 35160 KB Output is correct
2 Correct 8 ms 35164 KB Output is correct
3 Correct 7 ms 35196 KB Output is correct
4 Correct 6 ms 35160 KB Output is correct
5 Correct 6 ms 35164 KB Output is correct
6 Correct 6 ms 35176 KB Output is correct
7 Correct 8 ms 35164 KB Output is correct
8 Correct 6 ms 35164 KB Output is correct
9 Correct 6 ms 35284 KB Output is correct
10 Correct 6 ms 35164 KB Output is correct
11 Correct 7 ms 35160 KB Output is correct
12 Correct 7 ms 35164 KB Output is correct
13 Correct 7 ms 35420 KB Output is correct
14 Correct 6 ms 35164 KB Output is correct
15 Correct 7 ms 35164 KB Output is correct
16 Correct 6 ms 35164 KB Output is correct
17 Correct 7 ms 35164 KB Output is correct
18 Correct 6 ms 35164 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 35160 KB Output is correct
2 Correct 8 ms 35164 KB Output is correct
3 Correct 7 ms 35196 KB Output is correct
4 Correct 6 ms 35160 KB Output is correct
5 Correct 6 ms 35164 KB Output is correct
6 Correct 6 ms 35176 KB Output is correct
7 Correct 8 ms 35164 KB Output is correct
8 Correct 6 ms 35164 KB Output is correct
9 Correct 6 ms 35284 KB Output is correct
10 Correct 6 ms 35164 KB Output is correct
11 Correct 7 ms 35160 KB Output is correct
12 Correct 7 ms 35164 KB Output is correct
13 Correct 7 ms 35420 KB Output is correct
14 Correct 6 ms 35164 KB Output is correct
15 Correct 7 ms 35164 KB Output is correct
16 Correct 6 ms 35164 KB Output is correct
17 Correct 7 ms 35164 KB Output is correct
18 Correct 6 ms 35164 KB Output is correct
19 Correct 7 ms 35248 KB Output is correct
20 Correct 1197 ms 38068 KB Output is correct
21 Correct 570 ms 38064 KB Output is correct
22 Correct 635 ms 38224 KB Output is correct
23 Correct 670 ms 38316 KB Output is correct
24 Correct 882 ms 38068 KB Output is correct
25 Correct 1315 ms 38076 KB Output is correct
26 Correct 1183 ms 38228 KB Output is correct
27 Correct 1168 ms 38228 KB Output is correct
28 Correct 1168 ms 38072 KB Output is correct
29 Correct 1209 ms 38080 KB Output is correct
30 Correct 1192 ms 38224 KB Output is correct
31 Correct 1205 ms 38068 KB Output is correct
32 Correct 1169 ms 38072 KB Output is correct
33 Correct 1177 ms 38068 KB Output is correct
34 Correct 986 ms 37968 KB Output is correct
35 Correct 1193 ms 38068 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 35164 KB Output is correct
2 Correct 6 ms 35164 KB Output is correct
3 Correct 7 ms 35164 KB Output is correct
4 Correct 799 ms 61008 KB Output is correct
5 Correct 790 ms 61044 KB Output is correct
6 Correct 833 ms 61148 KB Output is correct
7 Correct 534 ms 62684 KB Output is correct
8 Correct 518 ms 63184 KB Output is correct
9 Correct 469 ms 63064 KB Output is correct
10 Correct 745 ms 61080 KB Output is correct
11 Correct 466 ms 63120 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 35160 KB Output is correct
2 Correct 8 ms 35164 KB Output is correct
3 Correct 7 ms 35196 KB Output is correct
4 Correct 6 ms 35160 KB Output is correct
5 Correct 6 ms 35164 KB Output is correct
6 Correct 6 ms 35176 KB Output is correct
7 Correct 8 ms 35164 KB Output is correct
8 Correct 6 ms 35164 KB Output is correct
9 Correct 6 ms 35284 KB Output is correct
10 Correct 6 ms 35164 KB Output is correct
11 Correct 7 ms 35160 KB Output is correct
12 Correct 7 ms 35164 KB Output is correct
13 Correct 7 ms 35420 KB Output is correct
14 Correct 6 ms 35164 KB Output is correct
15 Correct 7 ms 35164 KB Output is correct
16 Correct 6 ms 35164 KB Output is correct
17 Correct 7 ms 35164 KB Output is correct
18 Correct 6 ms 35164 KB Output is correct
19 Correct 7 ms 35276 KB Output is correct
20 Correct 169 ms 59404 KB Output is correct
21 Correct 184 ms 59448 KB Output is correct
22 Correct 164 ms 59544 KB Output is correct
23 Correct 166 ms 59476 KB Output is correct
24 Correct 172 ms 59360 KB Output is correct
25 Correct 178 ms 59372 KB Output is correct
26 Correct 166 ms 59472 KB Output is correct
27 Correct 166 ms 59556 KB Output is correct
28 Correct 183 ms 59468 KB Output is correct
29 Correct 175 ms 59496 KB Output is correct
30 Correct 170 ms 59624 KB Output is correct
31 Correct 174 ms 59244 KB Output is correct
32 Correct 180 ms 60036 KB Output is correct
33 Correct 174 ms 59216 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 35160 KB Output is correct
2 Correct 8 ms 35164 KB Output is correct
3 Correct 7 ms 35196 KB Output is correct
4 Correct 6 ms 35160 KB Output is correct
5 Correct 6 ms 35164 KB Output is correct
6 Correct 6 ms 35176 KB Output is correct
7 Correct 8 ms 35164 KB Output is correct
8 Correct 6 ms 35164 KB Output is correct
9 Correct 6 ms 35284 KB Output is correct
10 Correct 6 ms 35164 KB Output is correct
11 Correct 7 ms 35160 KB Output is correct
12 Correct 7 ms 35164 KB Output is correct
13 Correct 7 ms 35420 KB Output is correct
14 Correct 6 ms 35164 KB Output is correct
15 Correct 7 ms 35164 KB Output is correct
16 Correct 6 ms 35164 KB Output is correct
17 Correct 7 ms 35164 KB Output is correct
18 Correct 6 ms 35164 KB Output is correct
19 Correct 7 ms 35248 KB Output is correct
20 Correct 1197 ms 38068 KB Output is correct
21 Correct 570 ms 38064 KB Output is correct
22 Correct 635 ms 38224 KB Output is correct
23 Correct 670 ms 38316 KB Output is correct
24 Correct 882 ms 38068 KB Output is correct
25 Correct 1315 ms 38076 KB Output is correct
26 Correct 1183 ms 38228 KB Output is correct
27 Correct 1168 ms 38228 KB Output is correct
28 Correct 1168 ms 38072 KB Output is correct
29 Correct 1209 ms 38080 KB Output is correct
30 Correct 1192 ms 38224 KB Output is correct
31 Correct 1205 ms 38068 KB Output is correct
32 Correct 1169 ms 38072 KB Output is correct
33 Correct 1177 ms 38068 KB Output is correct
34 Correct 986 ms 37968 KB Output is correct
35 Correct 1193 ms 38068 KB Output is correct
36 Correct 1258 ms 38400 KB Output is correct
37 Correct 613 ms 38336 KB Output is correct
38 Correct 724 ms 38484 KB Output is correct
39 Correct 721 ms 38332 KB Output is correct
40 Correct 951 ms 38336 KB Output is correct
41 Correct 1343 ms 38480 KB Output is correct
42 Correct 1256 ms 38336 KB Output is correct
43 Correct 1215 ms 38568 KB Output is correct
44 Correct 1213 ms 38480 KB Output is correct
45 Correct 1240 ms 38344 KB Output is correct
46 Correct 1191 ms 38484 KB Output is correct
47 Correct 1205 ms 38336 KB Output is correct
48 Correct 1201 ms 38336 KB Output is correct
49 Correct 1198 ms 38484 KB Output is correct
50 Correct 1006 ms 38740 KB Output is correct
51 Correct 1190 ms 38340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 35160 KB Output is correct
2 Correct 8 ms 35164 KB Output is correct
3 Correct 7 ms 35196 KB Output is correct
4 Correct 6 ms 35160 KB Output is correct
5 Correct 6 ms 35164 KB Output is correct
6 Correct 6 ms 35176 KB Output is correct
7 Correct 8 ms 35164 KB Output is correct
8 Correct 6 ms 35164 KB Output is correct
9 Correct 6 ms 35284 KB Output is correct
10 Correct 6 ms 35164 KB Output is correct
11 Correct 7 ms 35160 KB Output is correct
12 Correct 7 ms 35164 KB Output is correct
13 Correct 7 ms 35420 KB Output is correct
14 Correct 6 ms 35164 KB Output is correct
15 Correct 7 ms 35164 KB Output is correct
16 Correct 6 ms 35164 KB Output is correct
17 Correct 7 ms 35164 KB Output is correct
18 Correct 6 ms 35164 KB Output is correct
19 Correct 7 ms 35248 KB Output is correct
20 Correct 1197 ms 38068 KB Output is correct
21 Correct 570 ms 38064 KB Output is correct
22 Correct 635 ms 38224 KB Output is correct
23 Correct 670 ms 38316 KB Output is correct
24 Correct 882 ms 38068 KB Output is correct
25 Correct 1315 ms 38076 KB Output is correct
26 Correct 1183 ms 38228 KB Output is correct
27 Correct 1168 ms 38228 KB Output is correct
28 Correct 1168 ms 38072 KB Output is correct
29 Correct 1209 ms 38080 KB Output is correct
30 Correct 1192 ms 38224 KB Output is correct
31 Correct 1205 ms 38068 KB Output is correct
32 Correct 1169 ms 38072 KB Output is correct
33 Correct 1177 ms 38068 KB Output is correct
34 Correct 986 ms 37968 KB Output is correct
35 Correct 1193 ms 38068 KB Output is correct
36 Correct 6 ms 35164 KB Output is correct
37 Correct 6 ms 35164 KB Output is correct
38 Correct 7 ms 35164 KB Output is correct
39 Correct 799 ms 61008 KB Output is correct
40 Correct 790 ms 61044 KB Output is correct
41 Correct 833 ms 61148 KB Output is correct
42 Correct 534 ms 62684 KB Output is correct
43 Correct 518 ms 63184 KB Output is correct
44 Correct 469 ms 63064 KB Output is correct
45 Correct 745 ms 61080 KB Output is correct
46 Correct 466 ms 63120 KB Output is correct
47 Correct 7 ms 35276 KB Output is correct
48 Correct 169 ms 59404 KB Output is correct
49 Correct 184 ms 59448 KB Output is correct
50 Correct 164 ms 59544 KB Output is correct
51 Correct 166 ms 59476 KB Output is correct
52 Correct 172 ms 59360 KB Output is correct
53 Correct 178 ms 59372 KB Output is correct
54 Correct 166 ms 59472 KB Output is correct
55 Correct 166 ms 59556 KB Output is correct
56 Correct 183 ms 59468 KB Output is correct
57 Correct 175 ms 59496 KB Output is correct
58 Correct 170 ms 59624 KB Output is correct
59 Correct 174 ms 59244 KB Output is correct
60 Correct 180 ms 60036 KB Output is correct
61 Correct 174 ms 59216 KB Output is correct
62 Correct 1258 ms 38400 KB Output is correct
63 Correct 613 ms 38336 KB Output is correct
64 Correct 724 ms 38484 KB Output is correct
65 Correct 721 ms 38332 KB Output is correct
66 Correct 951 ms 38336 KB Output is correct
67 Correct 1343 ms 38480 KB Output is correct
68 Correct 1256 ms 38336 KB Output is correct
69 Correct 1215 ms 38568 KB Output is correct
70 Correct 1213 ms 38480 KB Output is correct
71 Correct 1240 ms 38344 KB Output is correct
72 Correct 1191 ms 38484 KB Output is correct
73 Correct 1205 ms 38336 KB Output is correct
74 Correct 1201 ms 38336 KB Output is correct
75 Correct 1198 ms 38484 KB Output is correct
76 Correct 1006 ms 38740 KB Output is correct
77 Correct 1190 ms 38340 KB Output is correct
78 Correct 1415 ms 60100 KB Output is correct
79 Correct 813 ms 62008 KB Output is correct
80 Correct 891 ms 60948 KB Output is correct
81 Correct 935 ms 60976 KB Output is correct
82 Correct 1143 ms 59988 KB Output is correct
83 Correct 1562 ms 59948 KB Output is correct
84 Correct 1411 ms 59824 KB Output is correct
85 Correct 1398 ms 59960 KB Output is correct
86 Correct 1368 ms 59952 KB Output is correct
87 Correct 1424 ms 61500 KB Output is correct
88 Correct 1394 ms 59984 KB Output is correct
89 Correct 1399 ms 59952 KB Output is correct
90 Correct 1380 ms 60080 KB Output is correct
91 Correct 1414 ms 59476 KB Output is correct
92 Correct 1171 ms 62804 KB Output is correct
93 Correct 1384 ms 60960 KB Output is correct