Submission #387724

#TimeUsernameProblemLanguageResultExecution timeMemory
387724arujbansalFactories (JOI14_factories)C++17
Compilation error
0 ms0 KiB
#pragma GCC optimize("Ofast") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") #include "factories.h" #include <iostream> #include <algorithm> #include <vector> #include <map> #include <set> #include <array> #include <stack> #include <queue> #include <random> #include <numeric> #include <functional> #include <chrono> #include <utility> #include <iomanip> #include <assert.h> using namespace std; void dbg_out() { cerr << endl; } template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cerr << ' ' << H; dbg_out(T...); } #define dbg(...) cerr << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__) #define rng_init mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()) #define rng_seed(x) mt19937 rng(x) #define all(x) (x).begin(), (x).end() #define sz(x) (int) (x).size() // #define int long long using ll = long long; struct LCA { int n, k; vector<vector<int>> adj, sparse_table; vector<int> euler, first_occurrence; int timer; bool lca_built; LCA(int _n = 0) { lca_built = false; if (_n > 0) init(_n); } void init(int _n) { lca_built = false; n = _n; adj.resize(n); for (int i = 0; i < n; i++) adj[i].clear(); euler.clear(); first_occurrence.resize(n); timer = 0; } void add_edge(int u, int v) { assert(n > 0); adj[u].push_back(v); adj[v].push_back(u); } void run(int root = 0) { assert(n > 0); dfs(root, -1); build(); lca_built = true; } void dfs(int u = 0, int p = -1) { first_occurrence[u] = timer++; euler.push_back(u); for (const auto &v : adj[u]) { if (v == p) continue; dfs(v, u); euler.push_back(u); timer++; } } void build() { k = 31 - __builtin_clz(timer) + 1; sparse_table.resize(k); for (int i = 0; i < k; i++) sparse_table[i].resize(timer - (1 << i) + 1); for (int j = 0; j < timer; j++) sparse_table[0][j] = euler[j]; for (int i = 1; i < k; i++) { for (int j = 0; j + (1 << i) - 1 < timer; j++) { int x = sparse_table[i - 1][j]; int y = sparse_table[i - 1][j + (1 << (i - 1))]; sparse_table[i][j] = (first_occurrence[x] < first_occurrence[y] ? x : y); } } } int query(int u, int v) { assert(lca_built == true); int l = first_occurrence[u], r = first_occurrence[v]; if (l > r) swap(l, r); int i = 31 - __builtin_clz(r - l + 1); int x = sparse_table[i][l], y = sparse_table[i][r - (1 << i) + 1]; return first_occurrence[x] < first_occurrence[y] ? x : y; } }; const int MXN = 5e5 + 1, MXK = 19; const ll INF = 1e18; vector<pair<int, ll>> g[MXN]; int cd_par[MXN], subtree[MXN]; bool vis[MXN]; ll dist_g[MXN], best[MXN]; LCA lca_g; void dfs1(int u, int p, ll cur_dist) { dist_g[u] = cur_dist; for (const auto &[v, w] : g[u]) { if (v == p) continue; dfs1(v, u, cur_dist + w); } } ll dist(int u, int v) { if (u == v) return 0; int cur_lca = lca_g.query(u, v); return dist_g[u] - dist_g[cur_lca] + dist_g[v] - dist_g[cur_lca]; } void dfs_subtree(int u, int p) { subtree[u] = 1; for (const auto &[v, w] : g[u]) { if (v == p || vis[v]) continue; dfs_subtree(v, u); subtree[u] += subtree[v]; } } int centroid(int u, int p, int vertices) { for (const auto &[v, w] : g[u]) { if (v == p || vis[v]) continue; if (subtree[v] * 2 > vertices) return centroid(v, u, vertices); } return u; } void build_cd(int u, int p) { dfs_subtree(u, -1); int c = centroid(u, -1, subtree[u]); if (p == -1) p = c; cd_par[c] = p; vis[c] = true; for (const auto &[v, w] : g[c]) { if (vis[v]) continue; build_cd(v, c); } } void build_factory(int u) { int v = u; best[v] = 0; do { v = cd_par[v]; best[v] = min(best[v], dist(u, v)); } while (v != cd_par[v]); } void remove_factory(int v) { best[v] = INF; do { v = cd_par[v]; best[v] = INF; } while (v != cd_par[v]); } ll query_factory(int u) { int v = u; ll ans = best[v]; do { v = cd_par[v]; ans = min(ans, dist(u, v) + best[v]); } while (v != cd_par[v]); return ans; } void Init(int N, int A[], int B[], int D[]) { lca_g.init(N); for (int i = 0; i < N - 1; i++) { g[A[i]].emplace_back(B[i], D[i]); g[B[i]].emplace_back(A[i], D[i]); lca_g.add_edge(A[i], B[i]); } lca_g.run(0); dfs1(0, -1, 0); build_cd(0, -1); for (int i = 0; i <= N; i++) best[i] = INF; } ll Query(int S, int X[], int T, int Y[]) { ll ans = INF; for (int i = 0; i < S; i++) build_factory(X[i]); for (int i = 0; i < T; i++) ans = min(ans, query_factory(Y[i])); for (int i = 0; i < S; i++) remove_factory(X[i]); return ans; } // void solve() { // int N, Q; // cin >> N >> Q; // int A[N - 1], B[N - 1], D[N - 1]; // for (int i = 0; i < N - 1; i++) // cin >> A[i] >> B[i] >> D[i]; // Init(N, A, B, D); // while (Q--) { // int S, T; // cin >> S >> T; // int X[S], Y[T]; // for (int i = 0; i < S; i++) // cin >> X[i]; // for (int i = 0; i < T; i++) // cin >> Y[i]; // cout << Query(S, X, T, Y) << "\n"; // } // } // signed main() { // ios_base::sync_with_stdio(false); // cin.tie(nullptr); // int TC = 1; // // cin >> TC; // while (TC--) solve(); // }

