Submission #887373

# Submission time Handle Problem Language Result Execution time Memory
887373 2023-12-14T10:55:04 Z hmm789 Cats or Dogs (JOI18_catdog) C++14
100 / 100
145 ms 32880 KB
#include "catdog.h"
#include <bits/stdc++.h>
using namespace std;
#define INF 1000000000

struct node {
    int s, e, m, v[2][2];
    node *l, *r;
    node(int _s, int _e) {
        s = _s, e = _e, m = (s+e)/2;
        if(s != e) {
            v[0][0] = v[1][1] = 0;
            v[0][1] = v[1][0] = 1;
            l = new node(s, m);
            r = new node(m+1, e);
        } else {
            v[0][0] = v[1][1] = 0;
            v[0][1] = v[1][0] = INF;
        }
    }
    void update(int x, int a, int b) {
        if(s == e) {
            v[0][0] += a;
            v[1][1] += b;
            return;
        } else if(x > m) r->update(x, a, b);
        else l->update(x, a, b);
        for(int i = 0; i < 2; i++) for(int j = 0; j < 2; j++) v[i][j] = INF;
        for(int i = 0; i < 2; i++) for(int j = 0; j < 2; j++) for(int k = 0; k < 2; k++) for(int w = 0; w < 2; w++) {
            v[i][j] = min(v[i][j], l->v[i][k]+(k^w)+r->v[w][j]);
        }
    }
    pair<int, int> query() {
        int a = min(v[0][0], v[0][1]);
        int b = min(v[1][0], v[1][1]);
        return {min(a, b+1), min(b, a+1)};
    }
} *root[100000];

int n;
vector<int> adj[100000];
int heavy[100000], head[100000], pos[100000], par[100000], grp[100000], gsz[100000], cur=1;
int a[100000];

int dfs(int x, int p) {
    int sz = 1, mx = 0;
    par[x] = p;
    for(int i : adj[x]) if(i != p) {
        int child = dfs(i, x);
        sz += child;
        if(child > mx) {
            heavy[x] = i;
            mx = child;
        }
    }
    return sz;
}

void dfs2(int x, int h) {
    head[x] = h;
    grp[x] = grp[h];
    pos[x] = gsz[grp[x]]++;
    if(heavy[x] != -1) dfs2(heavy[x], h);
    for(int i : adj[x]) if(i != par[x] && i != heavy[x]) {
        grp[i] = cur++;
        dfs2(i, i);
    }
}

void initialize(int N, std::vector<int> A, std::vector<int> B) {
    n = N;
    for(int i = 0; i < n-1; i++) {
        A[i]--; B[i]--;
        adj[A[i]].push_back(B[i]);
        adj[B[i]].push_back(A[i]);
    }
    memset(heavy, -1, sizeof(heavy));
    dfs(0, -1);
    dfs2(0, 0);
    for(int i = 0; i < cur; i++) root[i] = new node(0, gsz[i]-1);
}

int update(int x, int a, int b) {
    pair<int, int> prv, cur;
    for(; x != -1; x = par[head[x]]) {
        prv = root[grp[x]]->query();
        root[grp[x]]->update(pos[x], a, b);
        cur = root[grp[x]]->query();
        a = cur.first-prv.first; b = cur.second-prv.second;
    }
    return min(cur.first, cur.second);
}

int cat(int v) {
    v--;
    a[v] = 1;
    return update(v, 0, n);
}

int dog(int v) {
    v--;
    a[v] = 2;
    return update(v, n, 0);
}

