답안 #1110505

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1110505 2024-11-09T15:22:06 Z BF001 Cats or Dogs (JOI18_catdog) C++17
100 / 100
1270 ms 36948 KB
#include<bits/stdc++.h>
using namespace std;
 
#define NN 100005
#define oo 1e6
#define fi first 
#define se second

typedef pair<int, int> ii;

int n, q, st[NN], dep[NN], hed[NN], id[NN], siz[NN], par[NN], cnt = 0;
vector<int> adj[NN];

struct segtree{
    struct  node
    {
        int a[2][2];
        node(){
            for (int i = 0; i <= 1; i++){
                for (int j = 0; j <= 1; j++) a[i][j] = oo;
            }
        }
        node operator + (node& o){
            node rt;
            for (int l = 0; l <= 1; l++){
                for (int i = 0; i <= 1; i++){
                    for (int j = 0; j <= 1; j++){
                        for (int r = 0; r <= 1; r++){
                            rt.a[l][r] = min(rt.a[l][r], a[l][i] + o.a[j][r] + (i ^ j));
                        }   
                    }
                }
            }
            return rt;
        }
    };
    vector<node> bit;
    int si;
    void init(int n){
        si = n;
        bit.resize(4 * n + 5);
        build(1, 1, si);
    }
    void build(int id, int l, int r){
        if (l == r){
            bit[id].a[0][0] = bit[id].a[1][1] = 0;
            return;
        }
        int mid = (l + r) >> 1;
        build(id * 2, l, mid);
        build(id * 2 + 1, mid + 1, r);
        bit[id] = bit[id * 2] + bit[id * 2 + 1];
    }
    void ud(int id, int l, int r, int pos, int val, int val1){
        if (l > pos || r < pos) return;
        if (l == r){
            bit[id].a[0][0] += val;
            bit[id].a[1][1] += val1;
            return;
        }
        int mid = (l + r) >> 1;
        ud(id * 2, l, mid, pos, val, val1);
        ud(id * 2 + 1, mid + 1, r, pos, val, val1);
        bit[id] = bit[id * 2] + bit[id * 2 + 1];
    }
    void ud(int pos, int val, int val1){
        ud(1, 1, si, pos, val, val1);
    }
} stree[NN];
 
void dfs(int u, int p){
    siz[u] = 1;
    par[u] = p;
    dep[u] = dep[p] + 1;
    for (auto x : adj[u]){
        if (x == p) continue;
        dfs(x, u);
        siz[u] += siz[x];
    }
}

void init(int u, int top){
    hed[u] = top;
    id[u] = ++cnt;
    int hv = -1;
    for (auto x : adj[u]){
        if (x == par[u]) continue;
        if (hv == -1 || siz[u] > siz[hv]) hv = x;
    }
    if (hv != -1) init(hv, top);
    else stree[top].init(cnt);
    for (auto x : adj[u]){
        if (x == par[u] || x == hv) continue;
        cnt = 0;
        init(x, x);
    }
}

ii gt(int u){
    int a = min(stree[hed[u]].bit[1].a[0][0], stree[hed[u]].bit[1].a[0][1]);
    int b = min(stree[hed[u]].bit[1].a[1][1], stree[hed[u]].bit[1].a[1][0]);
    return {min(a, b + 1), min(a + 1, b)};
}

void ud(int u, int val1, int val2){
    if (u == 0) return;
    ii a = gt(u);
    stree[hed[u]].ud(id[u], val1, val2);
    
    ii b = gt(u);
    ud(par[hed[u]], b.fi - a.fi, b.se - a.se);
}

int res(){
    return min({stree[1].bit[1].a[0][0], stree[1].bit[1].a[0][1], stree[1].bit[1].a[1][0], stree[1].bit[1].a[1][1]});
}

int cat(int u){
    st[u] = 1;
    ud(u, 0, oo);
    return res();
}

int dog(int u){
    st[u] = 2;
    ud(u, oo, 0);
    return res();
}

int neighbor(int u){
    if (st[u] == 0) return res();
    if (st[u] == 1){
        ud(u, 0, -oo);
        st[u] = 0;
        return res();
    }
    ud(u, -oo, 0);
    st[u] = 0;
    return res();
}

