Submission #674045

# Submission time Handle Problem Language Result Execution time Memory
674045 2022-12-22T17:24:36 Z peijar Reconstruction Project (JOI22_reconstruction) C++17
100 / 100
2197 ms 40092 KB
#include <bits/stdc++.h>
#define int long long
using namespace std;

const int INF = 1e18;

struct UnionFind {
  vector<int> id;

  UnionFind() {}
  UnionFind(int N) { id.assign(N, -1); }

  int find(int u) {
    if (id[u] < 0)
      return u;
    return id[u] = find(id[u]);
  }

  bool merge(int u, int v) {
    u = find(u), v = find(v);
    if (u == v)
      return false;
    if (id[u] > id[v])
      swap(u, v);
    id[u] += id[v];
    id[v] = u;
    return true;
  }
};

struct Edge {
  int u, v, w;

  Edge() {}

  Edge(int a, int b, int c) : u(a), v(b), w(c) {}

  bool operator<(Edge other) const {
    return tuple(w, u, v) < tuple(other.w, other.u, other.v);
  };
};

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

  int nbSommet, nbAretes;
  cin >> nbSommet >> nbAretes;

  vector<Edge> edges(nbAretes);
  for (auto &[u, v, w] : edges) {
    cin >> u >> v >> w;
    --u, --v;
  }

  sort(edges.begin(), edges.end());

  vector<int> debRight(nbAretes), finRight(nbAretes);
  vector<int> debLeft(nbAretes), finLeft(nbAretes);

  vector<int> usefulEdges;
  for (int iArete = nbAretes - 1; iArete >= 0; --iArete) {
    UnionFind dsu(nbSommet);
    int nxt = -1;
    for (int i : usefulEdges) {
      dsu.merge(edges[i].u, edges[i].v);
      if (dsu.find(edges[iArete].u) == dsu.find(edges[iArete].v)) {
        nxt = i;
        break;
      }
    }

    debRight[iArete] = edges[iArete].w + 1;
    finRight[iArete] = nxt == -1 ? INF : (edges[iArete].w + edges[nxt].w) / 2;

    vector<int> nxtUseful;
    nxtUseful.push_back(iArete);
    for (int i : usefulEdges)
      if (i != nxt)
        nxtUseful.push_back(i);
    usefulEdges = nxtUseful;
  }

  usefulEdges.clear();
  for (int iArete = 0; iArete < nbAretes; ++iArete) {
    UnionFind dsu(nbSommet);
    int nxt = -1;
    for (int i : usefulEdges) {
      dsu.merge(edges[i].u, edges[i].v);
      if (dsu.find(edges[iArete].u) == dsu.find(edges[iArete].v)) {
        nxt = i;
        break;
      }
    }

    finLeft[iArete] = edges[iArete].w;
    debLeft[iArete] =
        nxt == -1 ? 0 : ((edges[iArete].w + edges[nxt].w) / 2 + 1);

    vector<int> nxtUseful;
    nxtUseful.push_back(iArete);
    for (int i : usefulEdges)
      if (i != nxt)
        nxtUseful.push_back(i);
    usefulEdges = nxtUseful;
  }

  vector<tuple<int, int, int>> eventsL, eventsR;
  for (int iArete = 0; iArete < nbAretes; ++iArete) {
    eventsL.emplace_back(debLeft[iArete], 1, iArete);
    eventsL.emplace_back(finLeft[iArete] + 1, -1, iArete);
    eventsR.emplace_back(debRight[iArete], 1, iArete);
    eventsR.emplace_back(finRight[iArete] + 1, -1, iArete);
  }
  sort(eventsL.begin(), eventsL.end());
  sort(eventsR.begin(), eventsR.end());

  int mult = 0, add = 0;

  int posL = 0, posR = 0;
  int nbRequetes;
  cin >> nbRequetes;
  for (int iRequete = 0; iRequete < nbRequetes; ++iRequete) {
    int x;
    cin >> x;
    while (posL < (int)eventsL.size() and get<0>(eventsL[posL]) <= x) {
      auto [d, delta, iArete] = eventsL[posL++];
      if (delta == 1)
        mult--, add += edges[iArete].w;
      else
        mult++, add -= edges[iArete].w;
    }
    while (posR < (int)eventsR.size() and get<0>(eventsR[posR]) <= x) {
      auto [d, delta, iArete] = eventsR[posR++];
      if (delta == 1)
        mult++, add -= edges[iArete].w;
      else
        mult--, add += edges[iArete].w;
    }
    cout << x * mult + add << '\n';
  }
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 0 ms 316 KB Output is correct
15 Correct 1 ms 324 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 0 ms 316 KB Output is correct
15 Correct 1 ms 324 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 1985 ms 17144 KB Output is correct
21 Correct 1064 ms 17028 KB Output is correct
22 Correct 1125 ms 17136 KB Output is correct
23 Correct 1231 ms 17244 KB Output is correct
24 Correct 1596 ms 17032 KB Output is correct
25 Correct 1962 ms 17036 KB Output is correct
26 Correct 2047 ms 17136 KB Output is correct
27 Correct 2004 ms 17028 KB Output is correct
28 Correct 1977 ms 17036 KB Output is correct
29 Correct 977 ms 17040 KB Output is correct
30 Correct 1980 ms 17164 KB Output is correct
31 Correct 2000 ms 17028 KB Output is correct
32 Correct 2011 ms 17032 KB Output is correct
33 Correct 1999 ms 17032 KB Output is correct
34 Correct 880 ms 17320 KB Output is correct
35 Correct 1997 ms 17136 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1447 ms 37808 KB Output is correct
5 Correct 1419 ms 37808 KB Output is correct
6 Correct 1421 ms 37840 KB Output is correct
7 Correct 688 ms 39756 KB Output is correct
8 Correct 548 ms 39876 KB Output is correct
9 Correct 568 ms 39896 KB Output is correct
10 Correct 1416 ms 37916 KB Output is correct
11 Correct 553 ms 40092 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 0 ms 316 KB Output is correct
15 Correct 1 ms 324 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 194 ms 22468 KB Output is correct
21 Correct 190 ms 22636 KB Output is correct
22 Correct 209 ms 22540 KB Output is correct
23 Correct 200 ms 22588 KB Output is correct
24 Correct 191 ms 22544 KB Output is correct
25 Correct 199 ms 22564 KB Output is correct
26 Correct 199 ms 22528 KB Output is correct
27 Correct 195 ms 22772 KB Output is correct
28 Correct 207 ms 22512 KB Output is correct
29 Correct 202 ms 22628 KB Output is correct
30 Correct 197 ms 22704 KB Output is correct
31 Correct 195 ms 22360 KB Output is correct
32 Correct 190 ms 23112 KB Output is correct
33 Correct 199 ms 22440 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 0 ms 316 KB Output is correct
15 Correct 1 ms 324 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 1985 ms 17144 KB Output is correct
21 Correct 1064 ms 17028 KB Output is correct
22 Correct 1125 ms 17136 KB Output is correct
23 Correct 1231 ms 17244 KB Output is correct
24 Correct 1596 ms 17032 KB Output is correct
25 Correct 1962 ms 17036 KB Output is correct
26 Correct 2047 ms 17136 KB Output is correct
27 Correct 2004 ms 17028 KB Output is correct
28 Correct 1977 ms 17036 KB Output is correct
29 Correct 977 ms 17040 KB Output is correct
30 Correct 1980 ms 17164 KB Output is correct
31 Correct 2000 ms 17028 KB Output is correct
32 Correct 2011 ms 17032 KB Output is correct
33 Correct 1999 ms 17032 KB Output is correct
34 Correct 880 ms 17320 KB Output is correct
35 Correct 1997 ms 17136 KB Output is correct
36 Correct 1999 ms 17472 KB Output is correct
37 Correct 1066 ms 17460 KB Output is correct
38 Correct 1094 ms 17472 KB Output is correct
39 Correct 1267 ms 17468 KB Output is correct
40 Correct 1625 ms 17464 KB Output is correct
41 Correct 1916 ms 17600 KB Output is correct
42 Correct 1973 ms 17468 KB Output is correct
43 Correct 2024 ms 17476 KB Output is correct
44 Correct 1994 ms 17476 KB Output is correct
45 Correct 977 ms 17568 KB Output is correct
46 Correct 1980 ms 17468 KB Output is correct
47 Correct 2004 ms 17476 KB Output is correct
48 Correct 1991 ms 17464 KB Output is correct
49 Correct 2001 ms 17552 KB Output is correct
50 Correct 900 ms 17792 KB Output is correct
51 Correct 1969 ms 17580 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 0 ms 316 KB Output is correct
15 Correct 1 ms 324 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 1985 ms 17144 KB Output is correct
21 Correct 1064 ms 17028 KB Output is correct
22 Correct 1125 ms 17136 KB Output is correct
23 Correct 1231 ms 17244 KB Output is correct
24 Correct 1596 ms 17032 KB Output is correct
25 Correct 1962 ms 17036 KB Output is correct
26 Correct 2047 ms 17136 KB Output is correct
27 Correct 2004 ms 17028 KB Output is correct
28 Correct 1977 ms 17036 KB Output is correct
29 Correct 977 ms 17040 KB Output is correct
30 Correct 1980 ms 17164 KB Output is correct
31 Correct 2000 ms 17028 KB Output is correct
32 Correct 2011 ms 17032 KB Output is correct
33 Correct 1999 ms 17032 KB Output is correct
34 Correct 880 ms 17320 KB Output is correct
35 Correct 1997 ms 17136 KB Output is correct
36 Correct 1 ms 212 KB Output is correct
37 Correct 0 ms 212 KB Output is correct
38 Correct 1 ms 212 KB Output is correct
39 Correct 1447 ms 37808 KB Output is correct
40 Correct 1419 ms 37808 KB Output is correct
41 Correct 1421 ms 37840 KB Output is correct
42 Correct 688 ms 39756 KB Output is correct
43 Correct 548 ms 39876 KB Output is correct
44 Correct 568 ms 39896 KB Output is correct
45 Correct 1416 ms 37916 KB Output is correct
46 Correct 553 ms 40092 KB Output is correct
47 Correct 0 ms 212 KB Output is correct
48 Correct 194 ms 22468 KB Output is correct
49 Correct 190 ms 22636 KB Output is correct
50 Correct 209 ms 22540 KB Output is correct
51 Correct 200 ms 22588 KB Output is correct
52 Correct 191 ms 22544 KB Output is correct
53 Correct 199 ms 22564 KB Output is correct
54 Correct 199 ms 22528 KB Output is correct
55 Correct 195 ms 22772 KB Output is correct
56 Correct 207 ms 22512 KB Output is correct
57 Correct 202 ms 22628 KB Output is correct
58 Correct 197 ms 22704 KB Output is correct
59 Correct 195 ms 22360 KB Output is correct
60 Correct 190 ms 23112 KB Output is correct
61 Correct 199 ms 22440 KB Output is correct
62 Correct 1999 ms 17472 KB Output is correct
63 Correct 1066 ms 17460 KB Output is correct
64 Correct 1094 ms 17472 KB Output is correct
65 Correct 1267 ms 17468 KB Output is correct
66 Correct 1625 ms 17464 KB Output is correct
67 Correct 1916 ms 17600 KB Output is correct
68 Correct 1973 ms 17468 KB Output is correct
69 Correct 2024 ms 17476 KB Output is correct
70 Correct 1994 ms 17476 KB Output is correct
71 Correct 977 ms 17568 KB Output is correct
72 Correct 1980 ms 17468 KB Output is correct
73 Correct 2004 ms 17476 KB Output is correct
74 Correct 1991 ms 17464 KB Output is correct
75 Correct 2001 ms 17552 KB Output is correct
76 Correct 900 ms 17792 KB Output is correct
77 Correct 1969 ms 17580 KB Output is correct
78 Correct 2197 ms 36764 KB Output is correct
79 Correct 1251 ms 38844 KB Output is correct
80 Correct 1280 ms 37864 KB Output is correct
81 Correct 1461 ms 37832 KB Output is correct
82 Correct 1746 ms 37024 KB Output is correct
83 Correct 2055 ms 36852 KB Output is correct
84 Correct 2140 ms 36856 KB Output is correct
85 Correct 2125 ms 36752 KB Output is correct
86 Correct 2113 ms 36740 KB Output is correct
87 Correct 1121 ms 38416 KB Output is correct
88 Correct 2115 ms 36804 KB Output is correct
89 Correct 2109 ms 36884 KB Output is correct
90 Correct 2111 ms 36920 KB Output is correct
91 Correct 2108 ms 36752 KB Output is correct
92 Correct 1022 ms 39744 KB Output is correct
93 Correct 2106 ms 37940 KB Output is correct