Submission #959395

# Submission time Handle Problem Language Result Execution time Memory
959395 2024-04-08T06:57:02 Z alextodoran Reconstruction Project (JOI22_reconstruction) C++17
100 / 100
955 ms 34468 KB
/**
 _  _   __  _ _ _  _  _ _
 |a  ||t  ||o    d | |o  |
| __    _| | _ | __|  _ |
| __ |/_  | __  /__\ / _\|

**/

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int N_MAX = 500;
const int M_MAX = 100000;
const int Q_MAX = 1000000;
const int INF = (int) 1e9 + 2;

int N, M, Q;
int edge_u[M_MAX + 2], edge_v[M_MAX + 2];
int edge_c[M_MAX + 2];

int order[M_MAX + 2];

int min_q[M_MAX + 2], max_q[M_MAX + 2];

int other (int i, int x) {
    return (x != edge_u[i] ? edge_u[i] : edge_v[i]);
}

vector <int> adj[N_MAX + 2];

vector <int> chain;
bool dfs (int u, int v, int last = 0) {
    if (u == v) {
        return true;
    }
    for (int i : adj[u]) {
        if (i != last) {
            if (dfs(other(i, u), v, i) == true) {
                chain.push_back(i);
                return true;
            }
        }
    }
    return false;
}
void add (int i) {
    int u = edge_u[i], v = edge_v[i];
    adj[u].push_back(i);
    adj[v].push_back(i);
}
void del (int i) {
    int u = edge_u[i], v = edge_v[i];
    adj[u].erase(find(adj[u].begin(), adj[u].end(), i));
    adj[v].erase(find(adj[v].begin(), adj[v].end(), i));
}

vector <pair <ll, int>> events_l, events_r;

