Submission #67309

#TimeUsernameProblemLanguageResultExecution timeMemory
67309dualityFactories (JOI14_factories)C++11
100 / 100
5892 ms287836 KiB
#define DEBUG 0 #include <bits/stdc++.h> using namespace std; #if DEBUG // basic debugging macros int __i__,__j__; #define printLine(l) for(__i__=0;__i__<l;__i__++){cout<<"-";}cout<<endl #define printLine2(l,c) for(__i__=0;__i__<l;__i__++){cout<<c;}cout<<endl #define printVar(n) cout<<#n<<": "<<n<<endl #define printArr(a,l) cout<<#a<<": ";for(__i__=0;__i__<l;__i__++){cout<<a[__i__]<<" ";}cout<<endl #define print2dArr(a,r,c) cout<<#a<<":\n";for(__i__=0;__i__<r;__i__++){for(__j__=0;__j__<c;__j__++){cout<<a[__i__][__j__]<<" ";}cout<<endl;} #define print2dArr2(a,r,c,l) cout<<#a<<":\n";for(__i__=0;__i__<r;__i__++){for(__j__=0;__j__<c;__j__++){cout<<setw(l)<<setfill(' ')<<a[__i__][__j__]<<" ";}cout<<endl;} // advanced debugging class // debug 1,2,'A',"test"; class _Debug { public: template<typename T> _Debug& operator,(T val) { cout << val << endl; return *this; } }; #define debug _Debug(), #else #define printLine(l) #define printLine2(l,c) #define printVar(n) #define printArr(a,l) #define print2dArr(a,r,c) #define print2dArr2(a,r,c,l) #define debug #endif // define #define MAX_VAL 999999999 #define MAX_VAL_2 999999999999999999LL #define EPS 1e-6 #define mp make_pair #define pb push_back // typedef typedef unsigned int UI; typedef long long int LLI; typedef unsigned long long int ULLI; typedef unsigned short int US; typedef pair<int,int> pii; typedef pair<LLI,LLI> plli; typedef vector<int> vi; typedef vector<LLI> vlli; typedef vector<pii> vpii; typedef vector<plli> vplli; // ---------- END OF TEMPLATE ---------- vpii adjList[500000]; int disc[500000],fin[500000],depth[500000],num = 0; LLI dist[500000]; int parent[500000][19]; int doDFS(int u,int p,int d,LLI dd) { int i; disc[u] = num++,depth[u] = d,dist[u] = dd; parent[u][0] = p; for (i = 0; i < adjList[u].size(); i++) { int v = adjList[u][i].first; if (v != p) doDFS(v,u,d+1,dd+adjList[u][i].second); } fin[u] = num; return 0; } void Init(int N,int A[],int B[],int D[]) { int i,j; for (i = 0; i < (N-1); i++) { adjList[A[i]].pb(mp(B[i],D[i])); adjList[B[i]].pb(mp(A[i],D[i])); } doDFS(0,-1,0,0); for (i = 1; i < 19; i++) { for (j = 0; j < N; j++) { if (parent[j][i-1] != -1) parent[j][i] = parent[parent[j][i-1]][i-1]; else parent[j][i] = -1; } } } int lca(int u,int v) { int i; if (depth[u] < depth[v]) swap(u,v); for (i = 18; i >= 0; i--) { if ((parent[u][i] != -1) && (depth[parent[u][i]] >= depth[v])) u = parent[u][i]; } if (u == v) return u; for (i = 18; i >= 0; i--) { if ((parent[u][i] != -1) && (parent[u][i] != parent[v][i])) u = parent[u][i],v = parent[v][i]; } return parent[u][0]; } vector<pair<pii,int> > v; vector<pair<int,plli> > s; long long Query(int S,int X[],int T,int Y[]) { int i; for (i = 0; i < S; i++) v.pb(mp(mp(disc[X[i]],X[i]),0)); for (i = 0; i < T; i++) v.pb(mp(mp(disc[Y[i]],Y[i]),-1)); sort(v.begin(),v.end()); for (i = 1; i < (S+T); i++) { int l = lca(v[i-1].first.second,v[i].first.second); v.pb(mp(mp(disc[l],l),-2)); } sort(v.begin(),v.end()); LLI ans = MAX_VAL_2; for (i = v.size()-1; i >= 0; i--) { if ((i == v.size()-1) || (v[i].first != v[i+1].first)) { LLI d1 = MAX_VAL_2,d2 = MAX_VAL_2; if (v[i].second == 0) d1 = 0; else if (v[i].second == -1) d2 = 0; while (!s.empty() && (disc[s.back().first] < fin[v[i].first.second])) { d1 = min(d1,s.back().second.first+dist[s.back().first]-dist[v[i].first.second]); d2 = min(d2,s.back().second.second+dist[s.back().first]-dist[v[i].first.second]); s.pop_back(); } ans = min(ans,d1+d2); s.pb(mp(v[i].first.second,mp(d1,d2))); } } v.clear(),s.clear(); return ans; }

Compilation message (stderr)

factories.cpp: In function 'int doDFS(int, int, int, LLI)':
factories.cpp:66:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (i = 0; i < adjList[u].size(); i++) {
                 ~~^~~~~~~~~~~~~~~~~~~
factories.cpp: In function 'long long int Query(int, int*, int, int*)':
factories.cpp:114:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         if ((i == v.size()-1) || (v[i].first != v[i+1].first)) {
              ~~^~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...