Submission #58359

#TimeUsernameProblemLanguageResultExecution timeMemory
58359YehezkielFactories (JOI14_factories)C++11
15 / 100
8077 ms229504 KiB
#include "factories.h" #include <bits/stdc++.h> using namespace std; #define fi first #define se second #define mp make_pair #define eb emplace_back typedef long long LL; typedef long double LD; typedef pair<int,int> pii; const int logn=19; const int MAXN=500000; int par[logn+3][MAXN+5]={},depth[MAXN+5]={}; LL jarak[MAXN+5]={}; vector <pii> node[MAXN+5]; void inisLCA(int now,int _par,int _depth,LL _jarak){ depth[now]=_depth; jarak[now]=_jarak; par[0][now]=_par; for(int i=1;i<logn;i++) par[i][now]=par[i-1][par[i-1][now]]; for(auto v:node[now]) { if(v.fi==_par) continue; inisLCA(v.fi,now,_depth+1,_jarak+v.se); } } void inisLCA(){ inisLCA(1,0,0,0); } int LCA(int u,int v){ if(depth[u]>depth[v]) swap(u,v); int beda=depth[v]-depth[u]; for(int i=0;i<logn;i++) { if(beda&(1<<i)) v=par[i][v]; } if(u==v) return u; for(int i=logn-1;i>=0;i--) { if(par[i][u]!=par[i][v]) u=par[i][u],v=par[i][v]; } return par[0][u]; } LL dist(int u,int v){ return jarak[u]+jarak[v]-(jarak[LCA(u,v)]<<1); } int parCtr[MAXN+5]={},subtree[MAXN+5]={}; int hitungSubtree(int now,int tadi){ assert(parCtr[now]==-1); subtree[now]=1; for(auto v:node[now]){ if(v.fi==tadi||parCtr[v.fi]!=-1) continue; subtree[now]+=hitungSubtree(v.fi,now); } return subtree[now]; } void buatCtr(int now,int tadi){ assert(parCtr[now]==-1); hitungSubtree(now,-1); int target=subtree[now]>>1; int pilihan=-1,skipAja=tadi; bool lanjut=true; while(lanjut) { lanjut=false; for(auto v:node[now]) { if(v.fi==skipAja||parCtr[v.fi]!=-1) continue; if(subtree[v.fi]>target) { lanjut=true; pilihan=v.fi; } } if(lanjut) { skipAja=now; now=pilihan; } } parCtr[now]=tadi; for(auto v:node[now]) { if(parCtr[v.fi]==-1) buatCtr(v.fi,now); } } void buatCtr(){ memset(parCtr,-1,sizeof(parCtr)); buatCtr(1,0); } void Init(int N,int A[],int B[],int D[]){ for(int i=0;i<N;i++) { A[i]++;B[i]++; node[A[i]].eb(B[i],D[i]); node[B[i]].eb(A[i],D[i]); } inisLCA(); buatCtr(); } int lastReset[MAXN+5]={},kejadian=0; LL registered[MAXN+5]; const LL INF=1000000000000000; void ngeFix(int pos){ if(lastReset[pos]!=kejadian) registered[pos]=INF; lastReset[pos]=kejadian; } void update(int pos){ int now=pos; while(now) { ngeFix(now); registered[now]=min(registered[now],dist(now,pos)); now=parCtr[now]; } } LL Query(int pos){ int now=pos; LL ret=INF; while(now) { ngeFix(now); ret=min(ret,registered[now]+dist(now,pos)); now=parCtr[now]; } return ret; } LL Query(int S,int X[],int T,int Y[]){ for(int i=0;i<S;i++) X[i]++; for(int i=0;i<T;i++) Y[i]++; kejadian++; for(int i=0;i<S;i++) update(X[i]); LL ret=INF; for(int i=0;i<T;i++) ret=min(ret,Query(Y[i])); return ret; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...