int main () {
    ios_base::sync_with_stdio(false);
    cin.tie(0);

    cin >> N >> M;
    for (int i = 1; i <= M; i++) {
        cin >> edge_u[i] >> edge_v[i] >> edge_c[i];
    }
    iota(order + 1, order + M + 1, 1);
    sort(order + 1, order + M + 1, [&] (const int &i, const int &j) {
        return edge_c[i] < edge_c[j];
    });
    for (int k = 1; k <= M; k++) {
        int i = order[k]; int u = edge_u[i], v = edge_v[i];
        if (dfs(u, v) == true) {
            int min_j = -1;
            for (int j : chain) {
                if (min_j == -1 || edge_c[j] < edge_c[min_j]) {
                    min_j = j;
                }
            }
            del(min_j);
            min_q[i] = (edge_c[min_j] + edge_c[i]) / 2 + 1;
            chain.clear();
        } else {
            min_q[i] = 1;
        }
        add(i);
    }
    for (int u = 1; u <= N; u++) {
        adj[u].clear();
    }
    for (int k = M; k >= 1; k--) {
        int i = order[k]; int u = edge_u[i], v = edge_v[i];
        if (dfs(u, v) == true) {
            int max_j = -1;
            for (int j : chain) {
                if (max_j == -1 || edge_c[j] > edge_c[max_j]) {
                    max_j = j;
                }
            }
            del(max_j);
            max_q[i] = (edge_c[max_j] + edge_c[i]) / 2;
            chain.clear();
        } else {
            max_q[i] = INF;
        }
        add(i);
    }
    for (int i = 1; i <= M; i++) {
        events_l.push_back({min_q[i], edge_c[i]});
        events_l.push_back({edge_c[i] + 1, -edge_c[i]});
        events_r.push_back({edge_c[i], edge_c[i]});
        events_r.push_back({max_q[i] + 1, -edge_c[i]});
    }
    sort(events_l.begin(), events_l.end(), greater <pair <int, int>> ());
    sort(events_r.begin(), events_r.end(), greater <pair <int, int>> ());
    ll sum_l = 0, sum_r = 0;
    int cnt_l = 0, cnt_r = 0;
    cin >> Q;
    while (Q--) {
        int X;
        cin >> X;
        while (events_l.empty() == false && events_l.back().first <= X) {
            sum_l += events_l.back().second;
            cnt_l += (events_l.back().second > 0 ? +1 : -1);
            events_l.pop_back();
        }
        while (events_r.empty() == false && events_r.back().first <= X) {
            sum_r += events_r.back().second;
            cnt_r += (events_r.back().second > 0 ? +1 : -1);
            events_r.pop_back();
        }
        cout << (sum_l - (ll) cnt_l * X) + ((ll) cnt_r * X - sum_r) << "\n";
    }

    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 1 ms 2396 KB Output is correct
4 Correct 1 ms 2396 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Correct 1 ms 2396 KB Output is correct
7 Correct 1 ms 2524 KB Output is correct
8 Correct 1 ms 2392 KB Output is correct
9 Correct 1 ms 2400 KB Output is correct
10 Correct 1 ms 2400 KB Output is correct
11 Correct 1 ms 2652 KB Output is correct
12 Correct 5 ms 2524 KB Output is correct
13 Correct 1 ms 2404 KB Output is correct
14 Correct 1 ms 2400 KB Output is correct
15 Correct 1 ms 2396 KB Output is correct
16 Correct 1 ms 2396 KB Output is correct
17 Correct 1 ms 2392 KB Output is correct
18 Correct 1 ms 2392 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 1 ms 2396 KB Output is correct
4 Correct 1 ms 2396 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Correct 1 ms 2396 KB Output is correct
7 Correct 1 ms 2524 KB Output is correct
8 Correct 1 ms 2392 KB Output is correct
9 Correct 1 ms 2400 KB Output is correct
10 Correct 1 ms 2400 KB Output is correct
11 Correct 1 ms 2652 KB Output is correct
12 Correct 5 ms 2524 KB Output is correct
13 Correct 1 ms 2404 KB Output is correct
14 Correct 1 ms 2400 KB Output is correct
15 Correct 1 ms 2396 KB Output is correct
16 Correct 1 ms 2396 KB Output is correct
17 Correct 1 ms 2392 KB Output is correct
18 Correct 1 ms 2392 KB Output is correct
19 Correct 1 ms 2396 KB Output is correct
20 Correct 761 ms 12072 KB Output is correct
21 Correct 447 ms 13348 KB Output is correct
22 Correct 663 ms 12660 KB Output is correct
23 Correct 660 ms 11816 KB Output is correct
24 Correct 644 ms 13236 KB Output is correct
25 Correct 716 ms 11900 KB Output is correct
26 Correct 739 ms 12320 KB Output is correct
27 Correct 783 ms 13476 KB Output is correct
28 Correct 779 ms 13600 KB Output is correct
29 Correct 616 ms 12408 KB Output is correct
30 Correct 747 ms 12720 KB Output is correct
31 Correct 725 ms 13428 KB Output is correct
32 Correct 826 ms 12328 KB Output is correct
33 Correct 726 ms 13004 KB Output is correct
34 Correct 532 ms 13492 KB Output is correct
35 Correct 743 ms 11556 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2392 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 1 ms 2396 KB Output is correct
4 Correct 591 ms 32676 KB Output is correct
5 Correct 634 ms 32564 KB Output is correct
6 Correct 590 ms 31824 KB Output is correct
7 Correct 245 ms 33992 KB Output is correct
8 Correct 234 ms 34468 KB Output is correct
9 Correct 237 ms 33760 KB Output is correct
10 Correct 569 ms 32076 KB Output is correct
11 Correct 250 ms 33708 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 1 ms 2396 KB Output is correct
4 Correct 1 ms 2396 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Correct 1 ms 2396 KB Output is correct
7 Correct 1 ms 2524 KB Output is correct
8 Correct 1 ms 2392 KB Output is correct
9 Correct 1 ms 2400 KB Output is correct
10 Correct 1 ms 2400 KB Output is correct
11 Correct 1 ms 2652 KB Output is correct
12 Correct 5 ms 2524 KB Output is correct
13 Correct 1 ms 2404 KB Output is correct
14 Correct 1 ms 2400 KB Output is correct
15 Correct 1 ms 2396 KB Output is correct
16 Correct 1 ms 2396 KB Output is correct
17 Correct 1 ms 2392 KB Output is correct
18 Correct 1 ms 2392 KB Output is correct
19 Correct 1 ms 2392 KB Output is correct
20 Correct 150 ms 21216 KB Output is correct
21 Correct 155 ms 24476 KB Output is correct
22 Correct 155 ms 24656 KB Output is correct
23 Correct 146 ms 24556 KB Output is correct
24 Correct 148 ms 24496 KB Output is correct
25 Correct 152 ms 24404 KB Output is correct
26 Correct 152 ms 24496 KB Output is correct
27 Correct 152 ms 24400 KB Output is correct
28 Correct 160 ms 24484 KB Output is correct
29 Correct 177 ms 24572 KB Output is correct
30 Correct 157 ms 24524 KB Output is correct
31 Correct 164 ms 24576 KB Output is correct
32 Correct 150 ms 24916 KB Output is correct
33 Correct 170 ms 24400 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 1 ms 2396 KB Output is correct
4 Correct 1 ms 2396 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Correct 1 ms 2396 KB Output is correct
7 Correct 1 ms 2524 KB Output is correct
8 Correct 1 ms 2392 KB Output is correct
9 Correct 1 ms 2400 KB Output is correct
10 Correct 1 ms 2400 KB Output is correct
11 Correct 1 ms 2652 KB Output is correct
12 Correct 5 ms 2524 KB Output is correct
13 Correct 1 ms 2404 KB Output is correct
14 Correct 1 ms 2400 KB Output is correct
15 Correct 1 ms 2396 KB Output is correct
16 Correct 1 ms 2396 KB Output is correct
17 Correct 1 ms 2392 KB Output is correct
18 Correct 1 ms 2392 KB Output is correct
19 Correct 1 ms 2396 KB Output is correct
20 Correct 761 ms 12072 KB Output is correct
21 Correct 447 ms 13348 KB Output is correct
22 Correct 663 ms 12660 KB Output is correct
23 Correct 660 ms 11816 KB Output is correct
24 Correct 644 ms 13236 KB Output is correct
25 Correct 716 ms 11900 KB Output is correct
26 Correct 739 ms 12320 KB Output is correct
27 Correct 783 ms 13476 KB Output is correct
28 Correct 779 ms 13600 KB Output is correct
29 Correct 616 ms 12408 KB Output is correct
30 Correct 747 ms 12720 KB Output is correct
31 Correct 725 ms 13428 KB Output is correct
32 Correct 826 ms 12328 KB Output is correct
33 Correct 726 ms 13004 KB Output is correct
34 Correct 532 ms 13492 KB Output is correct
35 Correct 743 ms 11556 KB Output is correct
36 Correct 773 ms 9688 KB Output is correct
37 Correct 466 ms 11632 KB Output is correct
38 Correct 665 ms 11224 KB Output is correct
39 Correct 670 ms 10616 KB Output is correct
40 Correct 624 ms 11696 KB Output is correct
41 Correct 728 ms 12412 KB Output is correct
42 Correct 744 ms 12504 KB Output is correct
43 Correct 740 ms 10356 KB Output is correct
44 Correct 780 ms 10364 KB Output is correct
45 Correct 637 ms 10612 KB Output is correct
46 Correct 732 ms 9856 KB Output is correct
47 Correct 772 ms 11184 KB Output is correct
48 Correct 757 ms 10200 KB Output is correct
49 Correct 728 ms 11132 KB Output is correct
50 Correct 520 ms 10612 KB Output is correct
51 Correct 751 ms 10968 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 1 ms 2396 KB Output is correct
4 Correct 1 ms 2396 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Correct 1 ms 2396 KB Output is correct
7 Correct 1 ms 2524 KB Output is correct
8 Correct 1 ms 2392 KB Output is correct
9 Correct 1 ms 2400 KB Output is correct
10 Correct 1 ms 2400 KB Output is correct
11 Correct 1 ms 2652 KB Output is correct
12 Correct 5 ms 2524 KB Output is correct
13 Correct 1 ms 2404 KB Output is correct
14 Correct 1 ms 2400 KB Output is correct
15 Correct 1 ms 2396 KB Output is correct
16 Correct 1 ms 2396 KB Output is correct
17 Correct 1 ms 2392 KB Output is correct
18 Correct 1 ms 2392 KB Output is correct
19 Correct 1 ms 2396 KB Output is correct
20 Correct 761 ms 12072 KB Output is correct
21 Correct 447 ms 13348 KB Output is correct
22 Correct 663 ms 12660 KB Output is correct
23 Correct 660 ms 11816 KB Output is correct
24 Correct 644 ms 13236 KB Output is correct
25 Correct 716 ms 11900 KB Output is correct
26 Correct 739 ms 12320 KB Output is correct
27 Correct 783 ms 13476 KB Output is correct
28 Correct 779 ms 13600 KB Output is correct
29 Correct 616 ms 12408 KB Output is correct
30 Correct 747 ms 12720 KB Output is correct
31 Correct 725 ms 13428 KB Output is correct
32 Correct 826 ms 12328 KB Output is correct
33 Correct 726 ms 13004 KB Output is correct
34 Correct 532 ms 13492 KB Output is correct
35 Correct 743 ms 11556 KB Output is correct
36 Correct 1 ms 2392 KB Output is correct
37 Correct 1 ms 2396 KB Output is correct
38 Correct 1 ms 2396 KB Output is correct
39 Correct 591 ms 32676 KB Output is correct
40 Correct 634 ms 32564 KB Output is correct
41 Correct 590 ms 31824 KB Output is correct
42 Correct 245 ms 33992 KB Output is correct
43 Correct 234 ms 34468 KB Output is correct
44 Correct 237 ms 33760 KB Output is correct
45 Correct 569 ms 32076 KB Output is correct
46 Correct 250 ms 33708 KB Output is correct
47 Correct 1 ms 2392 KB Output is correct
48 Correct 150 ms 21216 KB Output is correct
49 Correct 155 ms 24476 KB Output is correct
50 Correct 155 ms 24656 KB Output is correct
51 Correct 146 ms 24556 KB Output is correct
52 Correct 148 ms 24496 KB Output is correct
53 Correct 152 ms 24404 KB Output is correct
54 Correct 152 ms 24496 KB Output is correct
55 Correct 152 ms 24400 KB Output is correct
56 Correct 160 ms 24484 KB Output is correct
57 Correct 177 ms 24572 KB Output is correct
58 Correct 157 ms 24524 KB Output is correct
59 Correct 164 ms 24576 KB Output is correct
60 Correct 150 ms 24916 KB Output is correct
61 Correct 170 ms 24400 KB Output is correct
62 Correct 773 ms 9688 KB Output is correct
63 Correct 466 ms 11632 KB Output is correct
64 Correct 665 ms 11224 KB Output is correct
65 Correct 670 ms 10616 KB Output is correct
66 Correct 624 ms 11696 KB Output is correct
67 Correct 728 ms 12412 KB Output is correct
68 Correct 744 ms 12504 KB Output is correct
69 Correct 740 ms 10356 KB Output is correct
70 Correct 780 ms 10364 KB Output is correct
71 Correct 637 ms 10612 KB Output is correct
72 Correct 732 ms 9856 KB Output is correct
73 Correct 772 ms 11184 KB Output is correct
74 Correct 757 ms 10200 KB Output is correct
75 Correct 728 ms 11132 KB Output is correct
76 Correct 520 ms 10612 KB Output is correct
77 Correct 751 ms 10968 KB Output is correct
78 Correct 888 ms 30816 KB Output is correct
79 Correct 573 ms 33100 KB Output is correct
80 Correct 804 ms 31880 KB Output is correct
81 Correct 787 ms 32232 KB Output is correct
82 Correct 790 ms 31600 KB Output is correct
83 Correct 860 ms 30936 KB Output is correct
84 Correct 885 ms 31292 KB Output is correct
85 Correct 886 ms 30544 KB Output is correct
86 Correct 955 ms 31200 KB Output is correct
87 Correct 819 ms 33072 KB Output is correct
88 Correct 903 ms 30648 KB Output is correct
89 Correct 882 ms 31500 KB Output is correct
90 Correct 903 ms 30776 KB Output is correct
91 Correct 906 ms 30432 KB Output is correct
92 Correct 674 ms 33572 KB Output is correct
93 Correct 894 ms 31780 KB Output is correct