Submission #304815

# Submission time Handle Problem Language Result Execution time Memory
304815 2020-09-22T00:17:40 Z gevacrt Cats or Dogs (JOI18_catdog) C++17
100 / 100
1038 ms 27128 KB
#include<bits/stdc++.h>
#include "catdog.h"
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;

typedef vector<int> vi;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<vi> vvi;
typedef vector<vii> vvii;

#define INF 100010
#define MOD 1000000007
#define all(x) x.begin(), x.end()

struct nda{
    int cc = 0, cd = INF;
    int dc = INF, dd = 0;
    bool eq(nda v){
        return (cc == v.cc) && (cd == v.cd) &&
        (dc == v.dc) && (dd == v.dd);
    }
};

nda mu(nda a, nda b){
    if(a.eq({-1,-1,-1,-1})) return b;
    if(b.eq({-1,-1,-1,-1})) return a;
    
    nda ans;
    ans.cc = min({a.cc+b.cc, a.cc+b.dc+1, a.cd+b.cc+1, a.cd+b.dc});
    ans.cd = min({a.cc+b.cd, a.cc+b.dd+1, a.cd+b.cd+1, a.cd+b.dd});
    ans.dc = min({a.dc+b.cc, a.dc+b.dc+1, a.dd+b.cc+1, a.dd+b.dc});
    ans.dd = min({a.dc+b.cd, a.dc+b.dd+1, a.dd+b.cd+1, a.dd+b.dd});
    return ans;
}

class SegTree{
    vector<nda> st; int l, r;

    void Update(int node, int l, int r, int L, int R, int v0, int v1){
        if(r < L or R < l) return;
        if(L <= l and r <= R){
            st[node].cc += v0;
            st[node].dd += v1;
            return;
        }

        int m = (l+r)>>1;
        Update(node*2, l, m, L, R, v0, v1);
        Update(node*2+1, m+1, r, L, R, v0, v1);
        st[node] = mu(st[node*2], st[node*2+1]);
        return;
    }

    nda Query(int node, int l, int r, int L, int R){
        if(r < L or R < l) return nda{-1,-1,-1,-1};
        if(L <= l and r <= R) return st[node];
        int m = (l+r)>>1;
        auto v1 = Query(node*2, l, m, L, R);
        auto v2 = Query(node*2+1, m+1, r, L, R);
        return mu(v1, v2);
    }

    public:
        SegTree(int l, int r){
            int sz = (r-l+1);
            st.resize(4*sz);
            this->l = l, this->r = r;
        }

        void Update(int L, int R, int v0, int v1){
            Update(1, l, r, L, R, v0, v1);
            return;
        }

        nda Query(int L, int R){
            return Query(1, l, r, L, R);
        }
};

class HLD{
    vi heavy, head, pos; // [next node, root of chain, index]
    vi parent, depth;
    int pos_timer = 0;
    SegTree ST;

    vi ende;

    int dfs(int u, const vvi &adj){
        int subtree_size = 1, max_size = 0;
        for(auto v : adj[u]){
            if(v == parent[u]) continue;
            parent[v] = u, depth[v] = depth[u]+1;
            int child_subtree_size = dfs(v, adj);
            subtree_size += child_subtree_size;
            if(max_size < child_subtree_size){
                max_size = child_subtree_size;
                heavy[u] = v;
            }
        }
        return subtree_size;
    }

    void decompose(int u, int h, const vvi &adj){
        head[u] = h; pos[u] = pos_timer++;
        ende[h] = pos[u];

        if(heavy[u] != -1)
            decompose(heavy[u], h, adj);
        for(auto v : adj[u]){
            if(v == parent[u] || v == heavy[u]) continue;
            decompose(v, v, adj);
        }
        return;
    }