int neighbor(int v) {
    v--;
    if(a[v] == 1) {
        a[v] = 0;
        return update(v, 0, -n);
    } else {
        a[v] = 0;
        return update(v, -n, 0);
    }
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 5212 KB Output is correct
2 Correct 1 ms 5212 KB Output is correct
3 Correct 2 ms 5620 KB Output is correct
4 Correct 1 ms 5212 KB Output is correct
5 Correct 1 ms 5368 KB Output is correct
6 Correct 1 ms 5212 KB Output is correct
7 Correct 1 ms 5212 KB Output is correct
8 Correct 1 ms 5352 KB Output is correct
9 Correct 1 ms 5360 KB Output is correct
10 Correct 1 ms 5212 KB Output is correct
11 Correct 2 ms 5212 KB Output is correct
12 Correct 1 ms 5212 KB Output is correct
13 Correct 1 ms 5212 KB Output is correct
14 Correct 1 ms 5356 KB Output is correct
15 Correct 1 ms 5464 KB Output is correct
16 Correct 1 ms 5356 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 5212 KB Output is correct
2 Correct 1 ms 5212 KB Output is correct
3 Correct 2 ms 5620 KB Output is correct
4 Correct 1 ms 5212 KB Output is correct
5 Correct 1 ms 5368 KB Output is correct
6 Correct 1 ms 5212 KB Output is correct
7 Correct 1 ms 5212 KB Output is correct
8 Correct 1 ms 5352 KB Output is correct
9 Correct 1 ms 5360 KB Output is correct
10 Correct 1 ms 5212 KB Output is correct
11 Correct 2 ms 5212 KB Output is correct
12 Correct 1 ms 5212 KB Output is correct
13 Correct 1 ms 5212 KB Output is correct
14 Correct 1 ms 5356 KB Output is correct
15 Correct 1 ms 5464 KB Output is correct
16 Correct 1 ms 5356 KB Output is correct
17 Correct 2 ms 5468 KB Output is correct
18 Correct 2 ms 5276 KB Output is correct
19 Correct 2 ms 5464 KB Output is correct
20 Correct 2 ms 5208 KB Output is correct
21 Correct 1 ms 5212 KB Output is correct
22 Correct 1 ms 5212 KB Output is correct
23 Correct 2 ms 5468 KB Output is correct
24 Correct 2 ms 5408 KB Output is correct
25 Correct 2 ms 5212 KB Output is correct
26 Correct 1 ms 5212 KB Output is correct
27 Correct 1 ms 5212 KB Output is correct
28 Correct 2 ms 5468 KB Output is correct
29 Correct 2 ms 5368 KB Output is correct
30 Correct 1 ms 5212 KB Output is correct
31 Correct 1 ms 5468 KB Output is correct
32 Correct 1 ms 5212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 5212 KB Output is correct
2 Correct 1 ms 5212 KB Output is correct
3 Correct 2 ms 5620 KB Output is correct
4 Correct 1 ms 5212 KB Output is correct
5 Correct 1 ms 5368 KB Output is correct
6 Correct 1 ms 5212 KB Output is correct
7 Correct 1 ms 5212 KB Output is correct
8 Correct 1 ms 5352 KB Output is correct
9 Correct 1 ms 5360 KB Output is correct
10 Correct 1 ms 5212 KB Output is correct
11 Correct 2 ms 5212 KB Output is correct
12 Correct 1 ms 5212 KB Output is correct
13 Correct 1 ms 5212 KB Output is correct
14 Correct 1 ms 5356 KB Output is correct
15 Correct 1 ms 5464 KB Output is correct
16 Correct 1 ms 5356 KB Output is correct
17 Correct 2 ms 5468 KB Output is correct
18 Correct 2 ms 5276 KB Output is correct
19 Correct 2 ms 5464 KB Output is correct
20 Correct 2 ms 5208 KB Output is correct
21 Correct 1 ms 5212 KB Output is correct
22 Correct 1 ms 5212 KB Output is correct
23 Correct 2 ms 5468 KB Output is correct
24 Correct 2 ms 5408 KB Output is correct
25 Correct 2 ms 5212 KB Output is correct
26 Correct 1 ms 5212 KB Output is correct
27 Correct 1 ms 5212 KB Output is correct
28 Correct 2 ms 5468 KB Output is correct
29 Correct 2 ms 5368 KB Output is correct
30 Correct 1 ms 5212 KB Output is correct
31 Correct 1 ms 5468 KB Output is correct
32 Correct 1 ms 5212 KB Output is correct
33 Correct 80 ms 14932 KB Output is correct
34 Correct 42 ms 15952 KB Output is correct
35 Correct 74 ms 12516 KB Output is correct
36 Correct 123 ms 21620 KB Output is correct
37 Correct 12 ms 10332 KB Output is correct
38 Correct 136 ms 23404 KB Output is correct
39 Correct 133 ms 23364 KB Output is correct
40 Correct 136 ms 23356 KB Output is correct
41 Correct 136 ms 23360 KB Output is correct
42 Correct 145 ms 23620 KB Output is correct
43 Correct 131 ms 23360 KB Output is correct
44 Correct 131 ms 23416 KB Output is correct
45 Correct 131 ms 23392 KB Output is correct
46 Correct 133 ms 23412 KB Output is correct
47 Correct 139 ms 23340 KB Output is correct
48 Correct 42 ms 16028 KB Output is correct
49 Correct 48 ms 18604 KB Output is correct
50 Correct 17 ms 8284 KB Output is correct
51 Correct 19 ms 10452 KB Output is correct
52 Correct 10 ms 8028 KB Output is correct
53 Correct 77 ms 21968 KB Output is correct
54 Correct 58 ms 12624 KB Output is correct
55 Correct 116 ms 18088 KB Output is correct
56 Correct 70 ms 13816 KB Output is correct
57 Correct 97 ms 20852 KB Output is correct
58 Correct 15 ms 10796 KB Output is correct
59 Correct 21 ms 10184 KB Output is correct
60 Correct 52 ms 17204 KB Output is correct
61 Correct 56 ms 17760 KB Output is correct
62 Correct 33 ms 15096 KB Output is correct
63 Correct 44 ms 18004 KB Output is correct
64 Correct 34 ms 20048 KB Output is correct
65 Correct 48 ms 29012 KB Output is correct
66 Correct 35 ms 11100 KB Output is correct
67 Correct 43 ms 21844 KB Output is correct
68 Correct 80 ms 29692 KB Output is correct
69 Correct 18 ms 7256 KB Output is correct
70 Correct 5 ms 5484 KB Output is correct
71 Correct 35 ms 16028 KB Output is correct
72 Correct 55 ms 24916 KB Output is correct
73 Correct 105 ms 32880 KB Output is correct
74 Correct 112 ms 29420 KB Output is correct
75 Correct 99 ms 32756 KB Output is correct
76 Correct 97 ms 31528 KB Output is correct
77 Correct 122 ms 29796 KB Output is correct