답안 #96033

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
96033 2019-02-05T10:17:36 Z onjo0127 Cats or Dogs (JOI18_catdog) C++11
100 / 100
277 ms 32116 KB
#include "catdog.h"
#include <bits/stdc++.h>
using namespace std;
using ll = long long;

const ll INF = 1e9;

struct node {
    ll cc, cd, dc, dd;
};

struct segtree {
    vector<node> tree;
    segtree(int N) {
        tree = vector<node>(4*N, {0, INF, INF, 0});
    }
    void pull(int idx) {
        node l = tree[idx*2], r = tree[idx*2+1];
        tree[idx].cc = min({l.cc + r.cc, l.cc + 1 + r.dc, l.cd + 1 + r.cc, l.cd + r.dc});
        tree[idx].cd = min({l.cc + r.cd, l.cc + 1 + r.dd, l.cd + 1 + r.cd, l.cd + r.dd});
        tree[idx].dc = min({l.dc + r.cc, l.dc + 1 + r.dc, l.dd + 1 + r.cc, l.dd + r.dc});
        tree[idx].dd = min({l.dc + r.cd, l.dc + 1 + r.dd, l.dd + 1 + r.cd, l.dd + r.dd});
    }
    void upd(int idx, int s, int e, int p, ll cat, ll dog) {
        if(p < s || e < p) return;
        if(s == e) {
            tree[idx] = {cat, INF, INF, dog};
            return;
        }
        int m = s+e >> 1;
        upd(idx*2, s, m, p, cat, dog);
        upd(idx*2+1, m+1, e, p, cat, dog);
        pull(idx);
    }
};

int A[100009];
ll C[100009], D[100009];
vector<int> adj[100009];
vector<segtree> seg;
vector<vector<int> > paths;
int sz[100009], pid[100009], vid[100009];

int dfs(int x, int p) {
    int s = 1;
    for(auto& it: adj[x]) if(it != p) s += dfs(it, x);
    return sz[x] = s;
}

void makeHLD(int x, int p) {
    if(2*sz[x] >= sz[p] && x != 1) {
        pid[x] = pid[p];
        vid[x] = vid[p] + 1;
        paths[pid[x]].push_back(x);
    }
    else {
        pid[x] = paths.size();
        vid[x] = 1;
        paths.push_back({p, x});
    }
    for(auto& it: adj[x]) if(it != p) makeHLD(it, x);
}

void updHLD(int u, ll cat, ll dog) {
    int pd = pid[u], vd = vid[u], rtp = paths[pd][0], rt = paths[pd][1];
    node& srt = seg[pd].tree[1];
    ll crt = min(srt.cc, srt.cd), drt = min(srt.dc, srt.dd);
    C[rtp] -= min(crt, drt + 1);
    D[rtp] -= min(crt + 1, drt);
    seg[pd].upd(1, 1, (int)paths[pd].size() - 1, vd, cat, dog);
    crt = min(srt.cc, srt.cd); drt = min(srt.dc, srt.dd);
    C[rtp] += min(crt, drt + 1);
    D[rtp] += min(crt + 1, drt);
    if(rtp != 0) updHLD(rtp, (A[rtp] == 2 ? INF : C[rtp]), (A[rtp] == 1 ? INF : D[rtp]));
}

void initialize(int N, vector<int> A, vector<int> B) {
    for(int i=0; i<N-1; i++) {
        int u = A[i], v = B[i];
        adj[u].push_back(v);
        adj[v].push_back(u);
    }
    dfs(1, 1);
    makeHLD(1, 0);
    for(auto& it: paths) seg.push_back(segtree(it.size() - 1));
}

int ans() {
    node& srt = seg[0].tree[1];
    ll crt = min(srt.cc, srt.cd), drt = min(srt.dc, srt.dd);
    return (int)min((A[1] == 2 ? INF : crt), (A[1] == 1 ? INF : drt));
}

int cat(int v) {
    A[v] = 1;
    updHLD(v, C[v], 1e9);
    return ans();
}

int dog(int v) {
    A[v] = 2;
    updHLD(v, 1e9, D[v]);
    return ans();
}

int neighbor(int v) {
    A[v] = 0;
    updHLD(v, C[v], D[v]);
    return ans();
}

Compilation message

catdog.cpp: In member function 'void segtree::upd(int, int, int, int, ll, ll)':
catdog.cpp:30:18: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
         int m = s+e >> 1;
                 ~^~
