Submission #674313

# Submission time Handle Problem Language Result Execution time Memory
674313 2022-12-23T16:59:23 Z jeroenodb Cats or Dogs (JOI18_catdog) C++17
100 / 100
350 ms 24388 KB
#include "catdog.h"
#include "bits/stdc++.h"
using namespace std;
#define all(x) begin(x),end(x)
template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; }
template<typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type> ostream& operator<<(ostream &os, const T_container &v) { string sep; for (const T &x : v) os << sep << x, sep = " "; return os; }
#define debug(a) cerr << "(" << #a << ": " << a << ")\n";
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> pi;
const int mxN = 1e5+1, oo = 1e9;

struct F {
    int mat[2][2] = {{0,1},{1,0}};
    friend F op(const F& u,const F& v) {
        F res = {oo,oo,oo,oo};
        for(int i : {0,1}) for(int j : {0,1}) for(int k : {0,1}) {
            res.mat[i][k] = min(res.mat[i][k], u.mat[i][j]+v.mat[j][k]);
        }
        return res;
    }
    int get() {
        int ans=oo;
        for(int i : {0,1}) for(int j : {0,1}) ans = min(ans,mat[i][j]);
        return ans;
    }
    pair<ll,ll> dp() {
        int x=oo,y=oo;
        for(int i : {0,1}) {
            x = min(x,mat[i][0]);
            y = min(y,mat[i][1]);
        }
        return {x,y};
    }
};
template<class T> struct splaytree {
    // flip
    #define L c[0]
    #define R c[1]
    struct node {
        T val;
        node *c[2] = {NULL,NULL}, *par =NULL;
        bool pathtop=true;
        node(const T& v) : val(v) {}
        node() {}
    };
    static void recalc(node* at) {
        at->val.recalc();
        if(at->L) at->val.recalc(at->L->val,0);
        if(at->R) at->val.recalc(at->R->val,1);
    }
    static void print(node* n) {
        if(n==NULL) return;
        print(n->L);
        cout << n->val << ' ';
        print(n->R);
    }
    static void rotate(node* n) {
        // Precondition: n is not the root, Postcondition: rotates n to the place of its parent
        assert(n and !n->pathtop and n->par);
        node* par = n->par;
        if(!par->pathtop) {
            auto gp = par->par;
            if(gp->L==par) gp->L=n;
            else if(gp->R==par) gp->R=n;
        }
        n->par = par->par;
        bool b= n!=par->L;
        #define l c[b]
        #define r c[b^1]
        par->l = n->r; // Fix right child of current node
        if(n->r) n->r->par = par;
        n->r = par; // Put parent under current node
        #undef l
        #undef r
        par->par = n;
        swap(par->pathtop, n->pathtop);
        recalc(par), recalc(n);
    }
    static void splay(node* n) {
        while(!n->pathtop) {
            if(n->par->pathtop) {
                rotate(n);
            } else {
                auto p = n->par, gp = p->par;
                // Double rotations
                if((p->L==n) == (gp->L==p)) rotate(p);
                else rotate(n);
                rotate(n);
            }
        }
    }
    #undef L
    #undef R
};

struct vertex{
    F mine,sub;
    vertex(){}
    void recalc(const vertex& o,bool rev=false) {
        sub  = rev?op(o.sub,sub):op(sub,o.sub);
    }
    void recalc() {
        sub=mine;
    }
}; 
array<array<ll,2>,2> matSingle[3] = {{0,1,1,0}, {0,oo,1,oo}, {oo,1,oo,0}};
struct linkcut {
    // initially the linkcut tree consists of n disconnected size 1 trees.
    typedef splaytree<vertex> bst;
    typedef bst::node node;
    int n=0;
    node* t=NULL;
    linkcut() {}
    linkcut(int nn) {
        n=nn;
        t = new node[n];
        for(int i=0;i<n;++i) {
            t[i] = node(vertex{});
        }
    }
    void attach(node* at, int sgn=1) {
        auto& f = at->par->val.mine;
        auto [x,y] = at->val.sub.dp();
        f.mat[0][1]+=sgn*min(x+1,y);
        f.mat[1][1]+=sgn*min(x+1,y);
        f.mat[0][0]+=sgn*min(x,y+1);
        f.mat[1][0]+=sgn*min(x,y+1);
    }
    void attach(int i, int sgn=1) {
        attach(t+i,sgn);
    }

