Submission #978695

# Submission time Handle Problem Language Result Execution time Memory
978695 2024-05-09T14:07:06 Z peterandvoi Cats or Dogs (JOI18_catdog) C++17
100 / 100
210 ms 27988 KB
#include <bits/stdc++.h>

#include "catdog.h"

using namespace std;

#ifdef ngu
#include "debug.h"
#else
#define debug(...) 42
#endif

template<class A, class B>
bool minz(A &a, const B &b) {
    return a > b ? a = b, true : false;
}

const int INF = int(1e9);

struct Node {
    int dat[2][2];

    Node() {
        for (int i = 0; i < 2; ++i) {
            for (int j = 0; j < 2; ++j) {
                dat[i][j] = INF;
            }
        }
    }

    Node operator + (const Node &o) const {
        Node res;
        for (int i = 0; i < 2; ++i) {
            for (int j = 0; j < 2; ++j) {
                if (dat[i][j] != INF) {
                    for (int ii = 0; ii < 2; ++ii) {
                        for (int jj = 0; jj < 2; ++jj) {
                            if (o.dat[ii][jj] != INF) {
                                int v = dat[i][j] + o.dat[ii][jj] + (j ^ ii);
                                minz(res.dat[i][jj], v);
                            }
                        }
                    }
                }
            }
        }
        return res;
    }
};

struct Segtree {
    int n;
    vector<Node> s;

    Segtree() {};

    Segtree(int n): n(n) {
        s.resize(4 << __lg(n), Node());
    }

    void bld(int id, int l, int r) {
        if (l == r) {
            s[id].dat[0][0] = 0;
            s[id].dat[1][1] = 0;
            return;
        }
        int md = (l + r) / 2;
        bld(id * 2, l, md);
        bld(id * 2 + 1, md + 1, r);
        s[id] = s[id * 2] + s[id * 2 + 1];
    }

    void bld() {
        bld(1, 1, n);
    }

    void upd(int i, int C, int D, int x) {
        int id = 1, l = 1, r = n;
        while (l ^ r) {
            int md = (l + r) / 2;
            id *= 2;
            if (i <= md) {
                r = md;
            } else {
                l = md + 1;
                id += 1;
            }
        }
        for (int i = 0; i < 2; ++i) {
            for (int j = 0; j < 2; ++j) {
                s[id].dat[i][j] = INF;
            }
        }
        if (x != 0) {
            s[id].dat[0][0] = C;
        }
        if (x != 1) {
            s[id].dat[1][1] = D;
        }
        while (id > 1) {
            id /= 2;
            s[id] = s[id * 2] + s[id * 2 + 1];
        }
    }

    int get(int i) {
        return min(s[1].dat[i][0], s[1].dat[i][1]);
    }

    int res(int i) {
        return min(get(i), get(i ^ 1) + 1);
    }
};

const int N = int(1e5) + 5;

int n;
int pos[N], head[N], tail[N], sz[N], par[N], c[N];
int s[2][N], up[2][N];
vector<int> g[N];
Segtree tree[N];

void dfs(int u) {
    for (int &v : g[u]) {
        if (v == par[u]) {
            swap(v, g[u].back());
            g[u].pop_back();
            break;
        }
    }
    sz[u] = 1;
    for (int v : g[u]) {
        par[v] = u;
        dfs(v);
        sz[u] += sz[v];
    }
}

int order;

void hld(int u) {
    pos[u] = ++order;
    tail[head[u]] = order;
    if (g[u].size()) {
        int x = *max_element(g[u].begin(), g[u].end(), [&](int i, int j) {
            return sz[i] < sz[j];
        });
        head[x] = head[u];
        hld(x);
        for (int v : g[u]) {
            if (v ^ x) {
                head[v] = v;
                order = 0;
                hld(v);
            }
        }
    }
}

void app(int u) {
    while (head[u] != 1) {
        u = head[u];
        int p = par[u];
        for (int i = 0; i < 2; ++i) {
            up[i][p] -= s[i][u];
            s[i][u] = tree[u].res(i);
            up[i][p] += s[i][u];
        }
        tree[head[p]].upd(pos[p], up[0][p], up[1][p], c[p]);
        u = p;
    }
}

