Submission #894169

# Submission time Handle Problem Language Result Execution time Memory
894169 2023-12-28T02:14:36 Z vovik Cats or Dogs (JOI18_catdog) C++14
100 / 100
95 ms 25680 KB
#pragma GCC optimize(3)
#include <bits/stdc++.h>

void initialize(int N, std::vector<int> A, std::vector<int> B);

int cat(int v), dog(int v), neighbor(int v);

struct node {
    int a00, a01, a10, a11;
};

struct segTree {
    int n;
    std::vector<node> t;

    node merge(node a, node b) {
        node c;
        return c.a00 = std::min(a.a00 + b.a00, a.a01 + b.a10), c.a01 = std::min(a.a00 + b.a01, a.a01 + b.a11), c.a10 = std::min(a.a10 + b.a00, a.a11 + b.a10), c.a11 = std::min(a.a10 + b.a01, a.a11 + b.a11), c;
    }

    segTree() = default;

    segTree(int n) {
        while (__builtin_popcount(n) != 1) ++n;
        this->n = n, t = std::vector<node>(n * 2);
        for (auto&f: t) f.a01 = f.a10 = 1;
    }

    void modify(int i, int&x, int&y) {
        i += n, t[i].a00 += x, t[i].a01 += x, t[i].a10 += y, t[i].a11 += y;
        while (i /= 2) t[i] = merge(t[i * 2], t[i * 2 + 1]);
    }

    std::pair<int, int> get() {
        int x = std::min(t[1].a00, t[1].a01), y = std::min(t[1].a10, t[1].a11);
        return {std::min(x, y + 1), std::min(x + 1, y)};
    }
};

segTree ok[100005];

int pos[100005], p[100005], f[100005], sz[100005], len[100005], tp[100005];

std::vector<int> g[100005];

void cfs(int v = 1, int p = -1) {
    for (int i = 0; i < g[v].size(); ++i) if (g[v][i] == p) g[v].erase(g[v].begin() + i);
    sz[v] = 1;
    for (auto u: g[v]) if (u != p) cfs(u, v), sz[v] += sz[u], ::p[u] = v;
    std::nth_element(g[v].begin(), g[v].begin(), g[v].end(), [](const int&v, const int&u) { return sz[v] > sz[u]; });
}

void dfs(int v = 1, int s = 1) {
    f[v] = s, pos[v] = len[s]++;
    for (auto u: g[v]) dfs(u, u == g[v].front() ? s : u);
}

void initialize(int N, std::vector<int> A, std::vector<int> B) {
    for (int i = 0; i < A.size(); ++i) g[A[i]].push_back(B[i]), g[B[i]].push_back(A[i]);
    cfs(), dfs();
    for (int v = 1; v <= N; ++v) if (!pos[v]) ok[v] = len[v];
}

void modify(int v, int x, int y) {
    while (v) {
        auto [w0, w1] = ok[f[v]].get();
        ok[f[v]].modify(pos[v], x, y);
        auto [n0, n1] = ok[f[v]].get();
        x = n0 - w0, y = n1 - w1, v = p[f[v]];
    }
}

int query() {
    auto [a, b] = ok[1].get();
    return std::min(a, b);
}

int cat(int v) { return tp[v] = 1, modify(v, 1e9, 0), query(); }
int dog(int v) { return tp[v] = 2, modify(v, 0, 1e9), query(); }
int neighbor(int v) { return modify(v, -1e9 * (tp[v] == 1), -1e9 * (tp[v] == 2)), tp[v] = 0, query(); }

Compilation message

catdog.cpp: In function 'void cfs(int, int)':
catdog.cpp:47:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   47 |     for (int i = 0; i < g[v].size(); ++i) if (g[v][i] == p) g[v].erase(g[v].begin() + i);
      |                     ~~^~~~~~~~~~~~~
catdog.cpp: In function 'void initialize(int, std::vector<int>, std::vector<int>)':
catdog.cpp:59:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   59 |     for (int i = 0; i < A.size(); ++i) g[A[i]].push_back(B[i]), g[B[i]].push_back(A[i]);
      |                     ~~^~~~~~~~~~
catdog.cpp: In function 'void modify(int, int, int)':
catdog.cpp:66:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   66 |         auto [w0, w1] = ok[f[v]].get();
      |              ^
catdog.cpp:68:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   68 |         auto [n0, n1] = ok[f[v]].get();
      |              ^
catdog.cpp: In function 'int query()':
catdog.cpp:74:10: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   74 |     auto [a, b] = ok[1].get();
      |          ^