    node* expose(int u) {
        node *at = NULL, *par = t+u;
        for(;par; at=par,par = par->par) {
            bst::splay(par);
            if(par->c[1]) {
                attach(par->c[1]);
                par->c[1]->pathtop = true;
                par->c[1] = NULL;
            }
            if(at) {
                attach(at,-1);
                at->pathtop = false;
            }
            par->c[1] = at;
            bst::recalc(par);     
        }
        bst::splay(t+u);
        return t+u;
    }
    void link(int u, int v) { // precondition: u is root of tree
        t[u].par = t+v; // connect with unmarked edge
        attach(u);
    }
    ll calc() {
        auto root = expose(0);
        return root->val.sub.get();
    }
    void update(int i, int old, int nw) {
        expose(i);
        for(int j=0;j<2;++j) for(int k=0;k<2;++k) {
            t[i].val.mine.mat[j][k]+=matSingle[nw][j][k]-matSingle[old][j][k];
        }
        
        bst::recalc(t+i);
    }
};
vvi adj;
linkcut tree;
void dfs(int at, int from) {
    for(int to : adj[at]) if(to!=from) {
        dfs(to,at);
        tree.link(to,at);
    }
}
int n;
vi typ;
void initialize(int N, std::vector<int> A, std::vector<int> B) {
    n=N;
    adj.resize(n);
    for(int i=0;i<n-1;++i) {
      adj[A[i]-1].push_back(B[i]-1);
      adj[B[i]-1].push_back(A[i]-1);
    }
    tree = linkcut(n);
    typ.resize(n);
    dfs(0,0);
}
int update(int v, int a) {
  tree.update(v,typ[v],a);
  typ[v]=a;
  return tree.calc();
}
int cat(int v) {
  return update(v-1,2);
}

int dog(int v) {
  return update(v-1,1);
}

int neighbor(int v) {
  return update(v-1,0);
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 1 ms 256 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 1 ms 256 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 2 ms 340 KB Output is correct
18 Correct 2 ms 340 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 0 ms 340 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 2 ms 340 KB Output is correct
24 Correct 2 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 2 ms 468 KB Output is correct
30 Correct 1 ms 212 KB Output is correct
31 Correct 1 ms 340 KB Output is correct
32 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 1 ms 256 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 2 ms 340 KB Output is correct
18 Correct 2 ms 340 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 0 ms 340 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 2 ms 340 KB Output is correct
24 Correct 2 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 2 ms 468 KB Output is correct
30 Correct 1 ms 212 KB Output is correct
31 Correct 1 ms 340 KB Output is correct
32 Correct 1 ms 340 KB Output is correct
33 Correct 156 ms 7968 KB Output is correct
34 Correct 60 ms 10220 KB Output is correct
35 Correct 162 ms 6940 KB Output is correct
36 Correct 248 ms 15392 KB Output is correct
37 Correct 13 ms 4792 KB Output is correct
38 Correct 258 ms 16888 KB Output is correct
39 Correct 279 ms 16888 KB Output is correct
40 Correct 260 ms 16892 KB Output is correct
41 Correct 256 ms 16896 KB Output is correct
42 Correct 274 ms 16928 KB Output is correct
43 Correct 260 ms 16932 KB Output is correct
44 Correct 254 ms 16936 KB Output is correct
45 Correct 350 ms 16836 KB Output is correct
46 Correct 253 ms 16948 KB Output is correct
47 Correct 288 ms 16832 KB Output is correct
48 Correct 75 ms 12196 KB Output is correct
49 Correct 70 ms 15292 KB Output is correct
50 Correct 25 ms 3536 KB Output is correct
51 Correct 26 ms 6076 KB Output is correct
52 Correct 12 ms 3284 KB Output is correct
53 Correct 112 ms 15660 KB Output is correct
54 Correct 82 ms 7052 KB Output is correct
55 Correct 235 ms 12100 KB Output is correct
56 Correct 132 ms 8000 KB Output is correct
57 Correct 132 ms 14688 KB Output is correct
58 Correct 20 ms 6716 KB Output is correct
59 Correct 26 ms 5644 KB Output is correct
60 Correct 63 ms 13700 KB Output is correct
61 Correct 65 ms 14360 KB Output is correct
62 Correct 53 ms 11552 KB Output is correct
63 Correct 33 ms 11136 KB Output is correct
64 Correct 43 ms 13100 KB Output is correct
65 Correct 59 ms 20556 KB Output is correct
66 Correct 37 ms 5340 KB Output is correct
67 Correct 51 ms 14600 KB Output is correct
68 Correct 92 ms 21040 KB Output is correct
69 Correct 24 ms 2132 KB Output is correct
70 Correct 6 ms 564 KB Output is correct
71 Correct 46 ms 9676 KB Output is correct
72 Correct 71 ms 17376 KB Output is correct
73 Correct 101 ms 24388 KB Output is correct
74 Correct 118 ms 20752 KB Output is correct
75 Correct 156 ms 24244 KB Output is correct
76 Correct 118 ms 22924 KB Output is correct
77 Correct 114 ms 21172 KB Output is correct