Compilation message (stderr)

In file included from /usr/include/x86_64-linux-gnu/c++/9/bits/gthr.h:148,
                 from /usr/include/c++/9/ext/atomicity.h:35,
                 from /usr/include/c++/9/bits/ios_base.h:39,
                 from /usr/include/c++/9/ios:42,
                 from /usr/include/c++/9/ostream:38,
                 from /usr/include/c++/9/iostream:39,
                 from factories.cpp:6:
/usr/include/x86_64-linux-gnu/c++/9/bits/gthr-default.h:102:1: error: option("tune=") was already specified
  102 | __gthrw(pthread_once)
      | ^~~~~~~
/usr/include/x86_64-linux-gnu/c++/9/bits/gthr-default.h:102:1: error: option("tune=") was already specified
/usr/include/x86_64-linux-gnu/c++/9/bits/gthr-default.h:103:1: error: option("tune=") was already specified
  103 | __gthrw(pthread_getspecific)
      | ^~~~~~~
/usr/include/x86_64-linux-gnu/c++/9/bits/gthr-default.h:103:1: error: option("tune=") was already specified
/usr/include/x86_64-linux-gnu/c++/9/bits/gthr-default.h:104:1: error: option("tune=") was already specified
  104 | __gthrw(pthread_setspecific)
      | ^~~~~~~
/usr/include/x86_64-linux-gnu/c++/9/bits/gthr-default.h:104:1: error: option("tune=") was already specified
/usr/include/x86_64-linux-gnu/c++/9/bits/gthr-default.h:106:1: error: option("tune=") was already specified
  106 | __gthrw(pthread_create)
      | ^~~~~~~
/usr/include/x86_64-linux-gnu/c++/9/bits/gthr-default.h:106:1: error: option("tune=") was already specified
/usr/include/x86_64-linux-gnu/c++/9/bits/gthr-default.h:107:1: error: option("tune=") was already specified
  107 | __gthrw(pthread_join)
      | ^~~~~~~
