Submission #953846

# Submission time Handle Problem Language Result Execution time Memory
953846 2024-03-26T17:59:49 Z EJIC_B_KEDAX Reconstruction Project (JOI22_reconstruction) C++17
100 / 100
1317 ms 60956 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;
                break;
            } 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 7 ms 35160 KB Output is correct
2 Correct 7 ms 35164 KB Output is correct
3 Correct 8 ms 35160 KB Output is correct
4 Correct 7 ms 35160 KB Output is correct
5 Correct 7 ms 35164 KB Output is correct
6 Correct 7 ms 35324 KB Output is correct
7 Correct 7 ms 35164 KB Output is correct
8 Correct 7 ms 35164 KB Output is correct
9 Correct 7 ms 35284 KB Output is correct
10 Correct 6 ms 35188 KB Output is correct
11 Correct 6 ms 35160 KB Output is correct
12 Correct 8 ms 35164 KB Output is correct
13 Correct 7 ms 35164 KB Output is correct
14 Correct 7 ms 35164 KB Output is correct
15 Correct 6 ms 35164 KB Output is correct
16 Correct 7 ms 35176 KB Output is correct
17 Correct 6 ms 35268 KB Output is correct
18 Correct 7 ms 35164 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 35160 KB Output is correct
2 Correct 7 ms 35164 KB Output is correct
3 Correct 8 ms 35160 KB Output is correct
4 Correct 7 ms 35160 KB Output is correct
5 Correct 7 ms 35164 KB Output is correct
6 Correct 7 ms 35324 KB Output is correct
7 Correct 7 ms 35164 KB Output is correct
8 Correct 7 ms 35164 KB Output is correct
9 Correct 7 ms 35284 KB Output is correct
10 Correct 6 ms 35188 KB Output is correct
11 Correct 6 ms 35160 KB Output is correct
12 Correct 8 ms 35164 KB Output is correct
13 Correct 7 ms 35164 KB Output is correct
14 Correct 7 ms 35164 KB Output is correct
15 Correct 6 ms 35164 KB Output is correct
16 Correct 7 ms 35176 KB Output is correct
17 Correct 6 ms 35268 KB Output is correct
18 Correct 7 ms 35164 KB Output is correct
19 Correct 7 ms 35160 KB Output is correct
20 Correct 974 ms 37884 KB Output is correct
21 Correct 445 ms 37888 KB Output is correct
22 Correct 475 ms 37896 KB Output is correct
23 Correct 521 ms 37892 KB Output is correct
24 Correct 698 ms 37896 KB Output is correct
25 Correct 1018 ms 37824 KB Output is correct
26 Correct 972 ms 37716 KB Output is correct
27 Correct 969 ms 37892 KB Output is correct
28 Correct 996 ms 37892 KB Output is correct
29 Correct 890 ms 37892 KB Output is correct
30 Correct 992 ms 37892 KB Output is correct
31 Correct 966 ms 38056 KB Output is correct
32 Correct 970 ms 37972 KB Output is correct
33 Correct 974 ms 37892 KB Output is correct
34 Correct 791 ms 38148 KB Output is correct
35 Correct 972 ms 37972 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 35164 KB Output is correct
2 Correct 7 ms 35248 KB Output is correct
3 Correct 6 ms 35164 KB Output is correct
4 Correct 683 ms 58540 KB Output is correct
5 Correct 678 ms 58448 KB Output is correct
6 Correct 676 ms 58368 KB Output is correct
7 Correct 432 ms 60300 KB Output is correct
8 Correct 380 ms 60496 KB Output is correct
9 Correct 351 ms 60500 KB Output is correct
10 Correct 624 ms 58544 KB Output is correct
11 Correct 343 ms 60500 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 35160 KB Output is correct
2 Correct 7 ms 35164 KB Output is correct
3 Correct 8 ms 35160 KB Output is correct
4 Correct 7 ms 35160 KB Output is correct
5 Correct 7 ms 35164 KB Output is correct
6 Correct 7 ms 35324 KB Output is correct
7 Correct 7 ms 35164 KB Output is correct
8 Correct 7 ms 35164 KB Output is correct
9 Correct 7 ms 35284 KB Output is correct
10 Correct 6 ms 35188 KB Output is correct
11 Correct 6 ms 35160 KB Output is correct
12 Correct 8 ms 35164 KB Output is correct
13 Correct 7 ms 35164 KB Output is correct
14 Correct 7 ms 35164 KB Output is correct
15 Correct 6 ms 35164 KB Output is correct
16 Correct 7 ms 35176 KB Output is correct
17 Correct 6 ms 35268 KB Output is correct
18 Correct 7 ms 35164 KB Output is correct
19 Correct 6 ms 35164 KB Output is correct
20 Correct 173 ms 57552 KB Output is correct
21 Correct 166 ms 57428 KB Output is correct
22 Correct 166 ms 57408 KB Output is correct
23 Correct 171 ms 57364 KB Output is correct
24 Correct 168 ms 57428 KB Output is correct
25 Correct 167 ms 57436 KB Output is correct
26 Correct 169 ms 57308 KB Output is correct
27 Correct 165 ms 57428 KB Output is correct
28 Correct 167 ms 57428 KB Output is correct
29 Correct 169 ms 57424 KB Output is correct
30 Correct 174 ms 57432 KB Output is correct
31 Correct 167 ms 57436 KB Output is correct
32 Correct 168 ms 57980 KB Output is correct
33 Correct 171 ms 57304 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 35160 KB Output is correct
2 Correct 7 ms 35164 KB Output is correct
3 Correct 8 ms 35160 KB Output is correct
4 Correct 7 ms 35160 KB Output is correct
5 Correct 7 ms 35164 KB Output is correct
6 Correct 7 ms 35324 KB Output is correct
7 Correct 7 ms 35164 KB Output is correct
8 Correct 7 ms 35164 KB Output is correct
9 Correct 7 ms 35284 KB Output is correct
10 Correct 6 ms 35188 KB Output is correct
11 Correct 6 ms 35160 KB Output is correct
12 Correct 8 ms 35164 KB Output is correct
13 Correct 7 ms 35164 KB Output is correct
14 Correct 7 ms 35164 KB Output is correct
15 Correct 6 ms 35164 KB Output is correct
16 Correct 7 ms 35176 KB Output is correct
17 Correct 6 ms 35268 KB Output is correct
18 Correct 7 ms 35164 KB Output is correct
19 Correct 7 ms 35160 KB Output is correct
20 Correct 974 ms 37884 KB Output is correct
21 Correct 445 ms 37888 KB Output is correct
22 Correct 475 ms 37896 KB Output is correct
23 Correct 521 ms 37892 KB Output is correct
24 Correct 698 ms 37896 KB Output is correct
25 Correct 1018 ms 37824 KB Output is correct
26 Correct 972 ms 37716 KB Output is correct
27 Correct 969 ms 37892 KB Output is correct
28 Correct 996 ms 37892 KB Output is correct
29 Correct 890 ms 37892 KB Output is correct
30 Correct 992 ms 37892 KB Output is correct
31 Correct 966 ms 38056 KB Output is correct
32 Correct 970 ms 37972 KB Output is correct
33 Correct 974 ms 37892 KB Output is correct
34 Correct 791 ms 38148 KB Output is correct
35 Correct 972 ms 37972 KB Output is correct
36 Correct 1008 ms 38224 KB Output is correct
37 Correct 497 ms 38220 KB Output is correct
38 Correct 510 ms 38224 KB Output is correct
39 Correct 565 ms 38220 KB Output is correct
40 Correct 745 ms 38224 KB Output is correct
41 Correct 1074 ms 38224 KB Output is correct
42 Correct 1024 ms 38236 KB Output is correct
43 Correct 1008 ms 38224 KB Output is correct
44 Correct 1039 ms 37972 KB Output is correct
45 Correct 928 ms 38340 KB Output is correct
46 Correct 1038 ms 38332 KB Output is correct
47 Correct 1060 ms 38336 KB Output is correct
48 Correct 1041 ms 38328 KB Output is correct
49 Correct 1020 ms 38324 KB Output is correct
50 Correct 816 ms 38452 KB Output is correct
51 Correct 1035 ms 38484 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 35160 KB Output is correct
2 Correct 7 ms 35164 KB Output is correct
3 Correct 8 ms 35160 KB Output is correct
4 Correct 7 ms 35160 KB Output is correct
5 Correct 7 ms 35164 KB Output is correct
6 Correct 7 ms 35324 KB Output is correct
7 Correct 7 ms 35164 KB Output is correct
8 Correct 7 ms 35164 KB Output is correct
9 Correct 7 ms 35284 KB Output is correct
10 Correct 6 ms 35188 KB Output is correct
11 Correct 6 ms 35160 KB Output is correct
12 Correct 8 ms 35164 KB Output is correct
13 Correct 7 ms 35164 KB Output is correct
14 Correct 7 ms 35164 KB Output is correct
15 Correct 6 ms 35164 KB Output is correct
16 Correct 7 ms 35176 KB Output is correct
17 Correct 6 ms 35268 KB Output is correct
18 Correct 7 ms 35164 KB Output is correct
19 Correct 7 ms 35160 KB Output is correct
20 Correct 974 ms 37884 KB Output is correct
21 Correct 445 ms 37888 KB Output is correct
22 Correct 475 ms 37896 KB Output is correct
23 Correct 521 ms 37892 KB Output is correct
24 Correct 698 ms 37896 KB Output is correct
25 Correct 1018 ms 37824 KB Output is correct
26 Correct 972 ms 37716 KB Output is correct
27 Correct 969 ms 37892 KB Output is correct
28 Correct 996 ms 37892 KB Output is correct
29 Correct 890 ms 37892 KB Output is correct
30 Correct 992 ms 37892 KB Output is correct
31 Correct 966 ms 38056 KB Output is correct
32 Correct 970 ms 37972 KB Output is correct
33 Correct 974 ms 37892 KB Output is correct
34 Correct 791 ms 38148 KB Output is correct
35 Correct 972 ms 37972 KB Output is correct
36 Correct 6 ms 35164 KB Output is correct
37 Correct 7 ms 35248 KB Output is correct
38 Correct 6 ms 35164 KB Output is correct
39 Correct 683 ms 58540 KB Output is correct
40 Correct 678 ms 58448 KB Output is correct
41 Correct 676 ms 58368 KB Output is correct
42 Correct 432 ms 60300 KB Output is correct
43 Correct 380 ms 60496 KB Output is correct
44 Correct 351 ms 60500 KB Output is correct
45 Correct 624 ms 58544 KB Output is correct
46 Correct 343 ms 60500 KB Output is correct
47 Correct 6 ms 35164 KB Output is correct
48 Correct 173 ms 57552 KB Output is correct
49 Correct 166 ms 57428 KB Output is correct
50 Correct 166 ms 57408 KB Output is correct
51 Correct 171 ms 57364 KB Output is correct
52 Correct 168 ms 57428 KB Output is correct
53 Correct 167 ms 57436 KB Output is correct
54 Correct 169 ms 57308 KB Output is correct
55 Correct 165 ms 57428 KB Output is correct
56 Correct 167 ms 57428 KB Output is correct
57 Correct 169 ms 57424 KB Output is correct
58 Correct 174 ms 57432 KB Output is correct
59 Correct 167 ms 57436 KB Output is correct
60 Correct 168 ms 57980 KB Output is correct
61 Correct 171 ms 57304 KB Output is correct
62 Correct 1008 ms 38224 KB Output is correct
63 Correct 497 ms 38220 KB Output is correct
64 Correct 510 ms 38224 KB Output is correct
65 Correct 565 ms 38220 KB Output is correct
66 Correct 745 ms 38224 KB Output is correct
67 Correct 1074 ms 38224 KB Output is correct
68 Correct 1024 ms 38236 KB Output is correct
69 Correct 1008 ms 38224 KB Output is correct
70 Correct 1039 ms 37972 KB Output is correct
71 Correct 928 ms 38340 KB Output is correct
72 Correct 1038 ms 38332 KB Output is correct
73 Correct 1060 ms 38336 KB Output is correct
74 Correct 1041 ms 38328 KB Output is correct
75 Correct 1020 ms 38324 KB Output is correct
76 Correct 816 ms 38452 KB Output is correct
77 Correct 1035 ms 38484 KB Output is correct
78 Correct 1203 ms 59056 KB Output is correct
79 Correct 670 ms 60956 KB Output is correct
80 Correct 709 ms 59984 KB Output is correct
81 Correct 786 ms 60068 KB Output is correct
82 Correct 936 ms 59220 KB Output is correct
83 Correct 1317 ms 59052 KB Output is correct
84 Correct 1214 ms 59052 KB Output is correct
85 Correct 1266 ms 59272 KB Output is correct
86 Correct 1228 ms 59056 KB Output is correct
87 Correct 1091 ms 60596 KB Output is correct
88 Correct 1214 ms 59060 KB Output is correct
89 Correct 1228 ms 59056 KB Output is correct
90 Correct 1207 ms 59008 KB Output is correct
91 Correct 1182 ms 57264 KB Output is correct
92 Correct 972 ms 60336 KB Output is correct
93 Correct 1215 ms 58452 KB Output is correct