Submission #97655

# Submission time Handle Problem Language Result Execution time Memory
97655 2019-02-17T16:27:48 Z Alexa2001 Cats or Dogs (JOI18_catdog) C++17
100 / 100
1380 ms 30752 KB
#include "catdog.h"
#include <bits/stdc++.h>
#define left_son (node<<1)
#define right_son ((node<<1)|1)
#define mid ((st+dr)>>1)

using namespace std;

const int Nmax = 1e5 + 5, inf = 1e6;

int n, tip[Nmax];

class SegmentTree
{
    int *go[2][2];
public:
    void build(int nr)
    {
        int i, j;
        for(i=0; i<2; ++i)
            for(j=0; j<2; ++j)
                go[i][j] = new int [nr << 2];
    }

    void build(int node, int st, int dr)
    {
        if(st == dr)
        {
            go[0][0][node] = go[1][1][node] = 0;
            go[0][1][node] = go[1][0][node] = inf;
            return;
        }
        build(left_son, st, mid);
        build(right_son, mid+1, dr);
        go[0][0][node] = go[1][1][node] = 0;
        go[0][1][node] = go[1][0][node] = 1;
    }

    void update(int node, int st, int dr, int pos, int a1, int a2)
    {
        if(st == dr)
        {
            go[0][0][node] += a1;
            go[1][1][node] += a2;
            return;
        }

        if(pos <= mid) update(left_son, st, mid, pos, a1, a2);
            else update(right_son, mid+1, dr, pos, a1, a2);

        int i, j, k, t;
        for(i=0; i<2; ++i)
            for(j=0; j<2; ++j)
            {
                int x = 10 * inf;
                for(k=0; k<2; ++k)
                    for(t=0; t<2; ++t)
                        x = min(x, go[i][k][left_son] + go[t][j][right_son] + (k != t));
                go[i][j][node] = x;
            }
    }

    int query0()
    {
        return min(go[0][0][1], go[0][1][1]);
    }
    int query1()
    {
        return min(go[1][0][1], go[1][1][1]);
    }
    int query()
    {
        return min(query1(), query0());
    }

} aint[Nmax];

namespace hp
{
    vector<int> v[Nmax], chain[Nmax];
    int nr = 0;
    int w[Nmax], t[Nmax], pos[Nmax], where[Nmax];

    void dfs(int node, int dad = 0)
    {
        w[node] = 1; t[node] = dad;
        for(auto it : v[node])
            if(it != dad)
            {
                w[node] += w[it];
                dfs(it, node);
            }
    }

    void make_chains(int node)
    {
        where[node] = nr;
        pos[node] = chain[nr].size();
        chain[nr].push_back(node);

        int xson = -1;
        for(auto it : v[node])
            if(it != t[node] && (xson == -1 || w[xson] < w[it])) xson = it;

        if(xson == -1) return;

        make_chains(xson);

        for(auto it : v[node])
            if(it != t[node] && it != xson)
            {
                ++nr;
                make_chains(it);
            }
    }

    void prepare()
    {
        dfs(1);
        nr = 1; make_chains(1);
        int i;
        for(i=1; i<=nr; ++i)
        {
            aint[i].build(chain[i].size());
            aint[i].build(1, 0, chain[i].size() - 1);
        }
    }

    void update(int node, int add1, int add2)
    {
        int T, ans0, ans1;
        int C = where[node];
        T = t[chain[C][0]];

        if(T)
            ans0 = aint[C].query0(), ans1 = aint[C].query1();

        aint[C].update(1, 0, chain[C].size() - 1, pos[node], add1, add2);

        if(!T) return;

        int ans2, ans3;
        ans2 = aint[C].query0();
        ans3 = aint[C].query1();

        update(T, min(ans2, ans3 + 1) - min(ans0, ans1 + 1), min(ans2 + 1, ans3) - min(ans0 + 1, ans1));
    }
}

void initialize(int N, vector<int> A, vector<int> B)
{
    int i;
    n = N;
    for(i=0; i<n-1; ++i)
    {
        hp :: v[A[i]].push_back(B[i]);
        hp :: v[B[i]].push_back(A[i]);
    }
    hp :: prepare();
}

int cat(int v) /// tip 1
{
    hp :: update(v, 0, inf);
    tip[v] = 1;
    return aint[1].query();
}

int dog(int v) /// tip 2
{
    hp :: update(v, inf, 0);
    tip[v] = 2;
    return aint[1].query();
}

int neighbor(int v) /// tip 0
{
    if(tip[v] == 1) hp :: update(v, 0, -inf);
        else hp :: update(v, -inf, 0);
    tip[v] = 0;
    return aint[1].query();
}

