Submission #237856

# Submission time Handle Problem Language Result Execution time Memory
237856 2020-06-09T06:07:07 Z Mercenary Cats or Dogs (JOI18_catdog) C++14
100 / 100
232 ms 39152 KB
#include<bits/stdc++.h>
#ifndef LOCAL
#include "catdog.h"
#endif // LOCAL

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/trie_policy.hpp>

#define pb push_back
#define mp make_pair
#define taskname "A"

using namespace std;
using namespace __gnu_pbds;

typedef long long ll;
typedef long double ld;
typedef pair<int,int> ii;
typedef tree <int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;

const int maxn = 1e5 + 5;
const int mod = 1e9 + 7;
int sz[maxn];
int a[maxn];
int n , q;
vector<int> adj[maxn];
int chainhead[maxn];
int chainid[maxn];
int specid[maxn];
int par[maxn];


struct seg{
    struct node{
        int dp[2][2];
        node(){memset(dp,0,sizeof dp);};
        int getans(){
            int res = 1e5;
            for(int i = 0 ; i < 2 ; ++i)for(int j = 0 ; j < 2 ; ++j)res = min(res , dp[i][j]);
            return res;
        }
        friend node operator + (const node & a , const node & b){
            node res;
            for(int i = 0 ; i < 2 ; ++i)for(int j = 0 ; j < 2 ; ++j){
                res.dp[i][j] = 1e5;
                for(int k = 0 ; k < 2 ; ++k)for(int t = 0 ; t < 2 ; ++t){
                    res.dp[i][j] = min(res.dp[i][j] , a.dp[i][k] + b.dp[t][j] + (k ^ t));
                }
            }
            return res;
        }
    };
    vector<node> s;
    int n;
    void build(int x , int l , int r){
        if(l == r){
            s[x].dp[0][1] = s[x].dp[1][0] = 1e5;
            return;
        }
        int mid = l + r >> 1;
        build(x * 2 , l , mid);build(x * 2 + 1 , mid + 1 , r);
        s[x] = s[x * 2] + s[x * 2 + 1];
    }
    void init(){
        s.resize((n + 3) * 4);
        build(1,1,n);
    }
    void update(int x , int &pos , int &delta0 , int &delta1 , int l , int r){
        if(l == r){
            s[x].dp[0][0] += delta0;
            s[x].dp[1][1] += delta1;
            return;
        }
        int mid = l + r >> 1;
        if(pos <= mid)update(x * 2 , pos , delta0 , delta1 , l , mid);
        else update(x * 2 + 1, pos , delta0 , delta1 , mid + 1 , r);
        s[x] = s[x * 2] + s[x * 2 + 1];
    }
}s[maxn];

void update(int u , int delta0 , int delta1){
    while(u > 0){
        int curchain = chainid[u];
        auto pre = s[curchain].s[1];
        s[curchain].update(1,specid[u],delta0,delta1,1,s[curchain].n);
        auto now = s[curchain].s[1];
        delta0 = min({now.dp[0][0],now.dp[0][1],now.dp[1][0]+1,now.dp[1][1]+1})
            - min({pre.dp[0][0],pre.dp[0][1],pre.dp[1][0]+1,pre.dp[1][1]+1});
        delta1 = min({now.dp[0][0] + 1 ,now.dp[0][1] + 1 ,now.dp[1][0],now.dp[1][1]})
            - min({pre.dp[0][0] + 1 ,pre.dp[0][1] + 1 ,pre.dp[1][0],pre.dp[1][1]});
        u = par[chainhead[curchain]];
    }
}
void dfs(int u , int par){
    sz[u] = 1;
    for(auto c : adj[u]){
        if(c == par)continue;
        ::par[c] = u;
        dfs(c , u);
        sz[u] += sz[c];
    }
}

int nTime = 0;

void hld(int u , int par){
    ii best = mp(0,0);
    chainid[u] = nTime;
    specid[u] = ++s[nTime].n;
    if(chainhead[nTime] == 0)chainhead[nTime] = u;
    for(auto &c : adj[u]){
        if(c == par)continue;
        best = max(best,mp(sz[c],c));
    }
    if(best.second != 0)hld(best.second,u);
    for(auto &c : adj[u]){
        if(c == par || c == best.second)continue;
        ++nTime;
        hld(c , u);
    }
}

