Submission #956440

# Submission time Handle Problem Language Result Execution time Memory
956440 2024-04-02T00:35:49 Z caterpillow Cats or Dogs (JOI18_catdog) C++17
100 / 100
244 ms 68472 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}); // assume an extra cat
        dog = clamp({actual.f, actual.s + 1}); // assume an extra dog 
    }
    inline pl cmb(pl a) const {
        if (a.f == a.s) return a + same; // same case
        else if (a.f > a.s) return pl {a.s, a.s} + cat; // extra cat case
        else return pl {a.f, a.f} + dog; // extra dog case
    }
    Node operator+(const Node& b) const {
        return {
            actual + b.actual,
            b.cmb(cat),
            b.cmb(same),
            b.cmb(dog)
        };
    }
    void operator+=(pl b) {
        actual = actual + b;
    }
    void operator-=(pl b) {
        actual = actual - b;
    }
};

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];
    }
};

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);
        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 = u;
        for (int v : adj[u]) {
            if (v == adj[u][0]) res = dfs_hld(v, rt);
            else dfs_hld(v, v);
        }
        return leaf[u] = res;
    }
    void gen(int r = 0) {
        t = 0;
        par[r] = -1;
        dfs_sz(r);
        dfs_hld(r, r);
    }
    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 child
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

*/

int n;
vt<int> state;

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 0 ms 348 KB Output is correct
2 Correct 1 ms 348 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 344 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 344 KB Output is correct
13 Correct 0 ms 344 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 ms 348 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 344 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 344 KB Output is correct
13 Correct 0 ms 344 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 1 ms 856 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 2 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 608 KB Output is correct
32 Correct 1 ms 352 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 ms 348 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 344 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 344 KB Output is correct
13 Correct 0 ms 344 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 1 ms 856 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 2 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 608 KB Output is correct
32 Correct 1 ms 352 KB Output is correct
33 Correct 112 ms 15048 KB Output is correct
34 Correct 63 ms 15956 KB Output is correct
35 Correct 104 ms 13380 KB Output is correct
36 Correct 187 ms 27612 KB Output is correct
37 Correct 12 ms 7772 KB Output is correct
38 Correct 244 ms 28836 KB Output is correct
39 Correct 195 ms 28852 KB Output is correct
40 Correct 201 ms 28740 KB Output is correct
41 Correct 213 ms 28988 KB Output is correct
42 Correct 189 ms 28864 KB Output is correct
43 Correct 213 ms 28740 KB Output is correct
44 Correct 218 ms 28892 KB Output is correct
45 Correct 224 ms 29012 KB Output is correct
46 Correct 198 ms 28760 KB Output is correct
47 Correct 209 ms 29000 KB Output is correct
48 Correct 68 ms 25448 KB Output is correct
49 Correct 77 ms 27596 KB Output is correct
50 Correct 24 ms 7000 KB Output is correct
51 Correct 29 ms 12888 KB Output is correct
52 Correct 13 ms 6748 KB Output is correct
53 Correct 131 ms 27988 KB Output is correct
54 Correct 62 ms 13468 KB Output is correct
55 Correct 165 ms 25164 KB Output is correct
56 Correct 98 ms 14220 KB Output is correct
57 Correct 141 ms 27216 KB Output is correct
58 Correct 18 ms 13272 KB Output is correct
59 Correct 26 ms 8440 KB Output is correct
60 Correct 70 ms 26580 KB Output is correct
61 Correct 71 ms 26828 KB Output is correct
62 Correct 54 ms 25112 KB Output is correct
63 Correct 34 ms 27728 KB Output is correct
64 Correct 39 ms 34896 KB Output is correct
65 Correct 66 ms 56400 KB Output is correct
66 Correct 33 ms 12228 KB Output is correct
67 Correct 64 ms 44624 KB Output is correct
68 Correct 90 ms 54096 KB Output is correct
69 Correct 15 ms 4444 KB Output is correct
70 Correct 4 ms 856 KB Output is correct
71 Correct 38 ms 28244 KB Output is correct
72 Correct 65 ms 53588 KB Output is correct
73 Correct 125 ms 68472 KB Output is correct
74 Correct 108 ms 50076 KB Output is correct
75 Correct 100 ms 68176 KB Output is correct
76 Correct 104 ms 61584 KB Output is correct
77 Correct 119 ms 52356 KB Output is correct