# Verdict Execution time Memory Grader output
1 Correct 6 ms 4992 KB Output is correct
2 Correct 7 ms 5120 KB Output is correct
3 Correct 6 ms 4992 KB Output is correct
4 Correct 8 ms 4992 KB Output is correct
5 Correct 8 ms 5120 KB Output is correct
6 Correct 7 ms 5120 KB Output is correct
7 Correct 6 ms 5120 KB Output is correct
8 Correct 7 ms 4992 KB Output is correct
9 Correct 7 ms 5120 KB Output is correct
10 Correct 7 ms 5120 KB Output is correct
11 Correct 7 ms 5120 KB Output is correct
12 Correct 7 ms 5120 KB Output is correct
13 Correct 9 ms 5120 KB Output is correct
14 Correct 7 ms 5120 KB Output is correct
15 Correct 5 ms 4992 KB Output is correct
16 Correct 6 ms 4992 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 4992 KB Output is correct
2 Correct 7 ms 5120 KB Output is correct
3 Correct 6 ms 4992 KB Output is correct
4 Correct 8 ms 4992 KB Output is correct
5 Correct 8 ms 5120 KB Output is correct
6 Correct 7 ms 5120 KB Output is correct
7 Correct 6 ms 5120 KB Output is correct
8 Correct 7 ms 4992 KB Output is correct
9 Correct 7 ms 5120 KB Output is correct
10 Correct 7 ms 5120 KB Output is correct
11 Correct 7 ms 5120 KB Output is correct
12 Correct 7 ms 5120 KB Output is correct
13 Correct 9 ms 5120 KB Output is correct
14 Correct 7 ms 5120 KB Output is correct
15 Correct 5 ms 4992 KB Output is correct
16 Correct 6 ms 4992 KB Output is correct
17 Correct 8 ms 5248 KB Output is correct
18 Correct 8 ms 5248 KB Output is correct
19 Correct 8 ms 5160 KB Output is correct
20 Correct 7 ms 5120 KB Output is correct
21 Correct 8 ms 5120 KB Output is correct
22 Correct 9 ms 5120 KB Output is correct
23 Correct 9 ms 5248 KB Output is correct
24 Correct 8 ms 5248 KB Output is correct
25 Correct 9 ms 5120 KB Output is correct
26 Correct 7 ms 5120 KB Output is correct
27 Correct 7 ms 5120 KB Output is correct
28 Correct 8 ms 5248 KB Output is correct
29 Correct 8 ms 5248 KB Output is correct
30 Correct 8 ms 5120 KB Output is correct
31 Correct 9 ms 5248 KB Output is correct
32 Correct 8 ms 5120 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 4992 KB Output is correct
2 Correct 7 ms 5120 KB Output is correct
3 Correct 6 ms 4992 KB Output is correct
4 Correct 8 ms 4992 KB Output is correct
5 Correct 8 ms 5120 KB Output is correct
6 Correct 7 ms 5120 KB Output is correct
7 Correct 6 ms 5120 KB Output is correct
8 Correct 7 ms 4992 KB Output is correct
9 Correct 7 ms 5120 KB Output is correct
10 Correct 7 ms 5120 KB Output is correct
11 Correct 7 ms 5120 KB Output is correct
12 Correct 7 ms 5120 KB Output is correct
13 Correct 9 ms 5120 KB Output is correct
14 Correct 7 ms 5120 KB Output is correct
15 Correct 5 ms 4992 KB Output is correct
16 Correct 6 ms 4992 KB Output is correct
17 Correct 8 ms 5248 KB Output is correct
18 Correct 8 ms 5248 KB Output is correct
19 Correct 8 ms 5160 KB Output is correct
20 Correct 7 ms 5120 KB Output is correct
21 Correct 8 ms 5120 KB Output is correct
22 Correct 9 ms 5120 KB Output is correct
23 Correct 9 ms 5248 KB Output is correct
24 Correct 8 ms 5248 KB Output is correct
25 Correct 9 ms 5120 KB Output is correct
26 Correct 7 ms 5120 KB Output is correct
27 Correct 7 ms 5120 KB Output is correct
28 Correct 8 ms 5248 KB Output is correct
29 Correct 8 ms 5248 KB Output is correct
30 Correct 8 ms 5120 KB Output is correct
31 Correct 9 ms 5248 KB Output is correct
32 Correct 8 ms 5120 KB Output is correct
33 Correct 407 ms 16256 KB Output is correct
34 Correct 213 ms 17528 KB Output is correct
35 Correct 578 ms 13272 KB Output is correct
36 Correct 867 ms 24132 KB Output is correct
37 Correct 37 ms 10872 KB Output is correct
38 Correct 733 ms 26216 KB Output is correct
39 Correct 950 ms 26220 KB Output is correct
40 Correct 1159 ms 26100 KB Output is correct
41 Correct 950 ms 26148 KB Output is correct
42 Correct 1260 ms 26216 KB Output is correct
43 Correct 1036 ms 26216 KB Output is correct
44 Correct 1243 ms 26084 KB Output is correct
45 Correct 1380 ms 26212 KB Output is correct
46 Correct 1014 ms 26340 KB Output is correct
47 Correct 781 ms 26088 KB Output is correct
48 Correct 170 ms 25520 KB Output is correct
49 Correct 229 ms 30752 KB Output is correct
50 Correct 41 ms 10360 KB Output is correct
51 Correct 82 ms 15068 KB Output is correct
52 Correct 27 ms 10232 KB Output is correct
53 Correct 487 ms 24440 KB Output is correct
54 Correct 312 ms 13744 KB Output is correct
55 Correct 885 ms 19824 KB Output is correct
56 Correct 411 ms 14752 KB Output is correct
57 Correct 614 ms 23396 KB Output is correct
58 Correct 59 ms 16244 KB Output is correct
59 Correct 51 ms 14200 KB Output is correct
60 Correct 129 ms 27768 KB Output is correct
61 Correct 133 ms 28756 KB Output is correct
62 Correct 89 ms 24120 KB Output is correct
63 Correct 79 ms 13220 KB Output is correct
64 Correct 106 ms 14836 KB Output is correct
65 Correct 133 ms 21488 KB Output is correct
66 Correct 63 ms 9628 KB Output is correct
67 Correct 95 ms 16372 KB Output is correct
68 Correct 185 ms 21108 KB Output is correct
69 Correct 40 ms 6744 KB Output is correct
70 Correct 20 ms 5376 KB Output is correct
71 Correct 119 ms 12788 KB Output is correct
72 Correct 117 ms 19188 KB Output is correct
73 Correct 206 ms 23292 KB Output is correct
74 Correct 197 ms 21236 KB Output is correct
75 Correct 195 ms 23284 KB Output is correct
76 Correct 195 ms 23592 KB Output is correct
77 Correct 198 ms 21368 KB Output is correct