답안 #489477

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
489477 2021-11-23T03:21:25 Z qwe854896 Cats or Dogs (JOI18_catdog) C++14
100 / 100
269 ms 47116 KB
#include"catdog.h"
#include<bits/stdc++.h>
using namespace std;

#define endl '\n'
#define pb emplace_back
#define LC(i) (i << 1)
#define RC(i) ((i << 1) | 1)
#define IOS ios::sync_with_stdio(false); cin.tie(nullptr)

using ll  = long long;
using vi  = vector <int>;

const ll INF = 100000000000007;
const int V = 200005;

/* Helper */
void chmin(ll& a, ll b) {
    if (b < a) a = b;
}

/* Graph */
int n;
vi g[V];

/* Segment Tree */
struct SegTree {
    struct Node {
        ll c[2][2]; // cat/dog cat/dog
        void init() {
            c[0][0] = c[1][1] = 0;
            c[0][1] = c[1][0] = INF;
        }
        void update(int x, int y) {
            c[0][0] += x;
            c[1][1] += y;
        }
    };

    int n;
    vector <Node> node;
    void init(int n) {
        this->n = n;
        node.resize(n << 3);
        build(1, 0, n - 1);
    }

    void pull(int i) {
        for (int a = 0; a < 2; ++a)
            for (int b = 0; b < 2; ++b)
                node[i].c[a][b] = INF;
        for (int a = 0; a < 2; ++a)
            for (int b = 0; b < 2; ++b)
                for (int c = 0; c < 2; ++c)
                    for (int d = 0; d < 2; ++d)
                        chmin(node[i].c[a][b], node[LC(i)].c[a][c] + (c ^ d) + node[RC(i)].c[d][b]);
    }

    void build(int i, int l, int r) {
        if (l == r) {
            node[i].init();
            return;
        }
        int m = (l + r) >> 1;
        build(LC(i), l, m);
        build(RC(i), m + 1, r);
        pull(i);
    }

    int ui, x, y;
    void update(int i, int l, int r) {
        if (ui < l || r < ui) return;
        if (l == r) {
            node[i].update(x, y);
            return;
        }
        int m = (l + r) >> 1;
        update(LC(i), l, m);
        update(RC(i), m + 1, r);
        pull(i);
    }

    void update(int i, int x, int y, ll &a, ll &b) {
        ui = i;
        this->x = x;
        this->y = y;
        update(1, 0, n - 1);
        query(a, b);
    }

    void query(ll &x, ll &y) {
        ll c = min(node[1].c[0][0], node[1].c[0][1]);
        ll d = min(node[1].c[1][0], node[1].c[1][1]);
        x = min(c, d + 1);
        y = min(c + 1, d);
    }
};

vector <SegTree> tree;

/* HLD */
int sz[V], hs[V];
int pdfs(int x, int px) {
    sz[x] = 1;
    int h = 0;
    for (int y : g[x])
        if (y != px) {
            sz[x] += pdfs(y, x);
            if (sz[y] > h)
                h = sz[y], hs[x] = y;
        }
    return sz[x];
}

int g_p[V];
int g_sz[V];
int g_cnt, v_num[V];
int pos[V];
void hdfs(int x, int px, int num) {
    if (!g_sz[num]) g_p[num] = px;
    pos[x] = g_sz[num]++;
    v_num[x] = num;
    
    if (hs[x] != -1)
        hdfs(hs[x], x, num);

    for (int y : g[x])
        if (y != px && y != hs[x])
            hdfs(y, x, g_cnt++);
}

void initialize(int N, vi A, vi B) {
    n = N;
    for (int i = 0; i < n; ++i)
        g[i].clear();

    for (int i = n - 2; i >= 0; --i)
        g[--A[i]].pb(--B[i]), g[B[i]].pb(A[i]);

    for (int i = 0; i < n; ++i)
        hs[i] = -1;

    pdfs(0, -1);
    g_cnt = 0;
    for (int i = 0; i < n; ++i) g_sz[i] = 0;
    hdfs(0, -1, g_cnt++);

    tree.resize(g_cnt);
    for (int i = 0; i < g_cnt; ++i)
        tree[i].init(g_sz[i]);
}

