답안 #884139

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
884139 2023-12-06T16:05:35 Z vjudge1 Cats or Dogs (JOI18_catdog) C++17
38 / 100
3000 ms 19092 KB
#include "catdog.h"
#include <iostream>
#include <algorithm>
#include <vector>
#pragma GCC optimize("Ofast,unroll-loops")
//
using namespace std;

const int nmax = 1e5 + 5;
const int inf = 1e6 + 5;

namespace Tree {
    vector<int> g[nmax];

    int area[nmax];
    int pch[nmax], lastpoz[nmax], p[nmax];
    int pin[nmax], pout[nmax], inp = 1;

    int nodepoz(int node) { return pin[node];}

    namespace Init {
        void preinit(int node, int f) {
            p[node] = f;
            area[node] = 1;
            for(auto x : g[node]) {
                if(x == f) continue;
                preinit(x, node);
                area[node] += area[x];
            }
            return;
        }

        void init(int node, int f) {
            lastpoz[pch[node]] = inp;
            pin[node] = inp++;
            int mx = -1;
            for(auto x : g[node]) {
                if(x == f) continue;
                mx = mx == -1 || area[mx] < area[x]? x : mx;
            }
            if(mx == -1) {pout[node] = inp - 1; return; }
            pch[mx] = pch[node];
            init(mx, node);

            for(auto x : g[node]) {
               if(x == f || x == mx) continue;
               pch[x] = x;
               init(x, node);
            }
        }
    }

    struct Mat {
        int mat[2][2];
        Mat() { mat[0][0] = mat[1][1] = 0; mat[0][1] = mat[1][0] = inf; }
        Mat(int a, int b) { mat[0][0] = a; mat[1][1] = b; mat[0][1] = mat[1][0] = inf; }
        Mat operator +(const Mat& x) const {
            if(mat[0][0] == 0 && mat[1][1] == 0 && mat[0][1] == inf && mat[1][0] == inf) return x;
            if(x.mat[0][0] == 0 && x.mat[1][1] == 0 && x.mat[0][1] == inf && x.mat[1][0] == inf) return *this;
//            if(x == ok) return *this;
            Mat rez;
            rez.mat[0][0] = rez.mat[1][1] = inf;

            for(int L = 0; L < 2; L++) {
                for(int M1 = 0; M1 < 2; M1++) {
                    for(int M2 = 0; M2 < 2; M2++) {
                        for(int R = 0; R < 2; R++) {
                            rez.mat[L][R] = min(rez.mat[L][R], mat[L][M1] + x.mat[M2][R] + (M1 ^ M2));
                        }
                    }
                }
            }

            return rez;
        }
    };

    #define Node Mat
    namespace aint {
        Node aint[nmax * 4];
        int n;
        void init(int _n) {
            n = _n;
//            aint.resize(n * 2 + 2);

        }

        void push(int node, int L) {;}
        void upd(int p, Node x, int node, int cl, int cr) {
            if(p < cl || cr < p) return;
            if(cl == cr) { aint[node] = x; return; }
            int mid = cl + cr >> 1, L = (mid - cl + 1) * 2;
            push(node, (mid - cl + 1) * 2);
            upd(p, x, node + 1, cl, mid);
            upd(p, x, node + L, mid + 1, cr);
            aint[node] = aint[node + 1] + aint[node + L];
        }
        Node query(int l, int r, int node, int cl, int cr) {
            if(r < cl || cr < l) return Node();
            if(l <= cl && cr <= r) return aint[node];
            int mid = cl + cr >> 1, L = (mid - cl + 1) * 2;
            push(node, (mid - cl + 1) * 2);
            return query(l, r, node + 1, cl, mid) + query(l, r, node + L, mid + 1, cr);
        }

        void upd(int p, Node x) { upd(p, x, 1, 1, n); }
        Node query(int l, int r) { return query(l, r, 1, 1, n); }


    };
    #undef Node



    struct SimpleDir {
        int red, blue;
        SimpleDir(int a = 0, int b = 0): red(a), blue(b) {;}
        void operator +=(const SimpleDir& x) {  *this = SimpleDir(red + min(x.red, x.blue + 1), blue + min(x.blue, x.red + 1)); }
        void operator -=(const SimpleDir& x) {  *this = SimpleDir(red - min(x.red, x.blue + 1), blue - min(x.blue, x.red + 1)); }
    };

    SimpleDir overson[nmax], mine[nmax];

