답안 #66982

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
66982 2018-08-13T06:18:58 Z imeimi2000 공장들 (JOI14_factories) C++17
100 / 100
3020 ms 295328 KB
#include "factories.h"
#include <vector>
#include <algorithm>
#include <functional>

using namespace std;
typedef long long llong;
typedef pair<int, int> pii;
typedef pair<llong, llong> pll;

struct _edge {
    int x, d;
    _edge(int x, int d) : x(x), d(d) {}
};

int n, k;
vector<_edge> edge[500001];

int dep[500001];
llong dist[500001];
int st[500001];
int ed[500001];
int ord[1000000];
pii seg[1000000][21];
int pw[1000001];
int sz;
void dfs(int x, int p) {
    ord[st[x] = k++] = x;
    for (_edge i : edge[x]) {
        if (i.x == p) continue;
        dep[i.x] = dep[x] + 1;
        dist[i.x] = dist[x] + i.d;
        dfs(i.x, x);
        ord[k++] = x;
    }
    ed[x] = k;
}

void init() {
    for (int i = 1, j = 0; i <= 1000000; ++i) {
        if ((1 << (j + 1)) < i) ++j;
        pw[i] = j;
    }
    for (int i = 0; i < k; ++i) {
        seg[i][0] = pii(dep[ord[i]], ord[i]);
    }
    for (int i = 1; i < 21; ++i) {
        for (int j = 0; j < k; ++j) {
            seg[j][i] = seg[j][i - 1];
            if (j + (1 << (i - 1)) < k) {
                seg[j][i] = min(seg[j][i], seg[j + (1 << (i - 1))][i - 1]);
            }
        }
    }
}

void Init(int N, int A[], int B[], int D[]) {
    n = N;
    for (int i = 0; i < n - 1; ++i) {
        ++A[i]; ++B[i];
        edge[A[i]].emplace_back(B[i], D[i]);
        edge[B[i]].emplace_back(A[i], D[i]);
    }
    dfs(1, 0);
    init();
}

int lca(int x, int y) {
    if (x == y) return x;
    x = st[x]; y = st[y];
    if (x > y) swap(x, y);
    int t = pw[y - x + 1];
    return min(seg[x][t], seg[y - (1 << t) + 1][t]).second;
}

const llong inf = 1e16;
int in[500001];
int m, idx;
int qs[1000000];
llong ans;
pll dfs2() {
    llong d = dist[qs[idx]];
    llong mx = d + ((in[qs[idx]] == 1) ? 0 : inf);
    llong my = d + ((in[qs[idx]] == 2) ? 0 : inf);
    
    int e = ed[qs[idx++]];
    while (idx < m) {
        if (e < st[qs[idx]]) break;
        llong rx, ry;
        
        tie(rx, ry) = dfs2();
        ans = min({ ans, mx + ry - d - d, my + rx - d - d });
        mx = min(mx, rx);
        my = min(my, ry);
    }
    return pll(mx, my);
}

llong Query(int S, int X[], int T, int Y[]) {
    m = 0;
    for (int i = 0; i < S; ++i) {
        qs[m++] = ++X[i];
    }
    for (int i = 0; i < T; ++i) {
        qs[m++] = ++Y[i];
    }
    
    sort(qs, qs + m, [&](int i, int j) { return st[i] < st[j]; });
    for (int i = 0; i + 1 < m; ++i) {
        qs[m + i] = lca(qs[i], qs[i + 1]);
    }
    m = (m << 1) - 1;
    sort(qs, qs + m, [&](int i, int j) { return st[i] < st[j]; });
    m = unique(qs, qs + m) - qs;
    
    for (int i = 0; i < m; ++i) in[qs[i]] = 0;
    for (int i = 0; i < S; ++i) in[X[i]] = 1;
    for (int i = 0; i < T; ++i) in[Y[i]] = 2;
    
    idx = 0;
    ans = inf;
    dfs2();
    return ans;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 34 ms 16504 KB Output is correct
2 Correct 837 ms 26380 KB Output is correct
3 Correct 770 ms 26380 KB Output is correct
4 Correct 819 ms 26432 KB Output is correct
5 Correct 778 ms 26684 KB Output is correct
6 Correct 513 ms 26684 KB Output is correct
7 Correct 763 ms 26684 KB Output is correct
8 Correct 805 ms 26684 KB Output is correct
9 Correct 777 ms 26704 KB Output is correct
10 Correct 528 ms 28956 KB Output is correct
11 Correct 750 ms 38444 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 18 ms 38444 KB Output is correct
2 Correct 1917 ms 258532 KB Output is correct
3 Correct 2070 ms 277056 KB Output is correct
4 Correct 1884 ms 278580 KB Output is correct
5 Correct 1793 ms 295120 KB Output is correct
6 Correct 1892 ms 295120 KB Output is correct
7 Correct 1105 ms 295120 KB Output is correct
8 Correct 915 ms 295120 KB Output is correct
9 Correct 953 ms 295120 KB Output is correct
10 Correct 1159 ms 295120 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 34 ms 16504 KB Output is correct
2 Correct 837 ms 26380 KB Output is correct
3 Correct 770 ms 26380 KB Output is correct
4 Correct 819 ms 26432 KB Output is correct
5 Correct 778 ms 26684 KB Output is correct
6 Correct 513 ms 26684 KB Output is correct
7 Correct 763 ms 26684 KB Output is correct
8 Correct 805 ms 26684 KB Output is correct
9 Correct 777 ms 26704 KB Output is correct
10 Correct 528 ms 28956 KB Output is correct
11 Correct 750 ms 38444 KB Output is correct
12 Correct 18 ms 38444 KB Output is correct
13 Correct 1917 ms 258532 KB Output is correct
14 Correct 2070 ms 277056 KB Output is correct
15 Correct 1884 ms 278580 KB Output is correct
16 Correct 1793 ms 295120 KB Output is correct
17 Correct 1892 ms 295120 KB Output is correct
18 Correct 1105 ms 295120 KB Output is correct
19 Correct 915 ms 295120 KB Output is correct
20 Correct 953 ms 295120 KB Output is correct
21 Correct 1159 ms 295120 KB Output is correct
22 Correct 3020 ms 295120 KB Output is correct
23 Correct 2649 ms 295120 KB Output is correct
24 Correct 2693 ms 295120 KB Output is correct
25 Correct 2661 ms 295120 KB Output is correct
26 Correct 2401 ms 295120 KB Output is correct
27 Correct 2914 ms 295328 KB Output is correct
28 Correct 2137 ms 295328 KB Output is correct
29 Correct 2490 ms 295328 KB Output is correct
30 Correct 2463 ms 295328 KB Output is correct
31 Correct 2349 ms 295328 KB Output is correct
32 Correct 1432 ms 295328 KB Output is correct
33 Correct 951 ms 295328 KB Output is correct
34 Correct 1309 ms 295328 KB Output is correct
35 Correct 1311 ms 295328 KB Output is correct
36 Correct 1265 ms 295328 KB Output is correct
37 Correct 1249 ms 295328 KB Output is correct