Submission #884376

# Submission time Handle Problem Language Result Execution time Memory
884376 2023-12-07T09:05:22 Z vjudge1 Cats or Dogs (JOI18_catdog) C++17
100 / 100
557 ms 25140 KB
#include "catdog.h"
#include <bits/stdc++.h>
 
using namespace std;
 
 
template<typename Node>
struct AINT {
    vector<Node> aint;
    int n;
    Node ID;
    void init(int _n, Node id) {
        ID = id;
        n = _n;
        aint.resize(n * 2 + 5, Node());
 
    }
 
    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;
        push(node, (mid - cl + 1) * 2);
        upd(p, x, node + 1, cl, mid);
        upd(p, x, node + (mid - cl + 1) * 2, mid + 1, cr);
        aint[node] = aint[node + 1] + aint[node + (mid - cl + 1) * 2];
//        cerr << cl << ' ' << cr << " :: \t" << aint[node].mat[0][0] << ' ' << aint[node].mat[1][1] << '\n'
    }
    Node query(int l, int r, int node, int cl, int cr) {
        if(r < cl || cr < l) return ID;
        if(l <= cl && cr <= r) return aint[node];
        int mid = cl + cr >> 1;
        push(node, (mid - cl + 1) * 2);
        return query(l, r, node + 1, cl, mid) + query(l, r, node + (mid - cl + 1) * 2, 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); }
 
 
    void push(int node, int L) {;}
};
 
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 = 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() { for(int i = 0; i < 2; i++) for(int j = 0; j < 2; j++) mat[i][j] = inf; }
        Mat operator +(const Mat& x) const {
            Mat rez;
 
            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;
        }
    };
 
    AINT<Mat> aint;
 
    struct SimpleDir {
        int red, blue;
        SimpleDir(int a = 0, int b = 0): red(a), blue(b) {;}
        SimpleDir operator +=(SimpleDir& x) { return *this = SimpleDir(red + min(x.red, x.blue + 1), blue + min(x.blue, x.red + 1)); }
        SimpleDir operator -=(SimpleDir& x) { return *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) { //
//        cerr << "Red: " << node << ' ' << overson[node].red << '\n';
 
        Mat curr;
        curr.mat[0][0] = overson[node].red;
        return curr;
    }
 
    Mat blue(int node) { //
//        cerr << "Blue: " << node << ' ' << overson[node].blue << '\n';
 
        Mat curr;
        curr.mat[1][1] = overson[node].blue;
        return curr;
    }
 
    Mat gol(int node) {
//        cerr << "Empty: " << node << ' ' << overson[node].red << ' ' << overson[node].blue << '\n';
 
        Mat curr;
        curr.mat[0][0] = overson[node].red;
        curr.mat[1][1] = overson[node].blue;
        return curr;
    }
 
    void init(int node, int f) {
        aint.upd(nodepoz(node), gol(node));
        for(auto x : g[node])
            if(x != f)
                init(x, node);
        return;
    }
 
    void init(int n) {
        Mat ok;
        ok.mat[0][0] = 0;
        ok.mat[1][1] = 0;
        aint.init(n, ok);
        init(0, 0);
//        cerr << "clown\n";
    }
 
    int upd(int node) {
        int father = pch[node];
        aint.upd(nodepoz(node), color[node] == 0? gol(node) : color[node] == 1? red(node) : blue(node));
 
//        cerr << pin[father] << ' ' << lastpoz[father] << '\n';
//
        if(father == 0) { auto R = aint.query(pin[father], lastpoz[father]); return min({R.mat[0][0],R.mat[0][1],R.mat[1][0],R.mat[1][1]});}
 
        overson[p[father]] -= mine[father];
        auto R = aint.query(pin[father], lastpoz[father]);
        mine[father] = SimpleDir(min(R.mat[0][0], R.mat[0][1]), min(R.mat[1][0], R.mat[1][1]));
        if(color[father] == 1) mine[father].blue = inf;
        if(color[father] == 2) mine[father].red = inf;
 
//        cerr << father << " gained " << mine[father].red << ' ' << mine[father].blue << '\n';
 
        overson[p[father]] += mine[father];
 
        return upd(p[father]);
    }
}
 
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];
    Tree::Init::init(0, 0);
 
    Tree::init(n);
 
    return;
}
 