    int color[nmax];

    Mat red(int node) {
        Mat curr(overson[node].red, inf);
        return curr;
    }

    Mat blue(int node) {
        Mat curr(inf, overson[node].blue);
        return curr;
    }

    Mat gol(int node) {
        Mat curr(overson[node].red, overson[node].blue);
        return curr;
    }


    void init(int n) {
        aint::init(n);
//        init(0, 0);
    }

    int upd(int node) {
        int P = pin[node], K = pin[pch[node]];
        int father = pch[node];
        if(color[P] == 0)
            aint::upd(P, gol(P));
        else if(color[P] == 1)
            aint::upd(P, red(P));
        else
            aint::upd(P, blue(P));

        if(father == 0) { auto R = aint::query(K, lastpoz[father]); return min({R.mat[0][0],R.mat[0][1],R.mat[1][0],R.mat[1][1]});}

        overson[pin[p[father]]] -= mine[K];
        auto R = aint::query(K, lastpoz[father]);
        mine[K] = SimpleDir(min(R.mat[0][0], R.mat[0][1]), min(R.mat[1][0], R.mat[1][1]));
        if(color[K] == 1) mine[K].blue = inf;
        else if(color[K] == 2) mine[K].red = inf;

        overson[pin[p[father]]] += mine[K];

        return upd(p[node]);
    }
}

void initialize(int n, std::vector<int> A, std::vector<int> B) {
    for(int i = 0; i < n - 1; i++) {
        Tree::g[--A[i]].emplace_back(--B[i]);
        Tree::g[B[i]].emplace_back(A[i]);
    }

    Tree::Init::preinit(0, 0);
    Tree::pch[0] = 0;
    Tree::Init::init(0, 0);

    Tree::init(n);

    return;
}

int cat(int v) {
    Tree::color[Tree::pin[--v]] = 1;
  return Tree::upd(v);
}

int dog(int v) {
    Tree::color[Tree::pin[--v]] = 2;
  return Tree::upd(v);
}

int neighbor(int v) {
    Tree::color[Tree::pin[--v]] = 0;
  return Tree::upd(v);
}

Compilation message

catdog.cpp: In function 'void Tree::aint::upd(int, Tree::Mat, int, int, int)':
catdog.cpp:92:26: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   92 |             int mid = cl + cr >> 1, L = (mid - cl + 1) * 2;
      |                       ~~~^~~~
catdog.cpp: In function 'Tree::Mat Tree::aint::query(int, int, int, int, int)':
catdog.cpp:101:26: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  101 |             int mid = cl + cr >> 1, L = (mid - cl + 1) * 2;
      |                       ~~~^~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 13144 KB Output is correct