# Verdict Execution time Memory Grader output
1 Correct 2 ms 8024 KB Output is correct
2 Correct 2 ms 8028 KB Output is correct
3 Correct 2 ms 8028 KB Output is correct
4 Correct 2 ms 8248 KB Output is correct
5 Correct 3 ms 8028 KB Output is correct
6 Correct 2 ms 8028 KB Output is correct
7 Correct 2 ms 8028 KB Output is correct
8 Correct 2 ms 8028 KB Output is correct
9 Correct 2 ms 8024 KB Output is correct
10 Correct 2 ms 8024 KB Output is correct
11 Correct 2 ms 8028 KB Output is correct
12 Correct 2 ms 8028 KB Output is correct
13 Correct 2 ms 8028 KB Output is correct
14 Correct 2 ms 8028 KB Output is correct
15 Correct 3 ms 8028 KB Output is correct
16 Correct 2 ms 8028 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 8024 KB Output is correct
2 Correct 2 ms 8028 KB Output is correct
3 Correct 2 ms 8028 KB Output is correct
4 Correct 2 ms 8248 KB Output is correct
5 Correct 3 ms 8028 KB Output is correct
6 Correct 2 ms 8028 KB Output is correct
7 Correct 2 ms 8028 KB Output is correct
8 Correct 2 ms 8028 KB Output is correct
9 Correct 2 ms 8024 KB Output is correct
10 Correct 2 ms 8024 KB Output is correct
11 Correct 2 ms 8028 KB Output is correct
12 Correct 2 ms 8028 KB Output is correct
13 Correct 2 ms 8028 KB Output is correct
14 Correct 2 ms 8028 KB Output is correct
15 Correct 3 ms 8028 KB Output is correct
16 Correct 2 ms 8028 KB Output is correct
17 Correct 3 ms 8536 KB Output is correct
18 Correct 3 ms 8284 KB Output is correct
19 Correct 3 ms 8284 KB Output is correct
20 Correct 2 ms 8284 KB Output is correct
21 Correct 2 ms 8284 KB Output is correct
22 Correct 2 ms 8284 KB Output is correct
23 Correct 3 ms 8284 KB Output is correct
24 Correct 3 ms 8284 KB Output is correct
25 Correct 2 ms 8260 KB Output is correct
26 Correct 2 ms 8300 KB Output is correct
27 Correct 2 ms 8280 KB Output is correct
28 Correct 2 ms 8368 KB Output is correct
29 Correct 3 ms 8280 KB Output is correct
30 Correct 2 ms 8284 KB Output is correct
31 Correct 2 ms 8284 KB Output is correct
32 Correct 2 ms 8284 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 8024 KB Output is correct
2 Correct 2 ms 8028 KB Output is correct
3 Correct 2 ms 8028 KB Output is correct
4 Correct 2 ms 8248 KB Output is correct
5 Correct 3 ms 8028 KB Output is correct
6 Correct 2 ms 8028 KB Output is correct
7 Correct 2 ms 8028 KB Output is correct
8 Correct 2 ms 8028 KB Output is correct
9 Correct 2 ms 8024 KB Output is correct
10 Correct 2 ms 8024 KB Output is correct
11 Correct 2 ms 8028 KB Output is correct
12 Correct 2 ms 8028 KB Output is correct
13 Correct 2 ms 8028 KB Output is correct
14 Correct 2 ms 8028 KB Output is correct
15 Correct 3 ms 8028 KB Output is correct
16 Correct 2 ms 8028 KB Output is correct
17 Correct 3 ms 8536 KB Output is correct
18 Correct 3 ms 8284 KB Output is correct
19 Correct 3 ms 8284 KB Output is correct
20 Correct 2 ms 8284 KB Output is correct
21 Correct 2 ms 8284 KB Output is correct
22 Correct 2 ms 8284 KB Output is correct
23 Correct 3 ms 8284 KB Output is correct
24 Correct 3 ms 8284 KB Output is correct
25 Correct 2 ms 8260 KB Output is correct
26 Correct 2 ms 8300 KB Output is correct
27 Correct 2 ms 8280 KB Output is correct
28 Correct 2 ms 8368 KB Output is correct
29 Correct 3 ms 8280 KB Output is correct
30 Correct 2 ms 8284 KB Output is correct
31 Correct 2 ms 8284 KB Output is correct
32 Correct 2 ms 8284 KB Output is correct
33 Correct 55 ms 13508 KB Output is correct
34 Correct 31 ms 14288 KB Output is correct
35 Correct 45 ms 12364 KB Output is correct
36 Correct 81 ms 17224 KB Output is correct
37 Correct 12 ms 10844 KB Output is correct
38 Correct 83 ms 18356 KB Output is correct
39 Correct 91 ms 18240 KB Output is correct
40 Correct 83 ms 18268 KB Output is correct
41 Correct 90 ms 18240 KB Output is correct
42 Correct 85 ms 18244 KB Output is correct
43 Correct 95 ms 18240 KB Output is correct
44 Correct 81 ms 18240 KB Output is correct
45 Correct 87 ms 18244 KB Output is correct
46 Correct 90 ms 18236 KB Output is correct
47 Correct 84 ms 18244 KB Output is correct
48 Correct 39 ms 15772 KB Output is correct
49 Correct 46 ms 17660 KB Output is correct
50 Correct 15 ms 10332 KB Output is correct
51 Correct 19 ms 11748 KB Output is correct
52 Correct 10 ms 10076 KB Output is correct
53 Correct 66 ms 17832 KB Output is correct
54 Correct 31 ms 12376 KB Output is correct
55 Correct 65 ms 15268 KB Output is correct
56 Correct 42 ms 12996 KB Output is correct
57 Correct 62 ms 17240 KB Output is correct
58 Correct 15 ms 12244 KB Output is correct
59 Correct 19 ms 11612 KB Output is correct
60 Correct 43 ms 16588 KB Output is correct
61 Correct 48 ms 16976 KB Output is correct
62 Correct 31 ms 15316 KB Output is correct
63 Correct 24 ms 15484 KB Output is correct
64 Correct 27 ms 17500 KB Output is correct
65 Correct 39 ms 23636 KB Output is correct
66 Correct 25 ms 11864 KB Output is correct
67 Correct 32 ms 18524 KB Output is correct
68 Correct 61 ms 22376 KB Output is correct
69 Correct 12 ms 9564 KB Output is correct
70 Correct 5 ms 8284 KB Output is correct
71 Correct 26 ms 15496 KB Output is correct
72 Correct 43 ms 21596 KB Output is correct
73 Correct 72 ms 25424 KB Output is correct
74 Correct 72 ms 21844 KB Output is correct
75 Correct 73 ms 25680 KB Output is correct
76 Correct 63 ms 25176 KB Output is correct
77 Correct 75 ms 22364 KB Output is correct