int update(int i, int x, int y) {
    while (i != -1) {
        int id;
        ll old_x, old_y, new_x, new_y;
        id = v_num[i];
        tree[id].query(old_x, old_y);
        tree[id].update(pos[i], x, y, new_x, new_y);
        x = new_x - old_x;
        y = new_y - old_y;
        i = g_p[id];
    }
    ll a, b;
    tree[0].query(a, b);
    return min(a, b);
}

int st[V];

int cat(int v) {
    --v;
    st[v] = 1;
    return update(v, 0, V);
}

int dog(int v) {
    --v;
    st[v] = 2;
    return update(v, V, 0);
}

int neighbor(int v) {
    --v;
    int ans;
    if (st[v] == 1)
        ans = update(v, 0, -V);
    else
        ans = update(v, -V, 0);
    st[v] = 0;
    return ans;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4940 KB Output is correct
2 Correct 3 ms 4940 KB Output is correct
3 Correct 3 ms 4940 KB Output is correct
4 Correct 3 ms 4944 KB Output is correct
5 Correct 3 ms 4944 KB Output is correct
6 Correct 4 ms 4944 KB Output is correct
7 Correct 3 ms 5004 KB Output is correct
8 Correct 3 ms 4944 KB Output is correct
9 Correct 3 ms 4944 KB Output is correct
10 Correct 3 ms 4984 KB Output is correct
11 Correct 3 ms 4944 KB Output is correct
12 Correct 2 ms 4944 KB Output is correct
13 Correct 2 ms 4944 KB Output is correct
14 Correct 3 ms 4944 KB Output is correct
15 Correct 3 ms 4996 KB Output is correct
16 Correct 3 ms 4944 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4940 KB Output is correct
2 Correct 3 ms 4940 KB Output is correct
3 Correct 3 ms 4940 KB Output is correct
4 Correct 3 ms 4944 KB Output is correct
5 Correct 3 ms 4944 KB Output is correct
6 Correct 4 ms 4944 KB Output is correct
7 Correct 3 ms 5004 KB Output is correct
8 Correct 3 ms 4944 KB Output is correct
9 Correct 3 ms 4944 KB Output is correct
10 Correct 3 ms 4984 KB Output is correct
11 Correct 3 ms 4944 KB Output is correct
12 Correct 2 ms 4944 KB Output is correct
13 Correct 2 ms 4944 KB Output is correct
14 Correct 3 ms 4944 KB Output is correct
15 Correct 3 ms 4996 KB Output is correct
16 Correct 3 ms 4944 KB Output is correct
17 Correct 5 ms 5200 KB Output is correct
18 Correct 6 ms 5328 KB Output is correct
19 Correct 4 ms 5200 KB Output is correct
20 Correct 3 ms 5072 KB Output is correct
21 Correct 3 ms 5128 KB Output is correct
22 Correct 4 ms 5072 KB Output is correct
23 Correct 4 ms 5328 KB Output is correct
24 Correct 4 ms 5200 KB Output is correct
25 Correct 4 ms 5188 KB Output is correct
26 Correct 3 ms 5072 KB Output is correct
27 Correct 3 ms 5024 KB Output is correct
28 Correct 3 ms 5328 KB Output is correct
29 Correct 4 ms 5328 KB Output is correct
30 Correct 3 ms 5072 KB Output is correct
31 Correct 3 ms 5328 KB Output is correct
32 Correct 5 ms 5200 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4940 KB Output is correct
2 Correct 3 ms 4940 KB Output is correct
3 Correct 3 ms 4940 KB Output is correct
4 Correct 3 ms 4944 KB Output is correct
5 Correct 3 ms 4944 KB Output is correct
6 Correct 4 ms 4944 KB Output is correct
7 Correct 3 ms 5004 KB Output is correct
8 Correct 3 ms 4944 KB Output is correct
9 Correct 3 ms 4944 KB Output is correct
10 Correct 3 ms 4984 KB Output is correct
11 Correct 3 ms 4944 KB Output is correct
12 Correct 2 ms 4944 KB Output is correct
13 Correct 2 ms 4944 KB Output is correct
14 Correct 3 ms 4944 KB Output is correct
15 Correct 3 ms 4996 KB Output is correct
16 Correct 3 ms 4944 KB Output is correct
17 Correct 5 ms 5200 KB Output is correct
18 Correct 6 ms 5328 KB Output is correct
19 Correct 4 ms 5200 KB Output is correct
20 Correct 3 ms 5072 KB Output is correct
21 Correct 3 ms 5128 KB Output is correct
22 Correct 4 ms 5072 KB Output is correct
23 Correct 4 ms 5328 KB Output is correct
24 Correct 4 ms 5200 KB Output is correct
25 Correct 4 ms 5188 KB Output is correct
26 Correct 3 ms 5072 KB Output is correct
27 Correct 3 ms 5024 KB Output is correct
28 Correct 3 ms 5328 KB Output is correct
29 Correct 4 ms 5328 KB Output is correct
30 Correct 3 ms 5072 KB Output is correct
31 Correct 3 ms 5328 KB Output is correct
32 Correct 5 ms 5200 KB Output is correct
33 Correct 123 ms 24524 KB Output is correct
34 Correct 63 ms 28320 KB Output is correct
35 Correct 116 ms 18652 KB Output is correct
36 Correct 194 ms 38804 KB Output is correct
37 Correct 22 ms 15912 KB Output is correct
38 Correct 220 ms 42748 KB Output is correct
39 Correct 211 ms 42748 KB Output is correct
40 Correct 269 ms 42720 KB Output is correct
41 Correct 205 ms 42760 KB Output is correct
42 Correct 200 ms 42768 KB Output is correct
43 Correct 216 ms 42680 KB Output is correct
44 Correct 198 ms 42736 KB Output is correct
45 Correct 203 ms 42748 KB Output is correct
46 Correct 237 ms 42716 KB Output is correct
47 Correct 198 ms 42720 KB Output is correct
48 Correct 71 ms 34880 KB Output is correct
49 Correct 105 ms 42756 KB Output is correct
50 Correct 28 ms 12580 KB Output is correct
51 Correct 31 ms 19632 KB Output is correct
52 Correct 16 ms 12596 KB Output is correct
53 Correct 109 ms 40824 KB Output is correct
54 Correct 77 ms 20476 KB Output is correct
55 Correct 187 ms 30836 KB Output is correct
56 Correct 98 ms 22008 KB Output is correct
57 Correct 163 ms 38096 KB Output is correct
58 Correct 26 ms 21716 KB Output is correct
59 Correct 34 ms 18232 KB Output is correct
60 Correct 71 ms 38408 KB Output is correct
61 Correct 71 ms 39772 KB Output is correct
62 Correct 50 ms 33148 KB Output is correct
63 Correct 59 ms 25736 KB Output is correct
64 Correct 61 ms 28572 KB Output is correct
65 Correct 71 ms 43268 KB Output is correct
66 Correct 47 ms 13812 KB Output is correct
67 Correct 54 ms 31464 KB Output is correct
68 Correct 130 ms 43816 KB Output is correct
69 Correct 24 ms 7824 KB Output is correct
70 Correct 10 ms 5452 KB Output is correct
71 Correct 48 ms 21440 KB Output is correct
72 Correct 73 ms 35472 KB Output is correct
73 Correct 138 ms 47116 KB Output is correct
74 Correct 152 ms 43548 KB Output is correct
75 Correct 121 ms 47032 KB Output is correct
76 Correct 124 ms 45884 KB Output is correct
77 Correct 162 ms 43920 KB Output is correct