int upd(int v, int x) {
    c[v] = x;
    tree[head[v]].upd(pos[v], up[0][v], up[1][v], c[v]);
    app(v);
    return min(tree[1].get(0), tree[1].get(1));
}

int cat(int v) {
    return upd(v, 1);
}

int dog(int v) {
    return upd(v, 0);
}

int neighbor(int v) {
    return upd(v, 2);
}

void initialize(int x, std::vector<int> A, std::vector<int> B) {
    n = x;
    for (int i = 0; i + 1 < n; ++i) {
        int u = A[i], v = B[i];
        g[u].emplace_back(v);
        g[v].emplace_back(u);
    }
    dfs(1);
    head[1] = 1;
    hld(1);
    for (int i = 1; i <= n; ++i) {
        c[i] = 2;
        if (tail[i]) {
            tree[i] = Segtree(tail[i]);
            tree[i].bld();
        }
    }
}

#ifdef ngu
int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    freopen("test.inp", "r", stdin);
    freopen("test.out", "w", stdout);
    int n;
    cin >> n;
    vector<int> A, B;
    for (int i = 1; i < n; ++i) {
        int u, v;
        cin >> u >> v;
        A.emplace_back(u);
        B.emplace_back(v);
    }
    initialize(n, A, B);
    int q;
    cin >> q;
    while (q--) {
        int type, v;
        cin >> type >> v;
        cout << upd(v, type) << "\n";
    }
}
#endif
# Verdict Execution time Memory Grader output
1 Correct 4 ms 6744 KB Output is correct
2 Correct 1 ms 6748 KB Output is correct
3 Correct 1 ms 6748 KB Output is correct
4 Correct 3 ms 6748 KB Output is correct
5 Correct 2 ms 6580 KB Output is correct
6 Correct 2 ms 6744 KB Output is correct
7 Correct 2 ms 6748 KB Output is correct
8 Correct 1 ms 6748 KB Output is correct
9 Correct 1 ms 6748 KB Output is correct
10 Correct 2 ms 6748 KB Output is correct
11 Correct 2 ms 6760 KB Output is correct
12 Correct 2 ms 6748 KB Output is correct
13 Correct 2 ms 6748 KB Output is correct
14 Correct 1 ms 6748 KB Output is correct
15 Correct 2 ms 6748 KB Output is correct
16 Correct 1 ms 6748 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 6744 KB Output is correct
2 Correct 1 ms 6748 KB Output is correct
3 Correct 1 ms 6748 KB Output is correct
4 Correct 3 ms 6748 KB Output is correct
5 Correct 2 ms 6580 KB Output is correct
6 Correct 2 ms 6744 KB Output is correct
7 Correct 2 ms 6748 KB Output is correct
8 Correct 1 ms 6748 KB Output is correct
9 Correct 1 ms 6748 KB Output is correct
10 Correct 2 ms 6748 KB Output is correct
11 Correct 2 ms 6760 KB Output is correct
12 Correct 2 ms 6748 KB Output is correct
13 Correct 2 ms 6748 KB Output is correct
14 Correct 1 ms 6748 KB Output is correct
15 Correct 2 ms 6748 KB Output is correct
16 Correct 1 ms 6748 KB Output is correct
17 Correct 2 ms 6748 KB Output is correct
18 Correct 2 ms 6748 KB Output is correct
19 Correct 3 ms 6768 KB Output is correct
20 Correct 2 ms 6748 KB Output is correct
21 Correct 2 ms 6748 KB Output is correct
22 Correct 2 ms 6748 KB Output is correct
23 Correct 9 ms 6748 KB Output is correct
24 Correct 3 ms 6748 KB Output is correct
25 Correct 3 ms 6748 KB Output is correct
26 Correct 2 ms 6748 KB Output is correct
27 Correct 3 ms 6748 KB Output is correct
28 Correct 2 ms 6748 KB Output is correct
29 Correct 3 ms 6748 KB Output is correct
30 Correct 2 ms 6748 KB Output is correct
31 Correct 3 ms 6748 KB Output is correct
32 Correct 3 ms 6764 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 6744 KB Output is correct
2 Correct 1 ms 6748 KB Output is correct
3 Correct 1 ms 6748 KB Output is correct
4 Correct 3 ms 6748 KB Output is correct
5 Correct 2 ms 6580 KB Output is correct
6 Correct 2 ms 6744 KB Output is correct
7 Correct 2 ms 6748 KB Output is correct
8 Correct 1 ms 6748 KB Output is correct
9 Correct 1 ms 6748 KB Output is correct
10 Correct 2 ms 6748 KB Output is correct
11 Correct 2 ms 6760 KB Output is correct
12 Correct 2 ms 6748 KB Output is correct
13 Correct 2 ms 6748 KB Output is correct
14 Correct 1 ms 6748 KB Output is correct
15 Correct 2 ms 6748 KB Output is correct
16 Correct 1 ms 6748 KB Output is correct
17 Correct 2 ms 6748 KB Output is correct
18 Correct 2 ms 6748 KB Output is correct
19 Correct 3 ms 6768 KB Output is correct
20 Correct 2 ms 6748 KB Output is correct
21 Correct 2 ms 6748 KB Output is correct
22 Correct 2 ms 6748 KB Output is correct
23 Correct 9 ms 6748 KB Output is correct
24 Correct 3 ms 6748 KB Output is correct
25 Correct 3 ms 6748 KB Output is correct
26 Correct 2 ms 6748 KB Output is correct
27 Correct 3 ms 6748 KB Output is correct
28 Correct 2 ms 6748 KB Output is correct
29 Correct 3 ms 6748 KB Output is correct
30 Correct 2 ms 6748 KB Output is correct
31 Correct 3 ms 6748 KB Output is correct
32 Correct 3 ms 6764 KB Output is correct
33 Correct 105 ms 15548 KB Output is correct
34 Correct 49 ms 16208 KB Output is correct
35 Correct 93 ms 13384 KB Output is correct
36 Correct 153 ms 21632 KB Output is correct
37 Correct 15 ms 11100 KB Output is correct
38 Correct 174 ms 23400 KB Output is correct
39 Correct 174 ms 23360 KB Output is correct
40 Correct 210 ms 23412 KB Output is correct
41 Correct 171 ms 23416 KB Output is correct
42 Correct 172 ms 23408 KB Output is correct
43 Correct 208 ms 23408 KB Output is correct
44 Correct 165 ms 23388 KB Output is correct
45 Correct 178 ms 23428 KB Output is correct
46 Correct 172 ms 23360 KB Output is correct
47 Correct 198 ms 23400 KB Output is correct
48 Correct 48 ms 20116 KB Output is correct
49 Correct 82 ms 23292 KB Output is correct
50 Correct 20 ms 10356 KB Output is correct
51 Correct 31 ms 13240 KB Output is correct
52 Correct 10 ms 10072 KB Output is correct
53 Correct 89 ms 21588 KB Output is correct
54 Correct 69 ms 13356 KB Output is correct
55 Correct 134 ms 18504 KB Output is correct
56 Correct 87 ms 14480 KB Output is correct
57 Correct 117 ms 20880 KB Output is correct
58 Correct 18 ms 13604 KB Output is correct
59 Correct 21 ms 12632 KB Output is correct
60 Correct 52 ms 21372 KB Output is correct
61 Correct 57 ms 22056 KB Output is correct
62 Correct 36 ms 18856 KB Output is correct
63 Correct 32 ms 15772 KB Output is correct
64 Correct 35 ms 17840 KB Output is correct
65 Correct 46 ms 25180 KB Output is correct
66 Correct 45 ms 11356 KB Output is correct
67 Correct 53 ms 19376 KB Output is correct
68 Correct 87 ms 24676 KB Output is correct
69 Correct 20 ms 8476 KB Output is correct
70 Correct 6 ms 7000 KB Output is correct
71 Correct 40 ms 15504 KB Output is correct
72 Correct 59 ms 22996 KB Output is correct
73 Correct 130 ms 27984 KB Output is correct
74 Correct 127 ms 24344 KB Output is correct
75 Correct 110 ms 27988 KB Output is correct
76 Correct 106 ms 26708 KB Output is correct
77 Correct 125 ms 23888 KB Output is correct