int cat(int v) {
    Tree::color[--v] = 1;
  return Tree::upd(v);
}
 
int dog(int v) {
    Tree::color[--v] = 2;
  return Tree::upd(v);
}
 
int neighbor(int v) {
    Tree::color[--v] = 0;
  return Tree::upd(v);
}

Compilation message

catdog.cpp: In function 'void initialize(int, std::vector<int>, std::vector<int>)':
catdog.cpp:192:16: warning: statement has no effect [-Wunused-value]
  192 |     Tree::pch[0];
      |     ~~~~~~~~~~~^
catdog.cpp: In instantiation of 'void AINT<Node>::upd(int, Node, int, int, int) [with Node = Tree::Mat]':
catdog.cpp:37:34:   required from 'void AINT<Node>::upd(int, Node) [with Node = Tree::Mat]'
catdog.cpp:147:42:   required from here
catdog.cpp:22:22: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   22 |         int mid = cl + cr >> 1;
      |                   ~~~^~~~
catdog.cpp: In instantiation of 'Node AINT<Node>::query(int, int, int, int, int) [with Node = Tree::Mat]':
catdog.cpp:38:44:   required from 'Node AINT<Node>::query(int, int) [with Node = Tree::Mat]'
catdog.cpp:169:75:   required from here
catdog.cpp:32:22: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   32 |         int mid = cl + cr >> 1;
      |                   ~~~^~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 6748 KB Output is correct