    public:
        HLD(const vvi &adj) : ST(0, adj.size()-1){
            int N = adj.size(); // !!0 based indexing!!
            heavy = vi(N, -1), head = vi(N), pos = vi(N);
            parent = vi(N), depth = vi(N);
            ende = vi(N);

            dfs(0, adj);
            decompose(0, 0, adj);
        }

        int Update(int a, int v0, int v1){
            for(int u = a; true; u = parent[head[u]]){
                auto prev = ST.Query(pos[head[u]], ende[head[u]]);
                ST.Update(pos[u], pos[u], v0, v1);
                auto next = ST.Query(pos[head[u]], ende[head[u]]);
                v0 = min({next.cc, next.cd, next.dc+1, next.dd+1}) - 
                    min({prev.cc, prev.cd, prev.dc+1, prev.dd+1});
                v1 = min({next.dd, next.dc, next.cc+1, next.cd+1}) -
                    min({prev.dd, prev.dc, prev.cc+1, prev.cd+1});

                if(head[u] == 0){
                    return min({next.cc, next.cd, next.dc, next.dd});
                }
            }
            return -1; // impossible case
        }
};

vi col; HLD hvt(vvi(1));
void initialize(int n, vi a, vi b){
    vvi adj(n); col = vi(n, 0);
    for(int x = 0; x < n-1; x++){
        adj[a[x]-1].push_back(b[x]-1);
        adj[b[x]-1].push_back(a[x]-1);
    }
    hvt = HLD(adj);
    return;
}

// cat ==> 0
int cat(int v){
    v--; col[v] = 1;
    return hvt.Update(v, 0, INF);
}

// dog ==> 1
int dog(int v){
    v--; col[v] = 2;
    return hvt.Update(v, INF, 0);
}

