Submission #744581

#TimeUsernameProblemLanguageResultExecution timeMemory
744581RushBRoad Closures (APIO21_roads)C++17
31 / 100
2083 ms62656 KiB
#include "roads.h" #include <bits/stdc++.h> using namespace std; const int N = 1e5 + 5; const long long INF = 1ll << 60; long long dp[N][2]; int X, p[N]; set<array<int, 2>> adj[N]; vector<int> rec, val[N]; unordered_set<int> S; bitset<N> enb, mark; struct segment_tree { vector<int> 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 -INF; int l = 0, r = n, v = 1, 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; } void add(int id, int x, int l, int r, int v = 1) { if (l + 1 == r) { sum[v] += x; cnt[v]++; return; } int m = l + r >> 1; if (id < m) add(id, x, l, m, v << 1); else add(id, x, m, r, v << 1 | 1); sum[v] = sum[v << 1] + sum[v << 1 | 1]; cnt[v] = cnt[v << 1] + cnt[v << 1 | 1]; } } SEG[N]; void dfs(int v, int p) { if (enb[v]) return void(dp[v][0] = dp[v][1] = 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]; if (w + dp[u][1] > 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] = SEG[v].get(X - i) + ans //dp[v][1] = SEG[v].get(X - i - 1) + ans mx = max(mx, ans); if (i + 1 == X) dp[v][1] = mx + dif; } if (V.size() + 1 < X) dp[v][1] = mx + dif; dp[v][0] = mx + dif; } 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]}); val[U[i]].push_back(W[i]); val[V[i]].push_back(W[i]); all += W[i]; } for (int i = 0; i < n; i++) sort(val[i].begin(), val[i].end()), SEG[i].init(val[i].size()); iota(p, p + n, 0), sort(p, p + n, [&](const int x, const int y) {return adj[x].size() < adj[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++) { X = i; for (;ptr < n && adj[p[ptr]].size() <= i; ptr++) { int v = p[ptr]; array<int, 2> P; for (auto it = adj[v].begin(); it != adj[v].end(); it = adj[v].upper_bound(P)) { P = *it; if (enb[P[0]]) { all -= P[1]; adj[v].erase(it); adj[P[0]].erase({v, P[1]}); SEG[v].add(lower_bound(val[v].begin(), val[v].end(), P[1]) - val[v].begin(), P[1], 0, val[v].size(), 1); //at loc P[1] and v } } enb[v] = true; S.erase(v); } long long ans = 0; for (auto v: S) if (!mark[v]) { 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:25:14: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   25 |    int m = l + r >> 1;
      |            ~~^~~
roads.cpp: In member function 'void segment_tree::add(int, int, int, int, int)':
roads.cpp:38:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   38 |   int m = l + r >> 1;
      |           ~~^~~
roads.cpp: In function 'void dfs(int, int)':
roads.cpp:65:30: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   65 |  for (int i = 0; i <= X && i <= V.size(); i++) {
      |                            ~~^~~~~~~~~~~
roads.cpp:72:19: warning: comparison of integer expressions of different signedness: 'std::vector<long long int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   72 |  if (V.size() + 1 < X) dp[v][1] = mx + dif;
      |      ~~~~~~~~~~~~~^~~
roads.cpp: In function 'std::vector<long long int> minimum_closure_costs(int, std::vector<int>, std::vector<int>, std::vector<int>)':
roads.cpp:78:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   78 |  for (int i = 0; i < U.size(); i++) {
      |                  ~~^~~~~~~~~~
roads.cpp:94:39: warning: comparison of integer expressions of different signedness: 'std::set<std::array<int, 2> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   94 |   for (;ptr < n && adj[p[ptr]].size() <= i; ptr++) {
      |                    ~~~~~~~~~~~~~~~~~~~^~~~
#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...