Submission #337791

#TimeUsernameProblemLanguageResultExecution timeMemory
337791ThistleFactories (JOI14_factories)C++14
0 / 100
8112 ms179576 KiB
#include "factories.h" #include <bits/stdc++.h> using namespace std; using ll=long long; #define all(a) a.begin(),a.end() #define vec vector #define pb push_back #define rng(i,a,b) for(int i=(a);i<(b);i++) #define rep(i,n) rng(i,0,n) #define siz(a) a.size() template<typename T> void chmin(T& a,T b){a=min(a,b);} template<typename T> void chmax(T& a,T b){a=max(a,b);} int n; vec<int>e[600000]; ll dpt[600000]; vec<int>eul; vec<ll>dpto; int out[600000]; int spt[1100000][20]; int lgt[1100000]; int lca(int a,int b){ int x=out[a],y=out[b]; if(x>y) swap(x,y); int k=lgt[(y-x+1)]; int s=spt[x][k],t=spt[y-(1<<k)+1][k]; if(dpto[s]<dpto[t]) return eul[s]; else return eul[t]; } ll dist(int a,int b){ return dpt[a]+dpt[b]-2*dpt[lca(a,b)]; } void Init(int N, int A[], int B[], int D[]) { n=N; map<pair<int, int>, ll>mp; rep(i,n-1){ e[A[i]].pb(B[i]); e[B[i]].pb(A[i]); mp[make_pair(min(A[i],B[i]),max(A[i],B[i]))]=D[i]; } auto dfs=[&](int x,int p,ll d,auto& dfs)->void{ dpt[x]=d; out[x]=siz(eul); eul.pb(x);dpto.pb(d); for(auto g:e[x]){ if(g==p) continue; ll k=mp[make_pair(min(x,g),max(x,g))]; dfs(g,x,d+k,dfs); eul.pb(x);dpto.pb(d); } }; dfs(0,-1,0,dfs); rep(i,siz(eul)) spt[i][0]=i; rng(i,2,siz(eul)+1) lgt[i]=lgt[i>>1]+1; rep(j,19) for(int i=0;i+(1<<(j+1))<=siz(eul);i++){ int a=spt[i][j],b=spt[i+(1<<j)][j]; if(dpto[a]<dpto[b]) spt[i][j+1]=a; else spt[i][j+1]=b; } } long long Query(int S, int X[], int T, int Y[]) { constexpr ll inf=1e17; ll ans=inf; vec<int>v; set<int>se,se2; rep(i,S){ v.pb(X[i]); se.insert(X[i]); } rep(i,T) { v.pb(Y[i]); se2.insert(Y[i]); } sort(all(v));v.erase(unique(all(v)),v.end()); sort(all(v),[&](int& a,int& b){ return out[a]<out[b]; }); int sz=v.size(); rep(i,sz-1){ v.pb(lca(v[i],v[i+1])); } sort(all(v));v.erase(unique(all(v)),v.end()); sort(all(v),[&](int& a,int& b){ return out[a]<out[b]; }); stack<int>st; vec<vec<int>>f(siz(v),vec<int>()); rep(i,v.size()){ while(!st.empty()&&dist(v[st.top()],v[i])!=dpt[v[i]]-dpt[v[st.top()]]){ st.pop(); } if(!st.empty()) f[st.top()].pb(i); st.push(i); } auto dfs=[&](int x,ll near,auto& dfs)->ll{ if(se.find(v[x])!=se.end()) near=0; for(auto g:f[x]){ chmin(near,dfs(g,near+dpt[v[g]]-dpt[v[x]],dfs)+dpt[v[g]]-dpt[v[x]]); } for(auto g:f[x]){ dfs(g,near+dpt[v[g]]-dpt[v[x]],dfs); } if(se2.find(v[x])!=se2.end()) chmin(ans,near); return near; }; dfs(0,inf,dfs); return ans; }

Compilation message (stderr)

factories.cpp: In function 'void Init(int, int*, int*, int*)':
factories.cpp:8:35: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
    8 | #define rng(i,a,b) for(int i=(a);i<(b);i++)
      |                                   ^
factories.cpp:9:18: note: in expansion of macro 'rng'
    9 | #define rep(i,n) rng(i,0,n)
      |                  ^~~
factories.cpp:59:3: note: in expansion of macro 'rep'
   59 |   rep(i,siz(eul)) spt[i][0]=i;
      |   ^~~
factories.cpp:8:35: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
    8 | #define rng(i,a,b) for(int i=(a);i<(b);i++)
      |                                   ^
factories.cpp:60:3: note: in expansion of macro 'rng'
   60 |   rng(i,2,siz(eul)+1) lgt[i]=lgt[i>>1]+1;
      |   ^~~
factories.cpp:62:37: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   62 |   rep(j,19) for(int i=0;i+(1<<(j+1))<=siz(eul);i++){
      |                                     ^
factories.cpp: In function 'long long int Query(int, int*, int, int*)':
factories.cpp:8:35: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
    8 | #define rng(i,a,b) for(int i=(a);i<(b);i++)
      |                                   ^
factories.cpp:9:18: note: in expansion of macro 'rng'
    9 | #define rep(i,n) rng(i,0,n)
      |                  ^~~
factories.cpp:98:3: note: in expansion of macro 'rep'
   98 |   rep(i,v.size()){
      |   ^~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...