int neighbor(int v){
    v--; int tp = col[v]; col[v] = 0;
    if(tp == 1) return hvt.Update(v, 0, -INF);
    return hvt.Update(v, -INF, 0);
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 256 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 1 ms 256 KB Output is correct
4 Correct 1 ms 256 KB Output is correct
5 Correct 1 ms 256 KB Output is correct
6 Correct 1 ms 256 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 1 ms 256 KB Output is correct
10 Correct 1 ms 256 KB Output is correct
11 Correct 1 ms 256 KB Output is correct
12 Correct 0 ms 256 KB Output is correct
13 Correct 0 ms 256 KB Output is correct
14 Correct 1 ms 256 KB Output is correct
15 Correct 1 ms 256 KB Output is correct
16 Correct 1 ms 256 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 256 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 1 ms 256 KB Output is correct
4 Correct 1 ms 256 KB Output is correct
5 Correct 1 ms 256 KB Output is correct
6 Correct 1 ms 256 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 1 ms 256 KB Output is correct
10 Correct 1 ms 256 KB Output is correct
11 Correct 1 ms 256 KB Output is correct
12 Correct 0 ms 256 KB Output is correct
13 Correct 0 ms 256 KB Output is correct
14 Correct 1 ms 256 KB Output is correct
15 Correct 1 ms 256 KB Output is correct
16 Correct 1 ms 256 KB Output is correct
17 Correct 4 ms 384 KB Output is correct
18 Correct 3 ms 512 KB Output is correct
19 Correct 2 ms 384 KB Output is correct
20 Correct 1 ms 384 KB Output is correct
21 Correct 1 ms 384 KB Output is correct
22 Correct 2 ms 384 KB Output is correct
23 Correct 5 ms 640 KB Output is correct
24 Correct 3 ms 512 KB Output is correct
25 Correct 3 ms 384 KB Output is correct
26 Correct 2 ms 384 KB Output is correct
27 Correct 1 ms 384 KB Output is correct
28 Correct 1 ms 512 KB Output is correct
29 Correct 2 ms 512 KB Output is correct
30 Correct 2 ms 360 KB Output is correct
31 Correct 1 ms 512 KB Output is correct
32 Correct 2 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 256 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 1 ms 256 KB Output is correct
4 Correct 1 ms 256 KB Output is correct
5 Correct 1 ms 256 KB Output is correct
6 Correct 1 ms 256 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 1 ms 256 KB Output is correct
10 Correct 1 ms 256 KB Output is correct
11 Correct 1 ms 256 KB Output is correct
12 Correct 0 ms 256 KB Output is correct
13 Correct 0 ms 256 KB Output is correct
14 Correct 1 ms 256 KB Output is correct
15 Correct 1 ms 256 KB Output is correct
16 Correct 1 ms 256 KB Output is correct
17 Correct 4 ms 384 KB Output is correct
18 Correct 3 ms 512 KB Output is correct
19 Correct 2 ms 384 KB Output is correct
20 Correct 1 ms 384 KB Output is correct
21 Correct 1 ms 384 KB Output is correct
22 Correct 2 ms 384 KB Output is correct
23 Correct 5 ms 640 KB Output is correct
24 Correct 3 ms 512 KB Output is correct
25 Correct 3 ms 384 KB Output is correct
26 Correct 2 ms 384 KB Output is correct
27 Correct 1 ms 384 KB Output is correct
28 Correct 1 ms 512 KB Output is correct
29 Correct 2 ms 512 KB Output is correct
30 Correct 2 ms 360 KB Output is correct
31 Correct 1 ms 512 KB Output is correct
32 Correct 2 ms 384 KB Output is correct
33 Correct 594 ms 10224 KB Output is correct
34 Correct 173 ms 11768 KB Output is correct
35 Correct 522 ms 7288 KB Output is correct
36 Correct 912 ms 17144 KB Output is correct
37 Correct 23 ms 5504 KB Output is correct
38 Correct 1038 ms 19192 KB Output is correct
39 Correct 997 ms 19192 KB Output is correct
40 Correct 982 ms 19192 KB Output is correct
41 Correct 1014 ms 19320 KB Output is correct
42 Correct 948 ms 19192 KB Output is correct
43 Correct 967 ms 19192 KB Output is correct
44 Correct 895 ms 19112 KB Output is correct
45 Correct 936 ms 19320 KB Output is correct
46 Correct 939 ms 19192 KB Output is correct
47 Correct 979 ms 19192 KB Output is correct
48 Correct 247 ms 13936 KB Output is correct
49 Correct 263 ms 17520 KB Output is correct
50 Correct 98 ms 3968 KB Output is correct
51 Correct 105 ms 7036 KB Output is correct
52 Correct 40 ms 3712 KB Output is correct
53 Correct 385 ms 18168 KB Output is correct
54 Correct 283 ms 8056 KB Output is correct
55 Correct 783 ms 13304 KB Output is correct
56 Correct 471 ms 8952 KB Output is correct
57 Correct 641 ms 16888 KB Output is correct
58 Correct 40 ms 7804 KB Output is correct
59 Correct 93 ms 6400 KB Output is correct
60 Correct 216 ms 15728 KB Output is correct
61 Correct 263 ms 16456 KB Output is correct
62 Correct 134 ms 13168 KB Output is correct
63 Correct 72 ms 12536 KB Output is correct
64 Correct 75 ms 14584 KB Output is correct
65 Correct 95 ms 23032 KB Output is correct
66 Correct 117 ms 5628 KB Output is correct
67 Correct 109 ms 16376 KB Output is correct
68 Correct 234 ms 23544 KB Output is correct
69 Correct 61 ms 2176 KB Output is correct
70 Correct 12 ms 640 KB Output is correct
71 Correct 97 ms 10744 KB Output is correct
72 Correct 134 ms 19192 KB Output is correct
73 Correct 364 ms 27128 KB Output is correct
74 Correct 443 ms 23288 KB Output is correct
75 Correct 273 ms 26744 KB Output is correct
76 Correct 268 ms 25464 KB Output is correct
77 Correct 449 ms 23672 KB Output is correct