catdog.cpp: In function 'void updHLD(int, ll, ll)':
catdog.cpp:65:55: warning: unused variable 'rt' [-Wunused-variable]
     int pd = pid[u], vd = vid[u], rtp = paths[pd][0], rt = paths[pd][1];
                                                       ^~
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 2680 KB Output is correct
2 Correct 4 ms 2680 KB Output is correct
3 Correct 4 ms 2680 KB Output is correct
4 Correct 4 ms 2680 KB Output is correct
5 Correct 3 ms 2680 KB Output is correct
6 Correct 3 ms 2680 KB Output is correct
7 Correct 3 ms 2680 KB Output is correct
8 Correct 3 ms 2680 KB Output is correct
9 Correct 3 ms 2680 KB Output is correct
10 Correct 3 ms 2680 KB Output is correct
11 Correct 4 ms 2680 KB Output is correct
12 Correct 3 ms 2680 KB Output is correct
13 Correct 4 ms 2680 KB Output is correct
14 Correct 3 ms 2680 KB Output is correct
15 Correct 4 ms 2652 KB Output is correct
16 Correct 4 ms 2680 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 2680 KB Output is correct
2 Correct 4 ms 2680 KB Output is correct
3 Correct 4 ms 2680 KB Output is correct
4 Correct 4 ms 2680 KB Output is correct
5 Correct 3 ms 2680 KB Output is correct
6 Correct 3 ms 2680 KB Output is correct
7 Correct 3 ms 2680 KB Output is correct
8 Correct 3 ms 2680 KB Output is correct
9 Correct 3 ms 2680 KB Output is correct
10 Correct 3 ms 2680 KB Output is correct
11 Correct 4 ms 2680 KB Output is correct
12 Correct 3 ms 2680 KB Output is correct
13 Correct 4 ms 2680 KB Output is correct
14 Correct 3 ms 2680 KB Output is correct
15 Correct 4 ms 2652 KB Output is correct
16 Correct 4 ms 2680 KB Output is correct
17 Correct 4 ms 2936 KB Output is correct
18 Correct 4 ms 2936 KB Output is correct
19 Correct 4 ms 2936 KB Output is correct
20 Correct 3 ms 2680 KB Output is correct
21 Correct 4 ms 2808 KB Output is correct
22 Correct 3 ms 2808 KB Output is correct
23 Correct 5 ms 2936 KB Output is correct
24 Correct 4 ms 2936 KB Output is correct
25 Correct 4 ms 2808 KB Output is correct
26 Correct 4 ms 2808 KB Output is correct
27 Correct 4 ms 2680 KB Output is correct
28 Correct 4 ms 2936 KB Output is correct
29 Correct 5 ms 2936 KB Output is correct
30 Correct 4 ms 2808 KB Output is correct
31 Correct 4 ms 2936 KB Output is correct
32 Correct 4 ms 2808 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 2680 KB Output is correct
2 Correct 4 ms 2680 KB Output is correct
3 Correct 4 ms 2680 KB Output is correct
4 Correct 4 ms 2680 KB Output is correct
5 Correct 3 ms 2680 KB Output is correct
6 Correct 3 ms 2680 KB Output is correct
7 Correct 3 ms 2680 KB Output is correct
8 Correct 3 ms 2680 KB Output is correct
9 Correct 3 ms 2680 KB Output is correct
10 Correct 3 ms 2680 KB Output is correct
11 Correct 4 ms 2680 KB Output is correct
12 Correct 3 ms 2680 KB Output is correct
13 Correct 4 ms 2680 KB Output is correct
14 Correct 3 ms 2680 KB Output is correct
15 Correct 4 ms 2652 KB Output is correct
16 Correct 4 ms 2680 KB Output is correct
17 Correct 4 ms 2936 KB Output is correct
18 Correct 4 ms 2936 KB Output is correct
19 Correct 4 ms 2936 KB Output is correct
20 Correct 3 ms 2680 KB Output is correct
21 Correct 4 ms 2808 KB Output is correct
22 Correct 3 ms 2808 KB Output is correct
23 Correct 5 ms 2936 KB Output is correct
24 Correct 4 ms 2936 KB Output is correct
25 Correct 4 ms 2808 KB Output is correct
26 Correct 4 ms 2808 KB Output is correct
27 Correct 4 ms 2680 KB Output is correct
28 Correct 4 ms 2936 KB Output is correct
29 Correct 5 ms 2936 KB Output is correct
30 Correct 4 ms 2808 KB Output is correct
31 Correct 4 ms 2936 KB Output is correct
32 Correct 4 ms 2808 KB Output is correct
33 Correct 118 ms 17344 KB Output is correct
34 Correct 81 ms 19532 KB Output is correct
35 Correct 93 ms 13112 KB Output is correct
36 Correct 225 ms 27764 KB Output is correct
37 Correct 34 ms 10676 KB Output is correct
38 Correct 247 ms 30620 KB Output is correct
39 Correct 269 ms 30756 KB Output is correct
40 Correct 277 ms 30608 KB Output is correct
41 Correct 233 ms 30492 KB Output is correct
42 Correct 259 ms 30492 KB Output is correct
43 Correct 239 ms 30620 KB Output is correct
44 Correct 252 ms 30620 KB Output is correct
45 Correct 237 ms 30748 KB Output is correct
46 Correct 195 ms 30492 KB Output is correct
47 Correct 198 ms 30620 KB Output is correct
48 Correct 112 ms 25468 KB Output is correct
49 Correct 110 ms 30840 KB Output is correct
50 Correct 27 ms 8752 KB Output is correct
51 Correct 42 ms 14016 KB Output is correct
52 Correct 20 ms 8496 KB Output is correct
53 Correct 127 ms 28708 KB Output is correct
54 Correct 67 ms 14248 KB Output is correct
55 Correct 157 ms 22068 KB Output is correct
56 Correct 206 ms 15448 KB Output is correct
57 Correct 153 ms 27116 KB Output is correct
58 Correct 41 ms 15144 KB Output is correct
59 Correct 38 ms 12676 KB Output is correct
60 Correct 103 ms 27756 KB Output is correct
61 Correct 98 ms 28756 KB Output is correct
62 Correct 82 ms 24764 KB Output is correct
63 Correct 50 ms 16504 KB Output is correct
64 Correct 52 ms 18676 KB Output is correct
65 Correct 107 ms 28340 KB Output is correct
66 Correct 46 ms 9080 KB Output is correct
67 Correct 75 ms 20600 KB Output is correct
68 Correct 141 ms 28748 KB Output is correct
69 Correct 23 ms 4956 KB Output is correct
70 Correct 8 ms 3064 KB Output is correct
71 Correct 54 ms 14272 KB Output is correct
72 Correct 86 ms 23792 KB Output is correct
73 Correct 168 ms 31868 KB Output is correct
74 Correct 169 ms 28400 KB Output is correct
75 Correct 125 ms 32116 KB Output is correct
76 Correct 124 ms 30680 KB Output is correct
77 Correct 152 ms 28856 KB Output is correct