/usr/include/x86_64-linux-gnu/c++/9/bits/gthr-default.h:107:1: error: option("tune=") was already specified
/usr/include/x86_64-linux-gnu/c++/9/bits/gthr-default.h:108:1: error: option("tune=") was already specified
  108 | __gthrw(pthread_equal)
      | ^~~~~~~
/usr/include/x86_64-linux-gnu/c++/9/bits/gthr-default.h:108:1: error: option("tune=") was already specified
/usr/include/x86_64-linux-gnu/c++/9/bits/gthr-default.h:109:1: error: option("tune=") was already specified
  109 | __gthrw(pthread_self)
      | ^~~~~~~
/usr/include/x86_64-linux-gnu/c++/9/bits/gthr-default.h:109:1: error: option("tune=") was already specified
/usr/include/x86_64-linux-gnu/c++/9/bits/gthr-default.h:110:1: error: option("tune=") was already specified
  110 | __gthrw(pthread_detach)
      | ^~~~~~~
/usr/include/x86_64-linux-gnu/c++/9/bits/gthr-default.h:110:1: error: option("tune=") was already specified
/usr/include/x86_64-linux-gnu/c++/9/bits/gthr-default.h:112:1: error: option("tune=") was already specified
  112 | __gthrw(pthread_cancel)
      | ^~~~~~~
/usr/include/x86_64-linux-gnu/c++/9/bits/gthr-default.h:112:1: error: option("tune=") was already specified
/usr/include/x86_64-linux-gnu/c++/9/bits/gthr-default.h:114:1: error: option("tune=") was already specified
  114 | __gthrw(sched_yield)
      | ^~~~~~~
/usr/include/x86_64-linux-gnu/c++/9/bits/gthr-default.h:114:1: error: option("tune=") was already specified
/usr/include/x86_64-linux-gnu/c++/9/bits/gthr-default.h:116:1: error: option("tune=") was already specified
  116 | __gthrw(pthread_mutex_lock)
      | ^~~~~~~
/usr/include/x86_64-linux-gnu/c++/9/bits/gthr-default.h:116:1: error: option("tune=") was already specified
/usr/include/x86_64-linux-gnu/c++/9/bits/gthr-default.h:117:1: error: option("tune=") was already specified
  117 | __gthrw(pthread_mutex_trylock)
      | ^~~~~~~
/usr/include/x86_64-linux-gnu/c++/9/bits/gthr-default.h:117:1: error: option("tune=") was already specified
/usr/include/x86_64-linux-gnu/c++/9/bits/gthr-default.h:119:1: error: option("tune=") was already specified
  119 | __gthrw(pthread_mutex_timedlock)
      | ^~~~~~~
/usr/include/x86_64-linux-gnu/c++/9/bits/gthr-default.h:119:1: error: option("tune=") was already specified
/usr/include/x86_64-linux-gnu/c++/9/bits/gthr-default.h:121:1: error: option("tune=") was already specified
  121 | __gthrw(pthread_mutex_unlock)
      | ^~~~~~~
/usr/include/x86_64-linux-gnu/c++/9/bits/gthr-default.h:121:1: error: option("tune=") was already specified
/usr/include/x86_64-linux-gnu/c++/9/bits/gthr-default.h:122:1: error: option("tune=") was already specified
  122 | __gthrw(pthread_mutex_init)
      | ^~~~~~~
/usr/include/x86_64-linux-gnu/c++/9/bits/gthr-default.h:122:1: error: option("tune=") was already specified
/usr/include/x86_64-linux-gnu/c++/9/bits/gthr-default.h:123:1: error: option("tune=") was already specified
  123 | __gthrw(pthread_mutex_destroy)
      | ^~~~~~~
/usr/include/x86_64-linux-gnu/c++/9/bits/gthr-default.h:123:1: error: option("tune=") was already specified
/usr/include/x86_64-linux-gnu/c++/9/bits/gthr-default.h:125:1: error: option("tune=") was already specified
  125 | __gthrw(pthread_cond_init)
      | ^~~~~~~
