Submission #67309

# Submission time Handle Problem Language Result Execution time Memory
67309 2018-08-13T22:01:41 Z duality Factories (JOI14_factories) C++11
100 / 100
5892 ms 287836 KB
#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

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 time Memory Grader output
1 Correct 37 ms 12664 KB Output is correct
2 Correct 1294 ms 30752 KB Output is correct
3 Correct 1306 ms 35048 KB Output is correct
4 Correct 1412 ms 35356 KB Output is correct
5 Correct 1238 ms 35604 KB Output is correct
6 Correct 843 ms 35604 KB Output is correct
7 Correct 1159 ms 35604 KB Output is correct
8 Correct 1296 ms 35604 KB Output is correct
9 Correct 1206 ms 35604 KB Output is correct
10 Correct 869 ms 37724 KB Output is correct
11 Correct 1264 ms 44368 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 19 ms 44368 KB Output is correct
2 Correct 2731 ms 113896 KB Output is correct
3 Correct 4302 ms 116504 KB Output is correct
4 Correct 1301 ms 131660 KB Output is correct
5 Correct 3723 ms 180544 KB Output is correct
6 Correct 3640 ms 180544 KB Output is correct
7 Correct 2428 ms 180544 KB Output is correct
8 Correct 1226 ms 180544 KB Output is correct
9 Correct 3025 ms 180544 KB Output is correct
10 Correct 2946 ms 180544 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 37 ms 12664 KB Output is correct
2 Correct 1294 ms 30752 KB Output is correct
3 Correct 1306 ms 35048 KB Output is correct
4 Correct 1412 ms 35356 KB Output is correct
5 Correct 1238 ms 35604 KB Output is correct
6 Correct 843 ms 35604 KB Output is correct
7 Correct 1159 ms 35604 KB Output is correct
8 Correct 1296 ms 35604 KB Output is correct
9 Correct 1206 ms 35604 KB Output is correct
10 Correct 869 ms 37724 KB Output is correct
11 Correct 1264 ms 44368 KB Output is correct
12 Correct 19 ms 44368 KB Output is correct
13 Correct 2731 ms 113896 KB Output is correct
14 Correct 4302 ms 116504 KB Output is correct
15 Correct 1301 ms 131660 KB Output is correct
16 Correct 3723 ms 180544 KB Output is correct
17 Correct 3640 ms 180544 KB Output is correct
18 Correct 2428 ms 180544 KB Output is correct
19 Correct 1226 ms 180544 KB Output is correct
20 Correct 3025 ms 180544 KB Output is correct
21 Correct 2946 ms 180544 KB Output is correct
22 Correct 3682 ms 251572 KB Output is correct
23 Correct 3456 ms 264068 KB Output is correct
24 Correct 3709 ms 265676 KB Output is correct
25 Correct 3954 ms 268616 KB Output is correct
26 Correct 5202 ms 268616 KB Output is correct
27 Correct 4426 ms 287836 KB Output is correct
28 Correct 2017 ms 287836 KB Output is correct
29 Correct 4872 ms 287836 KB Output is correct
30 Correct 5289 ms 287836 KB Output is correct
31 Correct 5892 ms 287836 KB Output is correct
32 Correct 2089 ms 287836 KB Output is correct
33 Correct 1411 ms 287836 KB Output is correct
34 Correct 2554 ms 287836 KB Output is correct
35 Correct 2412 ms 287836 KB Output is correct
36 Correct 2820 ms 287836 KB Output is correct
37 Correct 2700 ms 287836 KB Output is correct