Submission #1117334

# Submission time Handle Problem Language Result Execution time Memory
1117334 2024-11-23T10:31:22 Z Hamed_Ghaffari Cats or Dogs (JOI18_catdog) C++17
100 / 100
879 ms 23368 KB
#include<bits/stdc++.h>
#include "catdog.h"

using namespace std;

using pii = pair<int, int>;

#define Mp make_pair
#define X first
#define Y second
#define pb push_back
#define mins(a,b) (a = min(a,b))
#define lc id<<1
#define rc lc|1
#define mid ((l+r)/2)

const int MXN = 1e5+5;
const int INF = 1e9;

int n;

struct node {int dp[2][2];} seg[MXN<<2];
node merge(node x, node y) {
    node res;
    for(int i : {0,1})
        for(int j : {0,1})
            res.dp[i][j] = INF;
    for(bool i : {0,1})
        for(bool j : {0,1})
            for(bool k : {0,1})
                for(bool l : {0,1})
                    mins(res.dp[i][l], x.dp[i][j]+y.dp[k][l]+(j!=k));
    return res;
}

namespace Seg {
    void build(int l=0, int r=n, int id=1) {
        if(r-l == 1) {
            seg[id].dp[0][0] = seg[id].dp[1][1] = 0;
            seg[id].dp[0][1] = seg[id].dp[1][0] = INF;
            return;
        }
        build(l, mid, lc);
        build(mid, r, rc);
        seg[id] = merge(seg[lc], seg[rc]);
    }

    void upd(int p, pii x, int l=0, int r=n, int id=1) {
        if(r-l==1) {
            seg[id].dp[0][0] += x.X;
            seg[id].dp[1][1] += x.Y;
            return;
        }
        if(p<mid) upd(p, x, l, mid, lc);
        else upd(p, x, mid, r, rc);
        seg[id] = merge(seg[lc], seg[rc]);
    }

    node get(int s, int e, int l=0, int r=n, int id=1) {
        if(s<=l && r<=e) return seg[id];
        if(e<=mid) return get(s, e, l, mid, lc);
        if(s>=mid) return get(s, e, mid, r, rc);
        return merge(get(s, e, l, mid, lc), get(s, e, mid, r, rc));
    }
}

int sz[MXN], par[MXN], stt[MXN], fnt[MXN], head[MXN], timer;
bool is_cat[MXN], is_dog[MXN];
vector<int> g[MXN];

void dfs1(int v) {
    sz[v] = 1;
    for(int u : g[v])
        if(u^par[v]) {
            par[u] = v;
            dfs1(u);
            sz[v] += sz[u];
        }
}

void dfs2(int v) {
    if(!head[v]) head[v] = v;
    stt[v] = timer++;
    fnt[v] = timer;
    int big=-1;
    for(int u : g[v])
        if(u!=par[v] && (big==-1 || sz[big]<sz[u]))
            big = u;
    if(big != -1) {
        head[big] = head[v];
        dfs2(big);
        fnt[v] = fnt[big];
    }
    for(int u : g[v])
        if(u!=par[v] && u!=big)
            dfs2(u);
}

void upd(int v, int z) {
    v = head[v];
    if(v==1) return;
    node d = Seg::get(stt[v], fnt[v]);
    int C = min(d.dp[0][0], d.dp[0][1]);
    int D = min(d.dp[1][0], d.dp[1][1]);
    Seg::upd(stt[par[v]], Mp(z*min(C,D+1), z*min(C+1,D)));
}

void UPD(int v, pii x) {
    vector<int> stk;
    for(int u=v; head[u]!=1; u=par[head[u]])
        stk.pb(head[u]);
    while(!stk.empty()) {
        upd(stk.back(), -1);
        stk.pop_back();
    }
    Seg::upd(stt[v], x);
    for(int u=v; head[u]!=1; u=par[head[u]])
        upd(head[u], 1);
}

