Submission #956437

# Submission time Handle Problem Language Result Execution time Memory
956437 2024-04-02T00:22:50 Z caterpillow Cats or Dogs (JOI18_catdog) C++17
100 / 100
217 ms 66900 KB
#include <bits/stdc++.h>
#include "catdog.h"

using namespace std;

using ll = long long;
using pl = pair<ll, ll>;
#define vt vector
#define f first
#define s second
#define all(x) x.begin(), x.end() 
#define pb push_back
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define ROF(i, a, b) for (int i = (b) - 1; i >= (a); i--)
#define F0R(i, b) FOR (i, 0, b)
#define endl '\n'
#define debug(x) do{auto _x = x; cerr << #x << " = " << _x << endl;} while(0)
const ll INF = 1e9;

pl clamp(pl a) {
    return {min(a.f, a.s + 1), min(a.f + 1, a.s)};
}

pl operator+(const pl& a, const pl& b) {
    return {a.f + b.f, a.s + b.s};
}

pl operator-(const pl& a, const pl& b) {
    return {a.f - b.f, a.s - b.s};
}

struct Node {
    pl actual, cat, same, dog; // actual sum, extra cat, same, extra dog
    void build() {
        same = clamp(actual); // same case
        cat = clamp({actual.f + 1, actual.s}); // extra cat case
        dog = clamp({actual.f, actual.s + 1}); // extra dog case
    }
    inline pl cmb(pl a) const {
        if (a.f == a.s) return {a.f + same.f, a.s + same.s};
        else if (a.f > a.s) return {a.s + cat.f, a.s + cat.s};
        else return {a.f + dog.f, a.f + dog.s};
    }
    Node operator+(const Node& b) const {
        return {
            actual + b.actual,
            b.cmb(cat),
            b.cmb(same),
            b.cmb(dog)
        };
    }
    void operator+=(pl b) {
        actual.f += b.f;
        actual.s += b.s;
    }
    void operator-=(pl b) {
        actual.f -= b.f;
        actual.s -= b.s;
    }
};

const Node NID = {
    {0, 0},
    {1, 0},
    {0, 0},
    {0, 1}
};

// cursed reverse segtree
struct SegTree {
    int n;
    vt<Node> seg; 
    void init(int _n) {
        for (n = 1; n < _n; n *= 2);
        seg.resize(2 * n, NID);
    }
    Node query(int l, int r) {
        Node lhs = NID, rhs = NID;
        for (l += n, r += n + 1; l < r; l /= 2, r /= 2) {
            if (l & 1) lhs = seg[l++] + lhs;
            if (r & 1) rhs = rhs + seg[--r];
        }
        return rhs + lhs;
    }
    void pull(int i) {
        i += n;
        while (i > 1) i /= 2, seg[i] = seg[2 * i + 1] + seg[2 * i];
    }
    void upd(int i, pl val) {
        seg[i += n] += val;
        seg[i].build();
        pull(i - n);
    }
    Node& operator[](int i) {
        return seg[i + n];
    }
};

int n;
vt<int> state;

struct HLD {
    int n, t;
    vt<vt<int>> adj;
    vt<int> pos, root, sz, par, leaf;
    vt<pl> agg;
    SegTree seg;
    void init(int _n) {
        n = _n;
        adj.resize(n);
        agg.resize(n, {-1, -1});
        pos = root = sz = par = leaf = vt<int>(n);
        seg.init(n);
    }
    void ae(int u, int v) {
        adj[u].pb(v);
        adj[v].pb(u);
    }
    int dfs_sz(int u) {
        sz[u] = 1;
        for (int& v : adj[u]) {
            par[v] = u;
            adj[v].erase(find(all(adj[v]), u));
            sz[u] += dfs_sz(v);
            if (sz[v] > sz[adj[u][0]]) swap(v, adj[u][0]);
        }
        return sz[u];
    }
    int dfs_hld(int u, int rt) {
        root[u] = rt;
        pos[u] = t++;
        int res = -1;
        for (int v : adj[u]) {
            if (v == adj[u][0]) res = dfs_hld(v, rt);
            else dfs_hld(v, v);
        }
        if (res == -1) leaf[u] = u;
        else leaf[u] = res;
        return leaf[u];
    }
    void gen(int r = 0) {
        t = 0;
        par[r] = -1;
        dfs_sz(r);
        dfs_hld(r, r);
        F0R (u, n) {
            if (root[u] == u) agg[u] = {0, 0};
        }
    }
    void prop(int u) {
        while (true) {
            u = leaf[u]; // go to bottom of heavy path

            // calc updates subtree aggregate
            int rt = root[u];
            Node res = seg.query(pos[rt], pos[u]);

            // update aggregate and its parent
            int p = par[rt];

            if (p >= 0) seg[pos[p]] -= agg[rt];
            agg[rt] = res.same;
            if (p == -1) return;
            
            seg[pos[p]] += res.same;
            seg[pos[p]].build();
            seg.pull(pos[p]);

            u = p; // go to parent
        }
    }  
    ll update(int u, pl v) { // do stuff to update u
        seg.upd(pos[u], v);
        prop(u);
        return min(agg[0].f, agg[0].s);
    }
} hld;