2 Correct 2 ms 6916 KB Output is correct
3 Correct 1 ms 6748 KB Output is correct
4 Correct 2 ms 6748 KB Output is correct
5 Correct 2 ms 6748 KB Output is correct
6 Correct 1 ms 6748 KB Output is correct
7 Correct 1 ms 6924 KB Output is correct
8 Correct 1 ms 6748 KB Output is correct
9 Correct 1 ms 6748 KB Output is correct
10 Correct 2 ms 6748 KB Output is correct
11 Correct 2 ms 6744 KB Output is correct
12 Correct 1 ms 6744 KB Output is correct
13 Correct 2 ms 6748 KB Output is correct
14 Correct 1 ms 6748 KB Output is correct
15 Correct 2 ms 6748 KB Output is correct
16 Correct 1 ms 6748 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 6748 KB Output is correct
2 Correct 2 ms 6916 KB Output is correct
3 Correct 1 ms 6748 KB Output is correct
4 Correct 2 ms 6748 KB Output is correct
5 Correct 2 ms 6748 KB Output is correct
6 Correct 1 ms 6748 KB Output is correct
7 Correct 1 ms 6924 KB Output is correct
8 Correct 1 ms 6748 KB Output is correct
9 Correct 1 ms 6748 KB Output is correct
10 Correct 2 ms 6748 KB Output is correct
11 Correct 2 ms 6744 KB Output is correct
12 Correct 1 ms 6744 KB Output is correct
13 Correct 2 ms 6748 KB Output is correct
14 Correct 1 ms 6748 KB Output is correct
15 Correct 2 ms 6748 KB Output is correct
16 Correct 1 ms 6748 KB Output is correct
17 Correct 3 ms 7004 KB Output is correct
18 Correct 3 ms 6912 KB Output is correct
19 Correct 2 ms 6744 KB Output is correct
20 Correct 2 ms 6748 KB Output is correct
21 Correct 2 ms 6900 KB Output is correct
22 Correct 2 ms 6744 KB Output is correct
23 Correct 3 ms 7004 KB Output is correct
24 Correct 3 ms 6924 KB Output is correct
25 Correct 3 ms 6748 KB Output is correct
26 Correct 2 ms 6748 KB Output is correct
27 Correct 2 ms 6748 KB Output is correct
28 Correct 2 ms 7004 KB Output is correct
29 Correct 2 ms 7256 KB Output is correct
30 Correct 2 ms 6744 KB Output is correct
31 Correct 2 ms 6748 KB Output is correct
32 Correct 2 ms 6920 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 6748 KB Output is correct
2 Correct 2 ms 6916 KB Output is correct
3 Correct 1 ms 6748 KB Output is correct
4 Correct 2 ms 6748 KB Output is correct
5 Correct 2 ms 6748 KB Output is correct
6 Correct 1 ms 6748 KB Output is correct
7 Correct 1 ms 6924 KB Output is correct
8 Correct 1 ms 6748 KB Output is correct
9 Correct 1 ms 6748 KB Output is correct
10 Correct 2 ms 6748 KB Output is correct
11 Correct 2 ms 6744 KB Output is correct
12 Correct 1 ms 6744 KB Output is correct
13 Correct 2 ms 6748 KB Output is correct
14 Correct 1 ms 6748 KB Output is correct
15 Correct 2 ms 6748 KB Output is correct
16 Correct 1 ms 6748 KB Output is correct
17 Correct 3 ms 7004 KB Output is correct
18 Correct 3 ms 6912 KB Output is correct
19 Correct 2 ms 6744 KB Output is correct
20 Correct 2 ms 6748 KB Output is correct
21 Correct 2 ms 6900 KB Output is correct
22 Correct 2 ms 6744 KB Output is correct
23 Correct 3 ms 7004 KB Output is correct
24 Correct 3 ms 6924 KB Output is correct
25 Correct 3 ms 6748 KB Output is correct
26 Correct 2 ms 6748 KB Output is correct
27 Correct 2 ms 6748 KB Output is correct
28 Correct 2 ms 7004 KB Output is correct
29 Correct 2 ms 7256 KB Output is correct
30 Correct 2 ms 6744 KB Output is correct
31 Correct 2 ms 6748 KB Output is correct
32 Correct 2 ms 6920 KB Output is correct
33 Correct 309 ms 12892 KB Output is correct
34 Correct 110 ms 12804 KB Output is correct
35 Correct 284 ms 11596 KB Output is correct
36 Correct 486 ms 17048 KB Output is correct
37 Correct 24 ms 9564 KB Output is correct
38 Correct 557 ms 17988 KB Output is correct
39 Correct 522 ms 17728 KB Output is correct
40 Correct 511 ms 17988 KB Output is correct
41 Correct 526 ms 17812 KB Output is correct
42 Correct 474 ms 17652 KB Output is correct
43 Correct 523 ms 17800 KB Output is correct
44 Correct 500 ms 17728 KB Output is correct
45 Correct 477 ms 17732 KB Output is correct
46 Correct 498 ms 17808 KB Output is correct
47 Correct 549 ms 17800 KB Output is correct
48 Correct 165 ms 14776 KB Output is correct
49 Correct 181 ms 16628 KB Output is correct
50 Correct 62 ms 9048 KB Output is correct
51 Correct 72 ms 10708 KB Output is correct
52 Correct 30 ms 9148 KB Output is correct
53 Correct 229 ms 16268 KB Output is correct
54 Correct 166 ms 11208 KB Output is correct
55 Correct 417 ms 14920 KB Output is correct
56 Correct 259 ms 12192 KB Output is correct
57 Correct 371 ms 16016 KB Output is correct
58 Correct 38 ms 10708 KB Output is correct
59 Correct 67 ms 10432 KB Output is correct
60 Correct 161 ms 15672 KB Output is correct
61 Correct 167 ms 16072 KB Output is correct
62 Correct 104 ms 14028 KB Output is correct
63 Correct 57 ms 14452 KB Output is correct
64 Correct 70 ms 16328 KB Output is correct
65 Correct 84 ms 21484 KB Output is correct
66 Correct 67 ms 10836 KB Output is correct
67 Correct 80 ms 17240 KB Output is correct
68 Correct 157 ms 21840 KB Output is correct
69 Correct 29 ms 8480 KB Output is correct
70 Correct 7 ms 7000 KB Output is correct
71 Correct 63 ms 13904 KB Output is correct
72 Correct 99 ms 19648 KB Output is correct
73 Correct 238 ms 25140 KB Output is correct
74 Correct 256 ms 21572 KB Output is correct
75 Correct 169 ms 25068 KB Output is correct
76 Correct 182 ms 23980 KB Output is correct
77 Correct 267 ms 21844 KB Output is correct