Submission #1038806

#TimeUsernameProblemLanguageResultExecution timeMemory
1038806NeroZeinReconstruction Project (JOI22_reconstruction)C++17
0 / 100
5067 ms76480 KiB
#include "bits/stdc++.h" using namespace std; #ifdef Nero #include "Deb.h" #else #define debug(...) #endif const int N = 502; int n, m; int p[N], sz[N]; map<int, vector<int>> mp; map<pair<int, int>, long long> ps; int get(int v) { return p[v] = (p[v] == v ? v : get(p[v])); } void unite(int u, int v) { u = get(u); v = get(v); if (sz[u] > sz[v]) { swap(u, v); } sz[v] += sz[u]; p[u] = v; } void simulate(int x, vector<array<int, 3>> e) { if (mp.count(x)) { return; } sort(e.begin(), e.end(), [&](array<int, 3>& i, array<int, 3>& j) { return abs(i[2] - x) < abs(j[2] - x); }); for (int i = 1; i <= n; ++i) { p[i] = i; sz[i] = 1; } //debug(x); long long res = 0; for (int i = 0; i < m; ++i) { auto [u, v, w] = e[i]; if (get(u) != get(v)) { unite(u, v); //debug(u, v, w); res += (long long) abs(w - x); mp[x].push_back(w); } } sort(mp[x].begin(), mp[x].end()); assert((int) mp[x].size() == n - 1); long long sum = 0; for (int i = 0; i < n - 1; ++i) { sum += mp[x][i]; ps[{x, i}] = sum; } } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cin >> n >> m; vector<array<int, 3>> e(m); for (int i = 0; i < m; ++i) { int u, v, w; cin >> u >> v >> w; e[i] = {u, v, w}; } sort(e.begin(), e.end(), [&](array<int, 3>& i, array<int, 3>& j) { return i[2] < j[2]; }); simulate(0, e); for (int i = 1; i < m; ++i) { simulate((e[i - 1][2] + e[i][2]) / 2, e); simulate((e[i - 1][2] + e[i][2] + 2) / 2, e); } int q; cin >> q; while (q--) { int x; cin >> x; auto [rx, vec] = *prev(mp.upper_bound(x)); auto ind = upper_bound(vec.begin(), vec.end(), x) - vec.begin(); long long ans = 0; long long pref = 0; if (ind > 0) { pref = ps[{rx, ind - 1}]; ans += (long long) (ind) * x - pref; } if (ind < n - 1) { long long suf = ps[{rx, n - 2}] - pref; ans += suf - (long long) (n - ind - 1) * x; } cout << ans << '\n'; } return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...