void initialize(int N, vector<int> A, vector<int> B){
    n = N;
    for (int i = 0; i < n - 1; i++){
        adj[A[i]].push_back(B[i]);
        adj[B[i]].push_back(A[i]);
    }
    dfs(1, 0);
    init(1, 1);
}
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 6736 KB Output is correct
2 Correct 2 ms 6736 KB Output is correct
3 Correct 2 ms 6736 KB Output is correct
4 Correct 2 ms 6736 KB Output is correct
5 Correct 2 ms 6736 KB Output is correct
6 Correct 2 ms 6736 KB Output is correct
7 Correct 2 ms 6736 KB Output is correct
8 Correct 2 ms 6736 KB Output is correct
9 Correct 2 ms 6736 KB Output is correct
10 Correct 2 ms 6736 KB Output is correct
11 Correct 2 ms 6736 KB Output is correct
12 Correct 2 ms 6904 KB Output is correct
13 Correct 2 ms 6904 KB Output is correct
14 Correct 2 ms 6736 KB Output is correct
15 Correct 2 ms 6736 KB Output is correct
16 Correct 2 ms 6736 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 6736 KB Output is correct
2 Correct 2 ms 6736 KB Output is correct
3 Correct 2 ms 6736 KB Output is correct
4 Correct 2 ms 6736 KB Output is correct
5 Correct 2 ms 6736 KB Output is correct
6 Correct 2 ms 6736 KB Output is correct
7 Correct 2 ms 6736 KB Output is correct
8 Correct 2 ms 6736 KB Output is correct
9 Correct 2 ms 6736 KB Output is correct
10 Correct 2 ms 6736 KB Output is correct
11 Correct 2 ms 6736 KB Output is correct
12 Correct 2 ms 6904 KB Output is correct
13 Correct 2 ms 6904 KB Output is correct
14 Correct 2 ms 6736 KB Output is correct
15 Correct 2 ms 6736 KB Output is correct
16 Correct 2 ms 6736 KB Output is correct
17 Correct 3 ms 6736 KB Output is correct
18 Correct 3 ms 6736 KB Output is correct
19 Correct 3 ms 6736 KB Output is correct
20 Correct 2 ms 6736 KB Output is correct
21 Correct 3 ms 6736 KB Output is correct
22 Correct 3 ms 6992 KB Output is correct
23 Correct 3 ms 6736 KB Output is correct
24 Correct 3 ms 6736 KB Output is correct
25 Correct 3 ms 6736 KB Output is correct
26 Correct 2 ms 6736 KB Output is correct
27 Correct 2 ms 6904 KB Output is correct
28 Correct 2 ms 6736 KB Output is correct
29 Correct 3 ms 6992 KB Output is correct
30 Correct 2 ms 6736 KB Output is correct
31 Correct 3 ms 6736 KB Output is correct
32 Correct 3 ms 6736 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 6736 KB Output is correct
2 Correct 2 ms 6736 KB Output is correct
3 Correct 2 ms 6736 KB Output is correct
4 Correct 2 ms 6736 KB Output is correct
5 Correct 2 ms 6736 KB Output is correct
6 Correct 2 ms 6736 KB Output is correct
7 Correct 2 ms 6736 KB Output is correct
8 Correct 2 ms 6736 KB Output is correct
9 Correct 2 ms 6736 KB Output is correct
10 Correct 2 ms 6736 KB Output is correct
11 Correct 2 ms 6736 KB Output is correct
12 Correct 2 ms 6904 KB Output is correct
13 Correct 2 ms 6904 KB Output is correct
14 Correct 2 ms 6736 KB Output is correct
15 Correct 2 ms 6736 KB Output is correct
16 Correct 2 ms 6736 KB Output is correct
17 Correct 3 ms 6736 KB Output is correct
18 Correct 3 ms 6736 KB Output is correct
19 Correct 3 ms 6736 KB Output is correct
20 Correct 2 ms 6736 KB Output is correct
21 Correct 3 ms 6736 KB Output is correct
22 Correct 3 ms 6992 KB Output is correct
23 Correct 3 ms 6736 KB Output is correct
24 Correct 3 ms 6736 KB Output is correct
25 Correct 3 ms 6736 KB Output is correct
26 Correct 2 ms 6736 KB Output is correct
27 Correct 2 ms 6904 KB Output is correct
28 Correct 2 ms 6736 KB Output is correct
29 Correct 3 ms 6992 KB Output is correct
30 Correct 2 ms 6736 KB Output is correct
31 Correct 3 ms 6736 KB Output is correct
32 Correct 3 ms 6736 KB Output is correct
33 Correct 338 ms 16836 KB Output is correct
34 Correct 125 ms 18004 KB Output is correct
35 Correct 526 ms 14120 KB Output is correct
36 Correct 566 ms 23888 KB Output is correct
37 Correct 17 ms 11856 KB Output is correct
38 Correct 568 ms 25944 KB Output is correct
39 Correct 542 ms 25908 KB Output is correct
40 Correct 693 ms 25908 KB Output is correct
41 Correct 683 ms 25912 KB Output is correct
42 Correct 925 ms 25952 KB Output is correct
43 Correct 704 ms 25908 KB Output is correct
44 Correct 793 ms 26108 KB Output is correct
45 Correct 1270 ms 25980 KB Output is correct
46 Correct 713 ms 25840 KB Output is correct
47 Correct 593 ms 25952 KB Output is correct
48 Correct 58 ms 24464 KB Output is correct
49 Correct 84 ms 28912 KB Output is correct
50 Correct 20 ms 11336 KB Output is correct
51 Correct 27 ms 15308 KB Output is correct
52 Correct 14 ms 11088 KB Output is correct
53 Correct 276 ms 24136 KB Output is correct
54 Correct 201 ms 14416 KB Output is correct
55 Correct 514 ms 20184 KB Output is correct
56 Correct 328 ms 15656 KB Output is correct
57 Correct 443 ms 23164 KB Output is correct
58 Correct 24 ms 16332 KB Output is correct
59 Correct 27 ms 14672 KB Output is correct
60 Correct 81 ms 26440 KB Output is correct
61 Correct 65 ms 27440 KB Output is correct
62 Correct 62 ms 23248 KB Output is correct
63 Correct 38 ms 19016 KB Output is correct
64 Correct 63 ms 22352 KB Output is correct
65 Correct 56 ms 31072 KB Output is correct
66 Correct 64 ms 12368 KB Output is correct
67 Correct 56 ms 23884 KB Output is correct
68 Correct 101 ms 30792 KB Output is correct
69 Correct 27 ms 8928 KB Output is correct
70 Correct 6 ms 6992 KB Output is correct
71 Correct 45 ms 18288 KB Output is correct
72 Correct 78 ms 27984 KB Output is correct
73 Correct 124 ms 36948 KB Output is correct
74 Correct 199 ms 29632 KB Output is correct
75 Correct 382 ms 36692 KB Output is correct
76 Correct 360 ms 34084 KB Output is correct
77 Correct 214 ms 30536 KB Output is correct