Submission #1012951

# Submission time Handle Problem Language Result Execution time Memory
1012951 2024-07-03T01:20:36 Z Cookie Cats or Dogs (JOI18_catdog) C++14
100 / 100
149 ms 43260 KB
#include "catdog.h"
#include<bits/stdc++.h>
#include<fstream>
using namespace std;
//#pragma GCC optimize("Ofast,unroll-loops")
//#pragma GCC target("avx2,tune=native")
//huhu
#define sz(a) (int)a.size()
#define ALL(v) v.begin(), v.end()
#define ALLR(v) v.rbegin(), v.rend()
#define ll long long
#define pb push_back
#define forr(i, a, b) for(int i = a; i < b; i++)
#define dorr(i, a, b) for(int i = a; i >= b; i--)
#define ld long double
#define vt vector
#include<fstream>
#define fi first
#define se second
#define pll pair<ll, ll>
#define pii pair<int, int>
#define mpp make_pair
#define ull unsigned long long
const int mxn = 1e5 + 5, inf = 1e9 + 5;
struct Node{
    ll dp[2][2] = {};
    Node(){
        dp[0][1] = dp[1][0] = inf;
    }
};
Node operator +(Node a, Node b){
    Node res;
    for(int i = 0; i < 2; i++){
        for(int j = 0; j < 2; j++){
            res.dp[i][j] = inf;
        }
    }
    for(int i = 0; i < 2; i++){
        for(int j = 0; j < 2; j++){
            for(int k = 0; k < 2; k++){
                for(int l = 0; l < 2; l++){
                    res.dp[i][j] = min(res.dp[i][j], a.dp[i][k] + b.dp[l][j] + (k != l));
                }
            }
        }
    }
    return(res);
}
struct ST{
    vt<Node>st;
    int sta, en;
    void init(int l, int r){
        st.resize(4 * (r - l + 1) + 5); sta = l; en = r;
    }
    void upd(int nd, int l, int r, int id, ll dc, ll dd){
        if(l == r){
            st[nd].dp[0][0] += dc; st[nd].dp[1][1] += dd;
            return;
        }
        int mid = (l + r) >> 1;
        if(id <= mid)upd(nd << 1, l, mid, id, dc, dd); 
        else upd(nd << 1 | 1, mid + 1, r, id, dc, dd);
        st[nd] = st[nd << 1] + st[nd << 1 | 1];
    }
    void upd(int pos, ll dc, ll dd){
        assert(pos >= sta && pos <= en);
        upd(1, sta, en, pos, dc, dd);
    }
    pll get(){
        ll cat = min(st[1].dp[0][0], st[1].dp[0][1]), dog = min(st[1].dp[1][0], st[1].dp[1][1]);
        return(mpp(min(cat, dog + 1), min(dog, cat + 1)));
    }
};
ST st[mxn + 1];
int n;
int leaf[mxn + 1], head[mxn + 1], tin[mxn + 1], tt = 0, siz[mxn + 1], pa[mxn + 1];
vt<int>adj[mxn + 1];
int dp[mxn + 1][3], sign[mxn + 1];
void dfs(int s, int pre){
    siz[s] = 1; pa[s] = pre;
    for(auto i: adj[s]){
        if(i != pre){
            dfs(i, s); siz[s] += siz[i];
        }
    }
}
void hld(int s, int pre, int group){
    head[s] = group; tin[s] = ++tt; 
    leaf[group] = s;
    int mx = 0, son = -1;
    for(auto i: adj[s]){
        if(i != pre && siz[i] > mx){
            mx = siz[i]; son = i;
        }
    }
    if(son == -1)return;
    hld(son, s, group);
    for(auto i: adj[s]){
        if(i != pre && i != son){
            hld(i, s, i);
        }
    }
}
void initialize(int N, std::vector<int> A, std::vector<int> B) {
    n = N;
    for(int i = 0; i < n; i++){
        int u = A[i], v = B[i];
        adj[u].pb(v); adj[v].pb(u);
    }
    dfs(1, -1); hld(1, -1, 1);
    for(int i = 1; i <= n; i++){
        if(leaf[i]){
            st[i].init(tin[i], tin[leaf[i]]);
        }
    }
}
void upd(int node, ll dc, ll dd){
    while(node != -1){
        int par = head[node];
        //cout << par << " ";
        //cout << dc << " " << dd << " ";
        pll pre = st[par].get();
        st[par].upd(tin[node], dc, dd);
        pll nw = st[par].get();
        dc = nw.fi - pre.fi; dd = nw.se - pre.se;
        //cout << dc << " " << dd << "\n";
        node = pa[head[node]];
    }
}
int cat(int v) {
    upd(v, 0, inf);
    sign[v] = 1;
    pll res = st[1].get();
    return(min(res.fi, res.se));
}
 