/usr/include/x86_64-linux-gnu/c++/9/bits/gthr-default.h:125:1: error: option("tune=") was already specified
/usr/include/x86_64-linux-gnu/c++/9/bits/gthr-default.h:126:1: error: option("tune=") was already specified
  126 | __gthrw(pthread_cond_broadcast)
      | ^~~~~~~
/usr/include/x86_64-linux-gnu/c++/9/bits/gthr-default.h:126:1: error: option("tune=") was already specified
/usr/include/x86_64-linux-gnu/c++/9/bits/gthr-default.h:127:1: error: option("tune=") was already specified
  127 | __gthrw(pthread_cond_signal)
      | ^~~~~~~
/usr/include/x86_64-linux-gnu/c++/9/bits/gthr-default.h:127:1: error: option("tune=") was already specified
/usr/include/x86_64-linux-gnu/c++/9/bits/gthr-default.h:128:1: error: option("tune=") was already specified
  128 | __gthrw(pthread_cond_wait)
      | ^~~~~~~
/usr/include/x86_64-linux-gnu/c++/9/bits/gthr-default.h:128:1: error: option("tune=") was already specified
/usr/include/x86_64-linux-gnu/c++/9/bits/gthr-default.h:129:1: error: option("tune=") was already specified
  129 | __gthrw(pthread_cond_timedwait)
      | ^~~~~~~
/usr/include/x86_64-linux-gnu/c++/9/bits/gthr-default.h:129:1: error: option("tune=") was already specified
/usr/include/x86_64-linux-gnu/c++/9/bits/gthr-default.h:130:1: error: option("tune=") was already specified
  130 | __gthrw(pthread_cond_destroy)
      | ^~~~~~~
/usr/include/x86_64-linux-gnu/c++/9/bits/gthr-default.h:130:1: error: option("tune=") was already specified
/usr/include/x86_64-linux-gnu/c++/9/bits/gthr-default.h:132:1: error: option("tune=") was already specified
  132 | __gthrw(pthread_key_create)
      | ^~~~~~~
/usr/include/x86_64-linux-gnu/c++/9/bits/gthr-default.h:132:1: error: option("tune=") was already specified
/usr/include/x86_64-linux-gnu/c++/9/bits/gthr-default.h:133:1: error: option("tune=") was already specified
  133 | __gthrw(pthread_key_delete)
      | ^~~~~~~
/usr/include/x86_64-linux-gnu/c++/9/bits/gthr-default.h:133:1: error: option("tune=") was already specified
/usr/include/x86_64-linux-gnu/c++/9/bits/gthr-default.h:134:1: error: option("tune=") was already specified
  134 | __gthrw(pthread_mutexattr_init)
      | ^~~~~~~
/usr/include/x86_64-linux-gnu/c++/9/bits/gthr-default.h:134:1: error: option("tune=") was already specified
/usr/include/x86_64-linux-gnu/c++/9/bits/gthr-default.h:135:1: error: option("tune=") was already specified
  135 | __gthrw(pthread_mutexattr_settype)
      | ^~~~~~~
/usr/include/x86_64-linux-gnu/c++/9/bits/gthr-default.h:135:1: error: option("tune=") was already specified
/usr/include/x86_64-linux-gnu/c++/9/bits/gthr-default.h:136:1: error: option("tune=") was already specified
  136 | __gthrw(pthread_mutexattr_destroy)
      | ^~~~~~~
/usr/include/x86_64-linux-gnu/c++/9/bits/gthr-default.h:136:1: error: option("tune=") was already specified
/usr/include/x86_64-linux-gnu/c++/9/bits/gthr-default.h:237:1: error: option("tune=") was already specified
  237 | __gthrw2(__gthrw_(__pthread_key_create),
      | ^~~~~~~~
/usr/include/x86_64-linux-gnu/c++/9/bits/gthr-default.h:237:1: error: option("tune=") was already specified