Submission #922125

#TimeUsernameProblemLanguageResultExecution timeMemory
922125406Road Closures (APIO21_roads)C++17
100 / 100
424 ms74200 KiB
#include "roads.h" #include <bits/stdc++.h> //PAIN ^ 2 using namespace std; const int N = 1e5 + 5; const long long INF = 1ll << 60; long long dp[N][2]; int X, p[N], deg[N]; set<array<int, 2>> adj[N]; vector<array<int, 2>> adjV[N]; vector<int> rec; set<int> S; bitset<N> enb, mark; struct segment_tree { vector<long long> sum, cnt; int n = 0; void init(int _n) { n = _n; sum.assign(4 * n, 0); cnt.assign(4 * n, 0); } long long get(int x) { if (x <= 0) return (x == 0 ? 0 : -INF); int l = 0, r = n, v = 1; long long s = 0; while (r - l > 1) { int m = l + r >> 1; v <<= 1; if (cnt[v] >= x) r = m; else x -= cnt[v], s += sum[v], v |= 1, l = m; } return s + sum[v]; } void add(int id, int x) { int l = 0, r = n, v = 1; while (r - l > 1) { sum[v] += x, cnt[v]++; int m = l + r >> 1; v <<= 1; if (id < m) r = m; else l = m, v |= 1; } sum[v] += x, cnt[v]++; } } SEG[N]; void dfs(int v, int p) { if (enb[v]) assert(0); rec.push_back(v); mark[v] = true; long long dif = 0, ans = 0, mx = 0; vector<long long> V; V.reserve(adj[v].size()); for (auto P: adj[v]) { int u = P[0], w = P[1]; if (u == p) continue; dfs(u, v); dif += dp[u][0]; V.push_back(w + dp[u][1] - dp[u][0]); } sort(V.rbegin(), V.rend()); dp[v][0] = dp[v][1] = 0; for (int i = 0; i <= X && i <= V.size(); i++) { if (i) ans += V[i - 1]; dp[v][0] = max(dp[v][0], SEG[v].get(X - i) + ans); dp[v][1] = max(dp[v][1], SEG[v].get(X - i - 1) + ans); //if (v == 0) //cout << "i: " << i << " (chosen from V) that is " << ans << " SEG: " << SEG[v].get(X - i) << endl; } dp[v][0] += dif, dp[v][1] += dif; //cout << "V: " << v << " SEG: " << SEG[v].kol << " dp[0] " << dp[v][0] << " dp[1] " << dp[v][1] << endl; } vector<long long> minimum_closure_costs(int n, vector<int> U, vector<int> V, vector<int> W) { long long all = 0; for (int i = 0; i < U.size(); i++) { adj[U[i]].insert({V[i], W[i]}); adj[V[i]].insert({U[i], W[i]}); adjV[U[i]].push_back({-W[i], V[i]}); adjV[V[i]].push_back({-W[i], U[i]}); all += W[i]; } for (int i = 0; i < n; i++) sort(adjV[i].begin(), adjV[i].end()), SEG[i].init(adjV[i].size()); iota(p, p + n, 0), sort(p, p + n, [&](const int x, const int y) {return adjV[x].size() < adjV[y].size();}); for (int i = 0; i < n; i++) S.insert(i); vector<long long> re(n); re[0] = all; for (int i = 1, ptr = 0; i < n; i++) { //TODO X = i; for (;ptr < n && adjV[p[ptr]].size() <= i; ptr++) { int v = p[ptr]; array<int, 2> P; for (auto P: adjV[v]) { int u = P[1], w = -P[0]; if (enb[u]) all -= w; else { SEG[u].add(lower_bound(adjV[u].begin(), adjV[u].end(), array<int, 2>{-w, v}) - adjV[u].begin(), w); //cout << "ADDING TO " << u << " edge: " << w << endl; } adj[v].erase(P); adj[u].erase({v, w}); } enb[v] = true; S.erase(v); } long long ans = 0; for (auto v: S) if (!mark[v]) { //cout << "DFS: " << v << endl; dfs(v, v); ans += dp[v][0]; } for (auto v: rec) mark[v] = 0; rec.clear(); re[i] = all - ans; } return re; }

Compilation message (stderr)

roads.cpp: In member function 'long long int segment_tree::get(int)':
roads.cpp:28:18: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   28 |        int m = l + r >> 1;
      |                ~~^~~
roads.cpp: In member function 'void segment_tree::add(int, int)':
roads.cpp:39:18: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   39 |        int m = l + r >> 1;
      |                ~~^~~
roads.cpp: In function 'void dfs(int, int)':
roads.cpp:67:34: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   67 |      for (int i = 0; i <= X && i <= V.size(); i++) {
      |                                ~~^~~~~~~~~~~
roads.cpp:54:34: warning: unused variable 'mx' [-Wunused-variable]
   54 |      long long dif = 0, ans = 0, mx = 0;
      |                                  ^~
roads.cpp: In function 'std::vector<long long int> minimum_closure_costs(int, std::vector<int>, std::vector<int>, std::vector<int>)':
roads.cpp:80:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   80 |      for (int i = 0; i < U.size(); i++) {
      |                      ~~^~~~~~~~~~
roads.cpp:96:44: warning: comparison of integer expressions of different signedness: 'std::vector<std::array<int, 2> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   96 |       for (;ptr < n && adjV[p[ptr]].size() <= i; ptr++) {
      |                        ~~~~~~~~~~~~~~~~~~~~^~~~
roads.cpp:98:22: warning: unused variable 'P' [-Wunused-variable]
   98 |        array<int, 2> P;
      |                      ^
#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...
#Verdict Execution timeMemoryGrader output
Fetching results...