int dog(int v) {
    upd(v, inf, 0);
    sign[v] = 2;
    pll res = st[1].get();
    return(min(res.fi, res.se));
}
int neighbor(int v) {
    if(sign[v] == 1){
        upd(v, 0, -inf);
    }else{
        upd(v, -inf, 0);
    }
    sign[v] = 0;
    pll res = st[1].get();
    return(min(res.fi, res.se));
}
/*
int readInt(){
	int i;
	if(scanf("%d",&i)!=1){
		fprintf(stderr,"Error while reading input\n");
		exit(1);
	}
	return i;
}

int main(){
	int N=readInt();
	
	std::vector<int> A(N-1),B(N-1);
	for(int i=0;i<N-1;i++)
	{
		A[i]=readInt();
		B[i]=readInt();
	}
	int Q;
	assert(scanf("%d",&Q)==1);
	std::vector <int> T(Q),V(Q);
	for(int i=0;i<Q;i++)
	{
		T[i]=readInt();
		V[i]=readInt();
	}
	
	initialize(N,A,B);
	
	std::vector<int> res(Q);
	for(int j=0;j<Q;j++)
	{
		if(T[j]==1) res[j]=cat(V[j]);
		else if(T[j]==2) res[j]=dog(V[j]);
		else res[j]=neighbor(V[j]);
	}
	for(int j=0;j<Q;j++)
		printf("%d\n",res[j]);
	return 0;
	
}
*/
# Verdict Execution time Memory Grader output
1 Correct 2 ms 9052 KB Output is correct
2 Correct 1 ms 9052 KB Output is correct
3 Correct 1 ms 9052 KB Output is correct
4 Correct 2 ms 9268 KB Output is correct
5 Correct 1 ms 9048 KB Output is correct
6 Correct 2 ms 9052 KB Output is correct
7 Correct 2 ms 9052 KB Output is correct
8 Correct 2 ms 9048 KB Output is correct
9 Correct 2 ms 9052 KB Output is correct
10 Correct 1 ms 9052 KB Output is correct
11 Correct 2 ms 9052 KB Output is correct
12 Correct 2 ms 9052 KB Output is correct
13 Correct 2 ms 9052 KB Output is correct
14 Correct 2 ms 9052 KB Output is correct
15 Correct 1 ms 9052 KB Output is correct
16 Correct 1 ms 9052 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 9052 KB Output is correct
2 Correct 1 ms 9052 KB Output is correct
3 Correct 1 ms 9052 KB Output is correct
4 Correct 2 ms 9268 KB Output is correct
5 Correct 1 ms 9048 KB Output is correct
6 Correct 2 ms 9052 KB Output is correct
7 Correct 2 ms 9052 KB Output is correct
8 Correct 2 ms 9048 KB Output is correct
9 Correct 2 ms 9052 KB Output is correct
10 Correct 1 ms 9052 KB Output is correct
11 Correct 2 ms 9052 KB Output is correct
12 Correct 2 ms 9052 KB Output is correct
13 Correct 2 ms 9052 KB Output is correct
14 Correct 2 ms 9052 KB Output is correct
15 Correct 1 ms 9052 KB Output is correct
16 Correct 1 ms 9052 KB Output is correct
17 Correct 2 ms 9308 KB Output is correct
18 Correct 2 ms 9392 KB Output is correct
19 Correct 2 ms 9308 KB Output is correct
20 Correct 2 ms 9308 KB Output is correct
21 Correct 2 ms 9136 KB Output is correct
22 Correct 2 ms 9276 KB Output is correct
23 Correct 2 ms 9308 KB Output is correct
24 Correct 2 ms 9308 KB Output is correct
25 Correct 2 ms 9308 KB Output is correct
26 Correct 2 ms 9308 KB Output is correct
27 Correct 1 ms 9308 KB Output is correct
28 Correct 2 ms 9308 KB Output is correct
29 Correct 2 ms 9308 KB Output is correct
30 Correct 2 ms 9308 KB Output is correct
31 Correct 2 ms 9528 KB Output is correct
32 Correct 2 ms 9304 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 9052 KB Output is correct
2 Correct 1 ms 9052 KB Output is correct
3 Correct 1 ms 9052 KB Output is correct
4 Correct 2 ms 9268 KB Output is correct
5 Correct 1 ms 9048 KB Output is correct
6 Correct 2 ms 9052 KB Output is correct
7 Correct 2 ms 9052 KB Output is correct
8 Correct 2 ms 9048 KB Output is correct
9 Correct 2 ms 9052 KB Output is correct
10 Correct 1 ms 9052 KB Output is correct
11 Correct 2 ms 9052 KB Output is correct
12 Correct 2 ms 9052 KB Output is correct
13 Correct 2 ms 9052 KB Output is correct
14 Correct 2 ms 9052 KB Output is correct
15 Correct 1 ms 9052 KB Output is correct
16 Correct 1 ms 9052 KB Output is correct
17 Correct 2 ms 9308 KB Output is correct
18 Correct 2 ms 9392 KB Output is correct
19 Correct 2 ms 9308 KB Output is correct
20 Correct 2 ms 9308 KB Output is correct
21 Correct 2 ms 9136 KB Output is correct
22 Correct 2 ms 9276 KB Output is correct
23 Correct 2 ms 9308 KB Output is correct
24 Correct 2 ms 9308 KB Output is correct
25 Correct 2 ms 9308 KB Output is correct
26 Correct 2 ms 9308 KB Output is correct
27 Correct 1 ms 9308 KB Output is correct
28 Correct 2 ms 9308 KB Output is correct
29 Correct 2 ms 9308 KB Output is correct
30 Correct 2 ms 9308 KB Output is correct
31 Correct 2 ms 9528 KB Output is correct
32 Correct 2 ms 9304 KB Output is correct
33 Correct 82 ms 23488 KB Output is correct
34 Correct 46 ms 25888 KB Output is correct
35 Correct 78 ms 19276 KB Output is correct
36 Correct 127 ms 33688 KB Output is correct
37 Correct 14 ms 16988 KB Output is correct
38 Correct 140 ms 36420 KB Output is correct
39 Correct 139 ms 36412 KB Output is correct
40 Correct 145 ms 36420 KB Output is correct
41 Correct 128 ms 36412 KB Output is correct
42 Correct 135 ms 36400 KB Output is correct
43 Correct 149 ms 36540 KB Output is correct
44 Correct 142 ms 36672 KB Output is correct
45 Correct 133 ms 36420 KB Output is correct
46 Correct 138 ms 36404 KB Output is correct
47 Correct 133 ms 36556 KB Output is correct
48 Correct 43 ms 35992 KB Output is correct
49 Correct 56 ms 43260 KB Output is correct
50 Correct 16 ms 15964 KB Output is correct
51 Correct 22 ms 22460 KB Output is correct
52 Correct 11 ms 15964 KB Output is correct
53 Correct 70 ms 34900 KB Output is correct
54 Correct 51 ms 20408 KB Output is correct
55 Correct 123 ms 28084 KB Output is correct
56 Correct 69 ms 21752 KB Output is correct
57 Correct 91 ms 33152 KB Output is correct
58 Correct 20 ms 24368 KB Output is correct
59 Correct 22 ms 21340 KB Output is correct
60 Correct 51 ms 39288 KB Output is correct
61 Correct 53 ms 40508 KB Output is correct
62 Correct 37 ms 34508 KB Output is correct
63 Correct 27 ms 21848 KB Output is correct
64 Correct 29 ms 24156 KB Output is correct
65 Correct 39 ms 33020 KB Output is correct
66 Correct 38 ms 14936 KB Output is correct
67 Correct 43 ms 25948 KB Output is correct
68 Correct 76 ms 33572 KB Output is correct
69 Correct 19 ms 11356 KB Output is correct
70 Correct 5 ms 9564 KB Output is correct
71 Correct 35 ms 20060 KB Output is correct
72 Correct 52 ms 29032 KB Output is correct
73 Correct 113 ms 36688 KB Output is correct
74 Correct 117 ms 33364 KB Output is correct
75 Correct 103 ms 36688 KB Output is correct
76 Correct 89 ms 35408 KB Output is correct
77 Correct 118 ms 33616 KB Output is correct