int ans() {
    node d = Seg::get(stt[1], fnt[1]);
    return min({d.dp[0][0], d.dp[0][1], d.dp[1][0], d.dp[1][1]});
}

void initialize(int N, std::vector<int> A, std::vector<int> B) {
    n = N;
    for(int i=0; i<n-1; i++) g[A[i]].pb(B[i]), g[B[i]].pb(A[i]);
    dfs1(1);
    dfs2(1);
    Seg::build();
}

int cat(int v) {
    is_cat[v] = 1;
    UPD(v, Mp(0, INF));
    return ans();
}

int dog(int v) {
    is_dog[v] = 1;
    UPD(v, Mp(INF, 0));
    return ans();
}

int neighbor(int v) {
    if(is_cat[v]) {
        is_cat[v] = 0;
        UPD(v, Mp(0, -INF));
    }
    if(is_dog[v]) {
        is_dog[v] = 0;
        UPD(v, Mp(-INF, 0));
    }
    return ans();
}
# Verdict Execution time Memory Grader output
1 Correct 3 ms 2640 KB Output is correct
2 Correct 2 ms 2640 KB Output is correct
3 Correct 2 ms 2640 KB Output is correct
4 Correct 2 ms 2640 KB Output is correct
5 Correct 2 ms 2640 KB Output is correct
6 Correct 2 ms 2640 KB Output is correct
7 Correct 2 ms 2640 KB Output is correct
8 Correct 2 ms 2640 KB Output is correct
9 Correct 3 ms 2640 KB Output is correct
10 Correct 2 ms 2640 KB Output is correct
11 Correct 2 ms 2808 KB Output is correct
12 Correct 2 ms 2640 KB Output is correct
13 Correct 3 ms 2812 KB Output is correct
14 Correct 4 ms 2640 KB Output is correct
15 Correct 2 ms 2640 KB Output is correct
16 Correct 2 ms 2640 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 2640 KB Output is correct
2 Correct 2 ms 2640 KB Output is correct
3 Correct 2 ms 2640 KB Output is correct
4 Correct 2 ms 2640 KB Output is correct
5 Correct 2 ms 2640 KB Output is correct
6 Correct 2 ms 2640 KB Output is correct
7 Correct 2 ms 2640 KB Output is correct
8 Correct 2 ms 2640 KB Output is correct
9 Correct 3 ms 2640 KB Output is correct
10 Correct 2 ms 2640 KB Output is correct
11 Correct 2 ms 2808 KB Output is correct
12 Correct 2 ms 2640 KB Output is correct
13 Correct 3 ms 2812 KB Output is correct
14 Correct 4 ms 2640 KB Output is correct
15 Correct 2 ms 2640 KB Output is correct
16 Correct 2 ms 2640 KB Output is correct
17 Correct 4 ms 2896 KB Output is correct
18 Correct 6 ms 2896 KB Output is correct
19 Correct 4 ms 2896 KB Output is correct
20 Correct 3 ms 2752 KB Output is correct
21 Correct 3 ms 2640 KB Output is correct
22 Correct 3 ms 2640 KB Output is correct
23 Correct 5 ms 2896 KB Output is correct
24 Correct 5 ms 2896 KB Output is correct
25 Correct 4 ms 2640 KB Output is correct
26 Correct 3 ms 2640 KB Output is correct
27 Correct 3 ms 2808 KB Output is correct
28 Correct 3 ms 2896 KB Output is correct
29 Correct 3 ms 2968 KB Output is correct
30 Correct 3 ms 2640 KB Output is correct
31 Correct 3 ms 2896 KB Output is correct
32 Correct 3 ms 2640 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 2640 KB Output is correct
2 Correct 2 ms 2640 KB Output is correct
3 Correct 2 ms 2640 KB Output is correct
4 Correct 2 ms 2640 KB Output is correct
5 Correct 2 ms 2640 KB Output is correct
6 Correct 2 ms 2640 KB Output is correct
7 Correct 2 ms 2640 KB Output is correct
8 Correct 2 ms 2640 KB Output is correct
9 Correct 3 ms 2640 KB Output is correct
10 Correct 2 ms 2640 KB Output is correct
11 Correct 2 ms 2808 KB Output is correct
12 Correct 2 ms 2640 KB Output is correct
13 Correct 3 ms 2812 KB Output is correct
14 Correct 4 ms 2640 KB Output is correct
15 Correct 2 ms 2640 KB Output is correct
16 Correct 2 ms 2640 KB Output is correct
17 Correct 4 ms 2896 KB Output is correct
18 Correct 6 ms 2896 KB Output is correct
19 Correct 4 ms 2896 KB Output is correct
20 Correct 3 ms 2752 KB Output is correct
21 Correct 3 ms 2640 KB Output is correct
22 Correct 3 ms 2640 KB Output is correct
23 Correct 5 ms 2896 KB Output is correct
24 Correct 5 ms 2896 KB Output is correct
25 Correct 4 ms 2640 KB Output is correct
26 Correct 3 ms 2640 KB Output is correct
27 Correct 3 ms 2808 KB Output is correct
28 Correct 3 ms 2896 KB Output is correct
29 Correct 3 ms 2968 KB Output is correct
30 Correct 3 ms 2640 KB Output is correct
31 Correct 3 ms 2896 KB Output is correct
32 Correct 3 ms 2640 KB Output is correct
33 Correct 523 ms 9072 KB Output is correct
34 Correct 135 ms 9296 KB Output is correct
35 Correct 476 ms 7744 KB Output is correct
36 Correct 796 ms 13968 KB Output is correct
37 Correct 17 ms 5844 KB Output is correct
38 Correct 879 ms 14644 KB Output is correct
39 Correct 830 ms 14708 KB Output is correct
40 Correct 797 ms 14724 KB Output is correct
41 Correct 822 ms 14712 KB Output is correct
42 Correct 736 ms 16504 KB Output is correct
43 Correct 791 ms 14704 KB Output is correct
44 Correct 770 ms 16444 KB Output is correct
45 Correct 800 ms 14648 KB Output is correct
46 Correct 806 ms 14708 KB Output is correct
47 Correct 864 ms 14704 KB Output is correct
48 Correct 192 ms 12688 KB Output is correct
49 Correct 217 ms 13808 KB Output is correct
50 Correct 80 ms 5448 KB Output is correct
51 Correct 86 ms 7596 KB Output is correct
52 Correct 33 ms 5552 KB Output is correct
53 Correct 322 ms 13640 KB Output is correct
54 Correct 263 ms 7936 KB Output is correct
55 Correct 709 ms 12416 KB Output is correct
56 Correct 444 ms 9200 KB Output is correct
57 Correct 558 ms 13352 KB Output is correct
58 Correct 26 ms 7884 KB Output is correct
59 Correct 68 ms 6224 KB Output is correct
60 Correct 158 ms 13760 KB Output is correct
61 Correct 173 ms 14436 KB Output is correct
62 Correct 105 ms 11968 KB Output is correct
63 Correct 52 ms 11440 KB Output is correct
64 Correct 46 ms 12624 KB Output is correct
65 Correct 60 ms 18940 KB Output is correct
66 Correct 88 ms 7248 KB Output is correct
67 Correct 77 ms 15484 KB Output is correct
68 Correct 157 ms 18988 KB Output is correct
69 Correct 39 ms 4344 KB Output is correct
70 Correct 9 ms 3068 KB Output is correct
71 Correct 71 ms 10648 KB Output is correct
72 Correct 101 ms 17488 KB Output is correct
73 Correct 320 ms 23368 KB Output is correct
74 Correct 453 ms 18440 KB Output is correct
75 Correct 169 ms 21832 KB Output is correct
76 Correct 160 ms 20572 KB Output is correct
77 Correct 419 ms 20148 KB Output is correct