Submission #708715

#TimeUsernameProblemLanguageResultExecution timeMemory
708715ThienuFactories (JOI14_factories)C++17
15 / 100
1671 ms524288 KiB
#include "factories.h" #include<bits/stdc++.h> using namespace std; #pragma GCC optimize("O3") #pragma GCC target ("sse4") #define u_map unordered_map #define u_set unordered_set #define u_multiset unordered_multiset using ll = long long; using vvi = vector<vector<int>>; using vi = vector<int>; using vvll = vector<vector<long long>>; using vll = vector<long long>; using vd = vector<double>; using vvd = vector<vector<double>>; using pii = pair<int, int>; using vpii = vector<pair<int, int>>; template<typename C> struct rge{C l, r;}; template<typename C> rge<C> range(C i, C j) { return rge<C>{i, j}; } template<typename C> ostream& operator<<(ostream &os, rge<C> &r) { os << '{'; for(auto it = r.l; it != r.r; it++) os << "," + (it == r.l) << *it; os << '}'; return os; } template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << '{' << p.first << "," << p.second << '}'; } template<typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type> ostream& operator<<(ostream &os, const T_container &v) { os << '{'; string sep; for (const T &x : v) os << sep << x, sep = ","; return os << '}'; } void dbg_out() { cerr << ']' << endl; } template<typename A> void dbg_out(A H) { cerr << H; dbg_out(); } template<typename A, typename B, typename... C> void dbg_out(A H, B G, C... T) { cerr << H << ","; dbg_out(G, T...); } #ifdef DEBUG #define debug(...) cerr << "[" << #__VA_ARGS__ << "] = [", dbg_out(__VA_ARGS__) #else #define debug(...) #endif pair<ll, int> st[22][1000005]; // O(n log n) / O(1) static RMQ, using sparse tables. template <typename T> struct RMQ { private: // floor(log2(x)) inline int log2(int x){ assert(x > 0); return 31 - __builtin_clz(x); } public: vector<T> v; int n; // sparse table // vector<vector<T>> st; // assocative, idempotent inline T op(T x, T y) { return min(x, y); } RMQ(vector<T> &v): v(v) { n = v.size(); int log = log2(n) + 1; for(int i = 0; i < n; i++){ st[0][i] = v[i]; } int step = 1; for(int i = 1; i < log; i++){ for(int j = 0; j + step < n; j++){ st[i][j] = op(st[i-1][j], st[i-1][j+step]); } step <<= 1; } } RMQ() { n = 0; } // query [l, r) T query(int l, int r){ assert(0 <= l && l < r && r <= n); int log = log2(r-l); return op(st[log][l], st[log][r-(1 << log)]); } }; // O(n log n) / O(1) static LCA. struct LCA { public: vector<vpii> adj; int n; RMQ<pair<ll, int>> tour_rmq; private: vector<pair<ll, int>> tour; vi enter; vi exit; vll depth; int time = 0; void dfs1(int u, ll d, int last) { depth[u] = d; enter[u] = time; for(pair<ll, int> p : adj[u]){ int v, w; tie(v, w) = p; if(v == last) continue; tour.push_back({d, u}); time++; dfs1(v, d + w, u); } tour.push_back({d, u}); time++; exit[u] = time; } public: LCA(vector<vpii> &adj, int root = 0): adj(adj) { n = adj.size(); enter.resize(n); exit.resize(n); depth.resize(n); dfs1(root, 0, -1); tour_rmq = RMQ<pair<ll, int>>(tour); } LCA() {} int lca(int u, int v){ pii res = tour_rmq.query(min(enter[u], enter[v]), max(exit[u], exit[v])); return res.second; } inline ll dist(int u, int v){ return depth[u] + depth[v] - 2 * depth[lca(u, v)]; } }; //#include "grader.cpp" #define ll long long #define pb push_back #define X first #define Y second const int maxmum=500005,maxlift=25; vector<pair<int,int> > v[maxmum]; int n,lv[maxmum],par[maxmum][maxlift],sz_opt[maxmum];; ll d[maxmum]; LCA lca; int sz[maxmum],vis[maxmum],par_cen[maxmum]; ll opt[maxmum][maxlift]; int dfs_sz(int u,int p) { sz[u]=1; for(int i=0;i<sz_opt[u];i++) { int node=v[u][i].X; if(node==p||vis[node])continue; sz[u]+=dfs_sz(node,u); } return sz[u]; } int find_cen(int u,int p,int size) { for(int i=0;i<sz_opt[u];i++) { int node=v[u][i].X; if(node==p||vis[node])continue; if(sz[node]>size/2)return find_cen(node,u,size); } return u; } void centroid(int u,int p) { dfs_sz(u,u); int cen=find_cen(u,u,sz[u]); vis[cen]=1; par_cen[cen]=p; for(int i=0;i<sz_opt[cen];i++) { ll node=v[cen][i].X; if(node==p||vis[node])continue; centroid(node,cen); } } ll mn[maxmum]; void Init(int N, int A[], int B[], int D[]) { n=N; vector<vpii> vis(N); for(int i=0;i<n-1;i++) { v[A[i]+1].pb({B[i]+1,D[i]}); v[B[i]+1].pb({A[i]+1,D[i]}); vis[A[i]].pb({B[i],D[i]}); vis[B[i]].pb({A[i],D[i]}); } for(int i=1;i<=n;i++)sz_opt[i]=v[i].size(); par[1][0]=1; lca = LCA(vis); for(int i=1;i<=19;i++) { for(int j=1;j<=n;j++) { par[j][i]=par[par[j][i-1]][i-1]; } } dfs_sz(1,1); centroid(1,0); for(int i=1;i<=n;i++) { ll x=i,kk=0; while(1) { opt[i][kk]=lca.dist(x-1,i-1); ++kk; x=par_cen[x]; if(x==0)break; } } for(int i=1;i<=n;i++)mn[i]=1e18; debug(range(par_cen, par_cen + 10)); } void update(int num,int type) { int u=num,kk=0; while(1) { if(type==0) { mn[u]=min(mn[u],opt[num][kk]); }else { mn[u]=1e18; } u=par_cen[u]; ++kk; if(u==0)break; } } ll query(ll num) { int u=num,kk=0; ll val=1e18; while(1) { val=min(val,opt[num][kk]+mn[u]); u=par_cen[u]; ++kk; if(u==0)break; } return val; } long long Query(int S, int X[], int T, int Y[]) { for(int i=0;i<S;i++) { update(X[i]+1,0); } ll ans=1e18; for(int i=0;i<T;i++) { ans=min(ans,query(Y[i]+1)); } for(int i=0;i<S;i++) { update(X[i]+1,1); } return ans; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...