2 Correct 3 ms 13148 KB Output is correct
3 Correct 3 ms 13148 KB Output is correct
4 Correct 4 ms 13148 KB Output is correct
5 Correct 3 ms 13148 KB Output is correct
6 Correct 3 ms 13148 KB Output is correct
7 Correct 3 ms 13148 KB Output is correct
8 Correct 3 ms 13148 KB Output is correct
9 Correct 3 ms 13412 KB Output is correct
10 Correct 3 ms 13148 KB Output is correct
11 Correct 4 ms 13148 KB Output is correct
12 Correct 3 ms 13148 KB Output is correct
13 Correct 3 ms 13148 KB Output is correct
14 Correct 3 ms 13148 KB Output is correct
15 Correct 3 ms 13148 KB Output is correct
16 Correct 3 ms 13148 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 13144 KB Output is correct
2 Correct 3 ms 13148 KB Output is correct
3 Correct 3 ms 13148 KB Output is correct
4 Correct 4 ms 13148 KB Output is correct
5 Correct 3 ms 13148 KB Output is correct
6 Correct 3 ms 13148 KB Output is correct
7 Correct 3 ms 13148 KB Output is correct
8 Correct 3 ms 13148 KB Output is correct
9 Correct 3 ms 13412 KB Output is correct
10 Correct 3 ms 13148 KB Output is correct
11 Correct 4 ms 13148 KB Output is correct
12 Correct 3 ms 13148 KB Output is correct
13 Correct 3 ms 13148 KB Output is correct
14 Correct 3 ms 13148 KB Output is correct
15 Correct 3 ms 13148 KB Output is correct
16 Correct 3 ms 13148 KB Output is correct
17 Correct 5 ms 13148 KB Output is correct
18 Correct 5 ms 13148 KB Output is correct
19 Correct 4 ms 13232 KB Output is correct
20 Correct 3 ms 13148 KB Output is correct
21 Correct 3 ms 13148 KB Output is correct
22 Correct 3 ms 13148 KB Output is correct
23 Correct 5 ms 13148 KB Output is correct
24 Correct 5 ms 13148 KB Output is correct
25 Correct 4 ms 13148 KB Output is correct
26 Correct 3 ms 13148 KB Output is correct
27 Correct 5 ms 13204 KB Output is correct
28 Correct 5 ms 13148 KB Output is correct
29 Correct 4 ms 13148 KB Output is correct
30 Correct 3 ms 13168 KB Output is correct
31 Correct 3 ms 13148 KB Output is correct
32 Correct 3 ms 13148 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 13144 KB Output is correct
2 Correct 3 ms 13148 KB Output is correct
3 Correct 3 ms 13148 KB Output is correct
4 Correct 4 ms 13148 KB Output is correct
5 Correct 3 ms 13148 KB Output is correct
6 Correct 3 ms 13148 KB Output is correct
7 Correct 3 ms 13148 KB Output is correct
8 Correct 3 ms 13148 KB Output is correct
9 Correct 3 ms 13412 KB Output is correct
10 Correct 3 ms 13148 KB Output is correct
11 Correct 4 ms 13148 KB Output is correct
12 Correct 3 ms 13148 KB Output is correct
13 Correct 3 ms 13148 KB Output is correct
14 Correct 3 ms 13148 KB Output is correct
15 Correct 3 ms 13148 KB Output is correct
16 Correct 3 ms 13148 KB Output is correct
17 Correct 5 ms 13148 KB Output is correct
18 Correct 5 ms 13148 KB Output is correct
19 Correct 4 ms 13232 KB Output is correct
20 Correct 3 ms 13148 KB Output is correct
21 Correct 3 ms 13148 KB Output is correct
22 Correct 3 ms 13148 KB Output is correct
23 Correct 5 ms 13148 KB Output is correct
24 Correct 5 ms 13148 KB Output is correct
25 Correct 4 ms 13148 KB Output is correct
26 Correct 3 ms 13148 KB Output is correct
27 Correct 5 ms 13204 KB Output is correct
28 Correct 5 ms 13148 KB Output is correct
29 Correct 4 ms 13148 KB Output is correct
30 Correct 3 ms 13168 KB Output is correct
31 Correct 3 ms 13148 KB Output is correct
32 Correct 3 ms 13148 KB Output is correct
33 Correct 1328 ms 16456 KB Output is correct
34 Correct 320 ms 16220 KB Output is correct
35 Correct 791 ms 15948 KB Output is correct
36 Correct 2265 ms 18544 KB Output is correct
37 Correct 14 ms 14684 KB Output is correct
38 Correct 2597 ms 19080 KB Output is correct
39 Correct 2507 ms 19008 KB Output is correct
40 Correct 2711 ms 19016 KB Output is correct
41 Correct 2850 ms 19008 KB Output is correct
42 Correct 2107 ms 19008 KB Output is correct
43 Correct 1815 ms 19004 KB Output is correct
44 Correct 1619 ms 19020 KB Output is correct
45 Correct 2019 ms 19092 KB Output is correct
46 Correct 2041 ms 19028 KB Output is correct
47 Correct 2384 ms 19000 KB Output is correct
48 Correct 100 ms 17464 KB Output is correct
49 Correct 117 ms 18444 KB Output is correct
50 Correct 46 ms 14424 KB Output is correct
51 Correct 58 ms 15196 KB Output is correct
52 Correct 18 ms 14172 KB Output is correct
53 Correct 992 ms 18000 KB Output is correct
54 Correct 680 ms 15452 KB Output is correct
55 Correct 1671 ms 17412 KB Output is correct
56 Correct 1159 ms 16104 KB Output is correct
57 Correct 1171 ms 18004 KB Output is correct
58 Correct 18 ms 15316 KB Output is correct
59 Correct 38 ms 15192 KB Output is correct
60 Correct 81 ms 17876 KB Output is correct
61 Correct 100 ms 17980 KB Output is correct
62 Correct 53 ms 16852 KB Output is correct
63 Execution timed out 3037 ms 18264 KB Time limit exceeded
64 Halted 0 ms 0 KB -