답안 #678877

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
678877 2023-01-06T20:19:07 Z elkernos Reconstruction Project (JOI22_reconstruction) C++17
100 / 100
2054 ms 29748 KB
#include <bits/stdc++.h>
 
using namespace std;

struct E {
  int a, b, w;
  bool operator<(const E &he) const { return w < he.w; }
};
 struct UF {
   vector<int> par;
   UF(int sz) : par(sz, -1) {}
   int root(int i) { return par[i] < 0 ? i : par[i] = root(par[i]); }
   bool join(int a, int b)
   {
     a = root(a), b = root(b);
     if (a == b) return false;
     if (par[a] > par[b]) swap(a, b);
     par[a] += par[b];
     par[b] = a;
     return true;
   }
   bool same(int a, int b) { return root(a) == root(b); }
 };

int32_t main()
{
    cin.tie(0)->sync_with_stdio(0);
    int n, m;
    cin >> n >> m;
    vector<E> edges(m);
    for (auto &[a, b, w] : edges) {
        cin >> a >> b >> w;
    }
    sort(edges.begin(), edges.end());
    vector<pair<int, pair<int, int>>> dod;
    for (int i = 0; i < m; i++) {
        int l_when, r_when;
        {
            int l = i;
            UF uf(n + 1);
            for (int j = i - 1; j >= 0; j--) {
                uf.join(edges[j].a, edges[j].b);
                if (uf.same(edges[i].a, edges[i].b)) {
                    l = j;
                    break;
                }
            }
            l_when = (l == i ? -1 : edges[i].w - (edges[i].w - edges[l].w) / 2);
        }
        {
            int r = i;
            UF uf(n + 1);
            for (int j = i + 1; j < m; j++) {
                uf.join(edges[j].a, edges[j].b);
                if (uf.same(edges[i].a, edges[i].b)) {
                    r = j;
                    break;
                }
            }
            r_when = (r == i ? 2e9 : edges[i].w + (edges[r].w - edges[i].w + 1) / 2);
        }
        //(0 -> edges[i].c - x)
        //(edges[i].c - x -> x - edges[i].c)
        //(x - edges[i].c -> 0)
        dod.push_back({l_when, {-1, +edges[i].w}});
        dod.push_back({edges[i].w, {+2, -2 * edges[i].w}});
        dod.push_back({r_when, {-1, +edges[i].w}});
    }
    sort(dod.begin(), dod.end());
    int q;
    cin >> q;
    int p = 0;
    int64_t b = 0;
    int a = 0;
    while (q--) {
        int x;
        cin >> x;
        while (p < (int)dod.size() && dod[p].first <= x) {
            a += dod[p].second.first;
            b += dod[p].second.second;
            p++;
        }
        cout << (int64_t)a * x + b << '\n';
    }
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 320 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 316 KB Output is correct
8 Correct 1 ms 320 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 1 ms 324 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 0 ms 316 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 320 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 316 KB Output is correct
8 Correct 1 ms 320 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 1 ms 324 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 0 ms 316 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 1757 ms 9468 KB Output is correct
21 Correct 1282 ms 9428 KB Output is correct
22 Correct 1348 ms 9480 KB Output is correct
23 Correct 1484 ms 9456 KB Output is correct
24 Correct 1688 ms 9500 KB Output is correct
25 Correct 1722 ms 9576 KB Output is correct
26 Correct 1759 ms 9444 KB Output is correct
27 Correct 1754 ms 9444 KB Output is correct
28 Correct 1741 ms 9440 KB Output is correct
29 Correct 1747 ms 9452 KB Output is correct
30 Correct 1750 ms 9648 KB Output is correct
31 Correct 1767 ms 9660 KB Output is correct
32 Correct 1766 ms 9620 KB Output is correct
33 Correct 1759 ms 9560 KB Output is correct
34 Correct 1760 ms 9676 KB Output is correct
35 Correct 1757 ms 9444 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 320 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1655 ms 27664 KB Output is correct
5 Correct 1645 ms 27628 KB Output is correct
6 Correct 1646 ms 27568 KB Output is correct
7 Correct 916 ms 29532 KB Output is correct
8 Correct 703 ms 29728 KB Output is correct
9 Correct 635 ms 29732 KB Output is correct
10 Correct 1653 ms 27720 KB Output is correct
11 Correct 713 ms 29748 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 320 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 316 KB Output is correct
8 Correct 1 ms 320 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 1 ms 324 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 0 ms 316 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 205 ms 22404 KB Output is correct
21 Correct 197 ms 22412 KB Output is correct
22 Correct 198 ms 22348 KB Output is correct
23 Correct 199 ms 22316 KB Output is correct
24 Correct 202 ms 22404 KB Output is correct
25 Correct 203 ms 22284 KB Output is correct
26 Correct 203 ms 22432 KB Output is correct
27 Correct 210 ms 22372 KB Output is correct
28 Correct 200 ms 22236 KB Output is correct
29 Correct 206 ms 22304 KB Output is correct
30 Correct 203 ms 22520 KB Output is correct
31 Correct 223 ms 22136 KB Output is correct
32 Correct 211 ms 22988 KB Output is correct
33 Correct 198 ms 22208 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 320 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 316 KB Output is correct
8 Correct 1 ms 320 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 1 ms 324 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 0 ms 316 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 1757 ms 9468 KB Output is correct
21 Correct 1282 ms 9428 KB Output is correct
22 Correct 1348 ms 9480 KB Output is correct
23 Correct 1484 ms 9456 KB Output is correct
24 Correct 1688 ms 9500 KB Output is correct
25 Correct 1722 ms 9576 KB Output is correct
26 Correct 1759 ms 9444 KB Output is correct
27 Correct 1754 ms 9444 KB Output is correct
28 Correct 1741 ms 9440 KB Output is correct
29 Correct 1747 ms 9452 KB Output is correct
30 Correct 1750 ms 9648 KB Output is correct
31 Correct 1767 ms 9660 KB Output is correct
32 Correct 1766 ms 9620 KB Output is correct
33 Correct 1759 ms 9560 KB Output is correct
34 Correct 1760 ms 9676 KB Output is correct
35 Correct 1757 ms 9444 KB Output is correct
36 Correct 1763 ms 9748 KB Output is correct
37 Correct 1286 ms 9688 KB Output is correct
38 Correct 1359 ms 9764 KB Output is correct
39 Correct 1486 ms 9760 KB Output is correct
40 Correct 1706 ms 9744 KB Output is correct
41 Correct 1725 ms 9688 KB Output is correct
42 Correct 1749 ms 9628 KB Output is correct
43 Correct 1756 ms 9636 KB Output is correct
44 Correct 1759 ms 9760 KB Output is correct
45 Correct 1746 ms 9616 KB Output is correct
46 Correct 1752 ms 9736 KB Output is correct
47 Correct 1784 ms 9644 KB Output is correct
48 Correct 1785 ms 9680 KB Output is correct
49 Correct 1771 ms 9680 KB Output is correct
50 Correct 1746 ms 10012 KB Output is correct
51 Correct 1749 ms 9816 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 320 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 316 KB Output is correct
8 Correct 1 ms 320 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 1 ms 324 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 0 ms 316 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 1757 ms 9468 KB Output is correct
21 Correct 1282 ms 9428 KB Output is correct
22 Correct 1348 ms 9480 KB Output is correct
23 Correct 1484 ms 9456 KB Output is correct
24 Correct 1688 ms 9500 KB Output is correct
25 Correct 1722 ms 9576 KB Output is correct
26 Correct 1759 ms 9444 KB Output is correct
27 Correct 1754 ms 9444 KB Output is correct
28 Correct 1741 ms 9440 KB Output is correct
29 Correct 1747 ms 9452 KB Output is correct
30 Correct 1750 ms 9648 KB Output is correct
31 Correct 1767 ms 9660 KB Output is correct
32 Correct 1766 ms 9620 KB Output is correct
33 Correct 1759 ms 9560 KB Output is correct
34 Correct 1760 ms 9676 KB Output is correct
35 Correct 1757 ms 9444 KB Output is correct
36 Correct 0 ms 320 KB Output is correct
37 Correct 1 ms 212 KB Output is correct
38 Correct 0 ms 212 KB Output is correct
39 Correct 1655 ms 27664 KB Output is correct
40 Correct 1645 ms 27628 KB Output is correct
41 Correct 1646 ms 27568 KB Output is correct
42 Correct 916 ms 29532 KB Output is correct
43 Correct 703 ms 29728 KB Output is correct
44 Correct 635 ms 29732 KB Output is correct
45 Correct 1653 ms 27720 KB Output is correct
46 Correct 713 ms 29748 KB Output is correct
47 Correct 1 ms 212 KB Output is correct
48 Correct 205 ms 22404 KB Output is correct
49 Correct 197 ms 22412 KB Output is correct
50 Correct 198 ms 22348 KB Output is correct
51 Correct 199 ms 22316 KB Output is correct
52 Correct 202 ms 22404 KB Output is correct
53 Correct 203 ms 22284 KB Output is correct
54 Correct 203 ms 22432 KB Output is correct
55 Correct 210 ms 22372 KB Output is correct
56 Correct 200 ms 22236 KB Output is correct
57 Correct 206 ms 22304 KB Output is correct
58 Correct 203 ms 22520 KB Output is correct
59 Correct 223 ms 22136 KB Output is correct
60 Correct 211 ms 22988 KB Output is correct
61 Correct 198 ms 22208 KB Output is correct
62 Correct 1763 ms 9748 KB Output is correct
63 Correct 1286 ms 9688 KB Output is correct
64 Correct 1359 ms 9764 KB Output is correct
65 Correct 1486 ms 9760 KB Output is correct
66 Correct 1706 ms 9744 KB Output is correct
67 Correct 1725 ms 9688 KB Output is correct
68 Correct 1749 ms 9628 KB Output is correct
69 Correct 1756 ms 9636 KB Output is correct
70 Correct 1759 ms 9760 KB Output is correct
71 Correct 1746 ms 9616 KB Output is correct
72 Correct 1752 ms 9736 KB Output is correct
73 Correct 1784 ms 9644 KB Output is correct
74 Correct 1785 ms 9680 KB Output is correct
75 Correct 1771 ms 9680 KB Output is correct
76 Correct 1746 ms 10012 KB Output is correct
77 Correct 1749 ms 9816 KB Output is correct
78 Correct 1934 ms 26572 KB Output is correct
79 Correct 1451 ms 28704 KB Output is correct
80 Correct 1526 ms 27560 KB Output is correct
81 Correct 1686 ms 27740 KB Output is correct
82 Correct 1861 ms 26760 KB Output is correct
83 Correct 1897 ms 26820 KB Output is correct
84 Correct 1951 ms 26756 KB Output is correct
85 Correct 1956 ms 26668 KB Output is correct
86 Correct 1909 ms 26584 KB Output is correct
87 Correct 1931 ms 28156 KB Output is correct
88 Correct 1920 ms 26676 KB Output is correct
89 Correct 1918 ms 26692 KB Output is correct
90 Correct 1961 ms 26616 KB Output is correct
91 Correct 2054 ms 26524 KB Output is correct
92 Correct 1937 ms 29436 KB Output is correct
93 Correct 1939 ms 27640 KB Output is correct