void initialize(int N, vector<int> A, vector<int> B) {
    n = N;
    for(int i = 0 ; i < n - 1 ; ++i){
        adj[A[i]].pb(B[i]);
        adj[B[i]].pb(A[i]);
    }
    for(int i = 1 ; i <= n ; ++i)a[i] = 3;
    dfs(1,0);
    hld(1,0);
    for(int i = 0 ; i <= nTime ; ++i){
        s[i].init();
    }
}

int cat(int v) {
    a[v] = 1;
    update(v,1e5,0);
    return s[0].s[1].getans();
}

int dog(int v) {
    a[v] = 2;
    update(v,0,1e5);
    return s[0].s[1].getans();
}

int neighbor(int v) {
    if(a[v] == 1)update(v,-1e5,0);
    else update(v,0,-1e5);
    return s[0].s[1].getans();
}

Compilation message

catdog.cpp: In member function 'void seg::build(int, int, int)':
catdog.cpp:60:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
         int mid = l + r >> 1;
                   ~~^~~
catdog.cpp: In member function 'void seg::update(int, int&, int&, int&, int, int)':
catdog.cpp:74:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
         int mid = l + r >> 1;
                   ~~^~~
# Verdict Execution time Memory Grader output
1 Correct 7 ms 5760 KB Output is correct
2 Correct 8 ms 5760 KB Output is correct
3 Correct 8 ms 5760 KB Output is correct
4 Correct 8 ms 5760 KB Output is correct
5 Correct 9 ms 5888 KB Output is correct
6 Correct 8 ms 5888 KB Output is correct
7 Correct 8 ms 5864 KB Output is correct
8 Correct 8 ms 5888 KB Output is correct
9 Correct 8 ms 5760 KB Output is correct
10 Correct 7 ms 5888 KB Output is correct
11 Correct 8 ms 5888 KB Output is correct
12 Correct 8 ms 5760 KB Output is correct
13 Correct 8 ms 5760 KB Output is correct
14 Correct 7 ms 5760 KB Output is correct
15 Correct 7 ms 5888 KB Output is correct
16 Correct 8 ms 5760 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 5760 KB Output is correct
2 Correct 8 ms 5760 KB Output is correct
3 Correct 8 ms 5760 KB Output is correct
4 Correct 8 ms 5760 KB Output is correct
5 Correct 9 ms 5888 KB Output is correct
6 Correct 8 ms 5888 KB Output is correct
7 Correct 8 ms 5864 KB Output is correct
8 Correct 8 ms 5888 KB Output is correct
9 Correct 8 ms 5760 KB Output is correct
10 Correct 7 ms 5888 KB Output is correct
11 Correct 8 ms 5888 KB Output is correct
12 Correct 8 ms 5760 KB Output is correct
13 Correct 8 ms 5760 KB Output is correct
14 Correct 7 ms 5760 KB Output is correct
15 Correct 7 ms 5888 KB Output is correct
16 Correct 8 ms 5760 KB Output is correct
17 Correct 8 ms 6016 KB Output is correct
18 Correct 9 ms 6016 KB Output is correct
19 Correct 8 ms 6016 KB Output is correct
20 Correct 8 ms 5888 KB Output is correct
21 Correct 8 ms 5888 KB Output is correct
22 Correct 8 ms 5888 KB Output is correct
23 Correct 8 ms 6016 KB Output is correct
24 Correct 9 ms 6016 KB Output is correct
25 Correct 8 ms 5888 KB Output is correct
26 Correct 8 ms 5888 KB Output is correct
27 Correct 8 ms 5888 KB Output is correct
28 Correct 9 ms 6016 KB Output is correct
29 Correct 8 ms 6016 KB Output is correct
30 Correct 9 ms 6016 KB Output is correct
31 Correct 8 ms 6144 KB Output is correct
32 Correct 8 ms 5888 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 5760 KB Output is correct
2 Correct 8 ms 5760 KB Output is correct
3 Correct 8 ms 5760 KB Output is correct
4 Correct 8 ms 5760 KB Output is correct
5 Correct 9 ms 5888 KB Output is correct
6 Correct 8 ms 5888 KB Output is correct
7 Correct 8 ms 5864 KB Output is correct
8 Correct 8 ms 5888 KB Output is correct
9 Correct 8 ms 5760 KB Output is correct
10 Correct 7 ms 5888 KB Output is correct
11 Correct 8 ms 5888 KB Output is correct
12 Correct 8 ms 5760 KB Output is correct
13 Correct 8 ms 5760 KB Output is correct
14 Correct 7 ms 5760 KB Output is correct
15 Correct 7 ms 5888 KB Output is correct
16 Correct 8 ms 5760 KB Output is correct
17 Correct 8 ms 6016 KB Output is correct
18 Correct 9 ms 6016 KB Output is correct
19 Correct 8 ms 6016 KB Output is correct
20 Correct 8 ms 5888 KB Output is correct
21 Correct 8 ms 5888 KB Output is correct
22 Correct 8 ms 5888 KB Output is correct
23 Correct 8 ms 6016 KB Output is correct
24 Correct 9 ms 6016 KB Output is correct
25 Correct 8 ms 5888 KB Output is correct
26 Correct 8 ms 5888 KB Output is correct
27 Correct 8 ms 5888 KB Output is correct
28 Correct 9 ms 6016 KB Output is correct
29 Correct 8 ms 6016 KB Output is correct
30 Correct 9 ms 6016 KB Output is correct
31 Correct 8 ms 6144 KB Output is correct
32 Correct 8 ms 5888 KB Output is correct
33 Correct 129 ms 18696 KB Output is correct
34 Correct 80 ms 20728 KB Output is correct
35 Correct 122 ms 14960 KB Output is correct
36 Correct 198 ms 27620 KB Output is correct
37 Correct 31 ms 12672 KB Output is correct
38 Correct 217 ms 30056 KB Output is correct
39 Correct 227 ms 30188 KB Output is correct
40 Correct 232 ms 30052 KB Output is correct
41 Correct 225 ms 30056 KB Output is correct
42 Correct 214 ms 30052 KB Output is correct
43 Correct 226 ms 30184 KB Output is correct
44 Correct 222 ms 30056 KB Output is correct
45 Correct 220 ms 30180 KB Output is correct
46 Correct 212 ms 30184 KB Output is correct
47 Correct 219 ms 30056 KB Output is correct
48 Correct 94 ms 32064 KB Output is correct
49 Correct 116 ms 39152 KB Output is correct
50 Correct 34 ms 12536 KB Output is correct
51 Correct 49 ms 18780 KB Output is correct
52 Correct 27 ms 12544 KB Output is correct
53 Correct 131 ms 28792 KB Output is correct
54 Correct 75 ms 15788 KB Output is correct
55 Correct 169 ms 22768 KB Output is correct
56 Correct 105 ms 17056 KB Output is correct
57 Correct 155 ms 27128 KB Output is correct
58 Correct 39 ms 20596 KB Output is correct
59 Correct 44 ms 17528 KB Output is correct
60 Correct 100 ms 35312 KB Output is correct
61 Correct 106 ms 36464 KB Output is correct
62 Correct 73 ms 30704 KB Output is correct
63 Correct 54 ms 16248 KB Output is correct
64 Correct 58 ms 18168 KB Output is correct
65 Correct 81 ms 25464 KB Output is correct
66 Correct 58 ms 10872 KB Output is correct
67 Correct 69 ms 19704 KB Output is correct
68 Correct 128 ms 25848 KB Output is correct
69 Correct 31 ms 7672 KB Output is correct
70 Correct 13 ms 6144 KB Output is correct
71 Correct 64 ms 14972 KB Output is correct
72 Correct 88 ms 22392 KB Output is correct
73 Correct 162 ms 29176 KB Output is correct
74 Correct 171 ms 25596 KB Output is correct
75 Correct 147 ms 29176 KB Output is correct
76 Correct 148 ms 27896 KB Output is correct
77 Correct 176 ms 25976 KB Output is correct