Submission #1093723

#TimeUsernameProblemLanguageResultExecution timeMemory
1093723_8_8_Reconstruction Project (JOI22_reconstruction)C++17
35 / 100
5038 ms46420 KiB
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int N = 1e6 + 12, MOD = (int)1e9 + 3; #define int ll int n, m, vis[N], timer = 0, p[N], f[N]; vector<array<int, 3>> e(N); vector<vector<int>> tr; vector<int> cur; int id(int v, int u) { vector<vector<pair<int,int>>> g(n + 1); for(int i:cur) { g[e[i][1]].push_back({e[i][2], i}); g[e[i][2]].push_back({e[i][1], i}); } queue<int> q; q.push(v); ++timer; vis[v] = timer; while(!q.empty()) { int x = q.front(); q.pop(); // cout << x << ' ' << p[x] << '\n'; for(auto [to, i]:g[x]) { // cout << to << "x\n"; if(vis[to] != timer) { vis[to] = timer; p[to] = x; f[to] = i; q.push(to); } } } int ret = 1e9; while(u != v) { ret = min(ret, f[u]); u = p[u]; // cerr << u << ' ' << v << '\n'; } return ret; } int P[N]; int get(int a) { if(P[a] == a) return a; return P[a] = get(P[a]); } bool merge(int a, int b) { a = get(a); b = get(b); if(a == b) return false; P[a] = b; return true; } ll cost(vector<int> &x, int val) { // cout << (int)x.size() << '\n'; ll ret = 0; for(int i:x) { ret += abs(val - e[i][0]); } return ret; } int l[N], r[N]; const int inf = (int)1e9; void test() { cin >> n >> m; for(int i = 0;i < m; i++) { cin >> e[i][1] >> e[i][2] >> e[i][0]; l[i] = 0, r[i] = inf; } iota(P + 1, P + n + 1, 1); sort(e.begin(), e.begin() + m); int lst = 0; for(int i = 0;i < m; i++) { if(merge(e[i][2], e[i][1])) { cur.push_back(i); continue; } int j = id(e[i][1], e[i][2]); ll val = (e[i][0] + e[j][0] + 1) / 2; l[i] = val; r[j] = val - 1; for(int k = 0; k < (int)cur.size(); k++) { if(cur[k] == j) { cur.erase(cur.begin() + k); break; } } cur.push_back(i); } // for(int i = 0; i < m; i++) { // cout << l[i] << ' ' << r[i] << '\n'; // } int q; cin >> q; while(q--) { int x; cin >> x; ll res = 0; for(int i = 0; i < m; i++) { if(l[i] <= x && x <= r[i]) { res += abs(x - e[i][0]); } } cout << res << '\n'; } } signed main() { ios_base::sync_with_stdio(false); cin.tie(0); int t = 1; // cin >> t; while(t--) test(); return 0; }

Compilation message (stderr)

reconstruction.cpp: In function 'void test()':
reconstruction.cpp:75:9: warning: unused variable 'lst' [-Wunused-variable]
   75 |     int lst = 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...