/*

let dp[u] = {min cost to make u's subtree cat safe, min cost to make u's subtree dog safe}
the difference between them is at most 1

keep track of all the cases (extra cat, same, extra dog)
each node stores the sum of everything EXCEPT its heavy path
heavy path roots store its subtree aggregate

when performing an update, modify the current node
walk up from the BASE of each heavy path up to the root, updating subtree aggregates and their parents

*/

void initialize(int N, vt<int> A, vt<int> B) {
    n = N;
    state.resize(n);
    hld.init(n);
    F0R (i, n - 1) {
        int u = A[i] - 1, v = B[i] - 1;
        hld.ae(u, v);
    }
    hld.gen();
}

int cat(int v) {
    v--;
    state[v] = 1;
    return hld.update(v, {0, INF});
}

int dog(int v) {
    v--;
    state[v] = 2;
    return hld.update(v, {INF, 0});
}

int neighbor(int v) {
    v--;
    pl sus;
    if (state[v] == 1) sus = {0, -INF};
    else sus = {-INF, 0};
    state[v] = 0;
    return hld.update(v, sus);
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 ms 344 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 1 ms 600 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 ms 344 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 1 ms 600 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 1 ms 600 KB Output is correct
18 Correct 1 ms 604 KB Output is correct
19 Correct 1 ms 604 KB Output is correct
20 Correct 1 ms 344 KB Output is correct
21 Correct 1 ms 348 KB Output is correct
22 Correct 1 ms 348 KB Output is correct
23 Correct 1 ms 604 KB Output is correct
24 Correct 1 ms 604 KB Output is correct
25 Correct 1 ms 348 KB Output is correct
26 Correct 1 ms 348 KB Output is correct
27 Correct 1 ms 348 KB Output is correct
28 Correct 1 ms 860 KB Output is correct
29 Correct 1 ms 860 KB Output is correct
30 Correct 1 ms 348 KB Output is correct
31 Correct 1 ms 604 KB Output is correct
32 Correct 1 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 ms 344 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 1 ms 600 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 1 ms 600 KB Output is correct
18 Correct 1 ms 604 KB Output is correct
19 Correct 1 ms 604 KB Output is correct
20 Correct 1 ms 344 KB Output is correct
21 Correct 1 ms 348 KB Output is correct
22 Correct 1 ms 348 KB Output is correct
23 Correct 1 ms 604 KB Output is correct
24 Correct 1 ms 604 KB Output is correct
25 Correct 1 ms 348 KB Output is correct
26 Correct 1 ms 348 KB Output is correct
27 Correct 1 ms 348 KB Output is correct
28 Correct 1 ms 860 KB Output is correct
29 Correct 1 ms 860 KB Output is correct
30 Correct 1 ms 348 KB Output is correct
31 Correct 1 ms 604 KB Output is correct
32 Correct 1 ms 348 KB Output is correct
33 Correct 126 ms 14988 KB Output is correct
34 Correct 62 ms 15804 KB Output is correct
35 Correct 107 ms 13388 KB Output is correct
36 Correct 192 ms 27616 KB Output is correct
37 Correct 12 ms 7896 KB Output is correct
38 Correct 204 ms 28712 KB Output is correct
39 Correct 198 ms 28848 KB Output is correct
40 Correct 217 ms 28968 KB Output is correct
41 Correct 199 ms 28740 KB Output is correct
42 Correct 192 ms 28876 KB Output is correct
43 Correct 213 ms 28876 KB Output is correct
44 Correct 199 ms 28872 KB Output is correct
45 Correct 204 ms 28752 KB Output is correct
46 Correct 210 ms 28872 KB Output is correct
47 Correct 212 ms 28740 KB Output is correct
48 Correct 68 ms 25536 KB Output is correct
49 Correct 77 ms 27888 KB Output is correct
50 Correct 24 ms 6740 KB Output is correct
51 Correct 29 ms 12888 KB Output is correct
52 Correct 13 ms 6728 KB Output is correct
53 Correct 105 ms 27988 KB Output is correct
54 Correct 66 ms 13832 KB Output is correct
55 Correct 162 ms 25160 KB Output is correct
56 Correct 99 ms 14072 KB Output is correct
57 Correct 146 ms 27384 KB Output is correct
58 Correct 16 ms 13272 KB Output is correct
59 Correct 26 ms 8280 KB Output is correct
60 Correct 63 ms 26380 KB Output is correct
61 Correct 68 ms 26856 KB Output is correct
62 Correct 44 ms 25048 KB Output is correct
63 Correct 33 ms 27224 KB Output is correct
64 Correct 43 ms 34400 KB Output is correct
65 Correct 63 ms 55376 KB Output is correct
66 Correct 38 ms 12048 KB Output is correct
67 Correct 48 ms 43732 KB Output is correct
68 Correct 87 ms 53072 KB Output is correct
69 Correct 15 ms 4444 KB Output is correct
70 Correct 4 ms 860 KB Output is correct
71 Correct 41 ms 27712 KB Output is correct
72 Correct 63 ms 52472 KB Output is correct
73 Correct 114 ms 66800 KB Output is correct
74 Correct 108 ms 49244 KB Output is correct
75 Correct 101 ms 66900 KB Output is correct
76 Correct 98 ms 60120 KB Output is correct
77 Correct 104 ms 51032 KB Output is correct