Submission #835208

# Submission time Handle Problem Language Result Execution time Memory
835208 2023-08-23T10:26:14 Z qwerasdfzxcl Cats or Dogs (JOI18_catdog) C++17
100 / 100
242 ms 36120 KB
#include "catdog.h"
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

constexpr int INF1 = 1e9 + 100;
constexpr ll INF2 = 4e18;

int n;
vector<int> adj[100100], G[100100];
int par[100100], sz[100100], in[100100], top[100100], leaf[100100], typ[100100], cnt;
array<ll, 2> A[100100], C[100100], D[100100];

void operator += (array<ll, 2> &L, const array<ll, 2> &R){
	L[0] += R[0], L[1] += R[1];
}

void operator -= (array<ll, 2> &L, const array<ll, 2> &R){
	L[0] -= R[0], L[1] -= R[1];
}

struct M{
	ll a[2][2];

	M(){}
	M(ll _x, ll _y, ll _z, ll _w){
		a[0][0] = _x;
		a[0][1] = _y;
		a[1][0] = _z;
		a[1][1] = _w;
	}

	array<ll, 2> calc(const array<ll, 2> &x) const{
		return {min(x[0]+a[0][0], x[1]+a[1][0]), min(x[0]+a[0][1], x[1]+a[1][1])};
	}

	M operator + (const M &F) const{
		M ret(INF2, INF2, INF2, INF2);
		for (int i=0;i<2;i++){
			for (int j=0;j<2;j++){
				for (int k=0;k<2;k++) ret.a[i][k] = min(ret.a[i][k], a[j][k] + F.a[i][j]);
			}
		}

		return ret;
	}
};

struct Seg{
	M tree[200200];
	int sz;

	void init(int n){
		sz = n;
		for (int i=sz;i<sz*2;i++) tree[i] = M(0, 1, 1, 0);
		for (int i=sz-1;i;i--) tree[i] = tree[i<<1] + tree[i<<1|1];
	}

	void update(int p, const array<ll, 2> &x){
		p += sz;
		tree[p] = M(x[0], x[1]+1, x[0]+1, x[1]);
		for (p>>=1;p;p>>=1) tree[p] = tree[p<<1] + tree[p<<1|1];
	}

	M query(int l, int r){
		// printf("query %d %d\n", l, r);
		++r;
		M retL(0, INF2, INF2, 0), retR(0, INF2, INF2, 0);
		for (l+=sz, r+=sz;l<r;l>>=1, r>>=1){
			if (l&1) retL = retL + tree[l++];
			if (r&1) retR = tree[--r] + retR;
		}

		// printf("(%lld, %lld, %lld, %lld)\n", retL.a[0][0], retL.a[0][1], retL.a[1][0], retL.a[1][1]);
		// printf("(%lld, %lld, %lld, %lld)\n", retR.a[0][0], retR.a[0][1], retR.a[1][0], retR.a[1][1]);
		// auto ret = retL + retR;
		// printf("(%lld, %lld, %lld, %lld)\n", ret.a[0][0], ret.a[0][1], ret.a[1][0], ret.a[1][1]);

		return retL + retR;
	}
}tree;

void dfs0(int s, int pa = 0){
	par[s] = pa;
	sz[s] = 1;

	for (auto &v:adj[s]) if (v!=pa){
		G[s].push_back(v);
		dfs0(v, s);
		sz[s] += sz[v];
	}
}

void dfs1(int s){
	in[s] = ++cnt;
	if (G[s].empty()){
		leaf[top[s]] = s;
		return;
	} 

	swap(G[s][0], *max_element(G[s].begin(), G[s].end(), [&](int x, int y){return sz[x] < sz[y];}));

	for (auto &v:G[s]){
		if (v==G[s][0]) top[v] = top[s];
		else top[v] = v;

		dfs1(v);
	}
}

void initialize(int N, std::vector<int> A, std::vector<int> B) {
	n = N;
	cnt = 0;
	for (int i=0;i<N-1;i++){
		adj[A[i]].push_back(B[i]);
		adj[B[i]].push_back(A[i]);
	}

	top[1] = 1;
	dfs0(1);
	dfs1(1);

	fill(::A+1, ::A+n+1, array<ll, 2>{0, 0});
	fill(C+1, C+n+1, array<ll, 2>{0, 0});
	fill(D+1, D+n+1, array<ll, 2>{0, 0});
	fill(typ+1, typ+n+1, 0);
	tree.init(n+1);

	// printf("ok\n");
}

void update(int x){
	C[x] -= A[x];
	if (typ[x]==0) A[x] = {0, 0};
	else if (typ[x]==1) A[x] = {0, INF1};
	else A[x] = {INF1, 0};
	C[x] += A[x];

	tree.update(in[x], C[x]);

	while(true){
		x = top[x];
		if (x!=1) C[par[x]] -= array<ll, 2>{min(D[x][0], D[x][1]+1), min(D[x][0]+1, D[x][1])};
		D[x] = tree.query(in[x], in[leaf[x]]).calc({0, 0});
		if (x!=1) C[par[x]] += array<ll, 2>{min(D[x][0], D[x][1]+1), min(D[x][0]+1, D[x][1])};
		else break;

		x = par[x];
		tree.update(in[x], C[x]);
	}

	// printf("ok done ans = %lld\n", min(D[1][0], D[1][1]));
	// printf("top: ");
	// for (int i=1;i<=n;i++) printf("%d ", top[i]);
	// printf("\n");

	// printf("A: ");
	// for (int i=1;i<=n;i++) printf("(%lld, %lld) ", A[i][0], A[i][1]);
	// printf("\n");

	// printf("C: ");
	// for (int i=1;i<=n;i++) printf("(%lld, %lld) ", C[i][0], C[i][1]);
	// printf("\n");

	// printf("D: ");
	// for (int i=1;i<=n;i++) printf("(%lld, %lld) ", D[i][0], D[i][1]);
	// printf("\n");
}

int cat(int v) {
	typ[v] = 1;
	update(v);
	return min(D[1][0], D[1][1]);
}

int dog(int v) {
	typ[v] = 2;
	update(v);
	return min(D[1][0], D[1][1]);
}

int neighbor(int v) {
	typ[v] = 0;
	update(v);
	return min(D[1][0], D[1][1]);
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4948 KB Output is correct
2 Correct 2 ms 5008 KB Output is correct
3 Correct 2 ms 4948 KB Output is correct
4 Correct 2 ms 4948 KB Output is correct
5 Correct 3 ms 4948 KB Output is correct
6 Correct 3 ms 5008 KB Output is correct
7 Correct 2 ms 4948 KB Output is correct
8 Correct 2 ms 4948 KB Output is correct
9 Correct 2 ms 5008 KB Output is correct
10 Correct 2 ms 5012 KB Output is correct
11 Correct 2 ms 4948 KB Output is correct
12 Correct 2 ms 4948 KB Output is correct
13 Correct 2 ms 4948 KB Output is correct
14 Correct 2 ms 5008 KB Output is correct
15 Correct 2 ms 4948 KB Output is correct
16 Correct 2 ms 5008 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4948 KB Output is correct
2 Correct 2 ms 5008 KB Output is correct
3 Correct 2 ms 4948 KB Output is correct
4 Correct 2 ms 4948 KB Output is correct
5 Correct 3 ms 4948 KB Output is correct
6 Correct 3 ms 5008 KB Output is correct
7 Correct 2 ms 4948 KB Output is correct
8 Correct 2 ms 4948 KB Output is correct
9 Correct 2 ms 5008 KB Output is correct
10 Correct 2 ms 5012 KB Output is correct
11 Correct 2 ms 4948 KB Output is correct
12 Correct 2 ms 4948 KB Output is correct
13 Correct 2 ms 4948 KB Output is correct
14 Correct 2 ms 5008 KB Output is correct
15 Correct 2 ms 4948 KB Output is correct
16 Correct 2 ms 5008 KB Output is correct
17 Correct 4 ms 5140 KB Output is correct
18 Correct 3 ms 5204 KB Output is correct
19 Correct 3 ms 5204 KB Output is correct
20 Correct 2 ms 5076 KB Output is correct
21 Correct 2 ms 5012 KB Output is correct
22 Correct 3 ms 4996 KB Output is correct
23 Correct 4 ms 5204 KB Output is correct
24 Correct 3 ms 5204 KB Output is correct
25 Correct 3 ms 5076 KB Output is correct
26 Correct 3 ms 5076 KB Output is correct
27 Correct 3 ms 5076 KB Output is correct
28 Correct 3 ms 5204 KB Output is correct
29 Correct 3 ms 5204 KB Output is correct
30 Correct 3 ms 5076 KB Output is correct
31 Correct 3 ms 5204 KB Output is correct
32 Correct 3 ms 5076 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4948 KB Output is correct
2 Correct 2 ms 5008 KB Output is correct
3 Correct 2 ms 4948 KB Output is correct
4 Correct 2 ms 4948 KB Output is correct
5 Correct 3 ms 4948 KB Output is correct
6 Correct 3 ms 5008 KB Output is correct
7 Correct 2 ms 4948 KB Output is correct
8 Correct 2 ms 4948 KB Output is correct
9 Correct 2 ms 5008 KB Output is correct
10 Correct 2 ms 5012 KB Output is correct
11 Correct 2 ms 4948 KB Output is correct
12 Correct 2 ms 4948 KB Output is correct
13 Correct 2 ms 4948 KB Output is correct
14 Correct 2 ms 5008 KB Output is correct
15 Correct 2 ms 4948 KB Output is correct
16 Correct 2 ms 5008 KB Output is correct
17 Correct 4 ms 5140 KB Output is correct
18 Correct 3 ms 5204 KB Output is correct
19 Correct 3 ms 5204 KB Output is correct
20 Correct 2 ms 5076 KB Output is correct
21 Correct 2 ms 5012 KB Output is correct
22 Correct 3 ms 4996 KB Output is correct
23 Correct 4 ms 5204 KB Output is correct
24 Correct 3 ms 5204 KB Output is correct
25 Correct 3 ms 5076 KB Output is correct
26 Correct 3 ms 5076 KB Output is correct
27 Correct 3 ms 5076 KB Output is correct
28 Correct 3 ms 5204 KB Output is correct
29 Correct 3 ms 5204 KB Output is correct
30 Correct 3 ms 5076 KB Output is correct
31 Correct 3 ms 5204 KB Output is correct
32 Correct 3 ms 5076 KB Output is correct
33 Correct 137 ms 16944 KB Output is correct
34 Correct 61 ms 18900 KB Output is correct
35 Correct 125 ms 13720 KB Output is correct
36 Correct 212 ms 25512 KB Output is correct
37 Correct 15 ms 11372 KB Output is correct
38 Correct 242 ms 27780 KB Output is correct
39 Correct 210 ms 27768 KB Output is correct
40 Correct 221 ms 27768 KB Output is correct
41 Correct 239 ms 27772 KB Output is correct
42 Correct 205 ms 27788 KB Output is correct
43 Correct 223 ms 27780 KB Output is correct
44 Correct 229 ms 27780 KB Output is correct
45 Correct 217 ms 27780 KB Output is correct
46 Correct 218 ms 27776 KB Output is correct
47 Correct 219 ms 27768 KB Output is correct
48 Correct 74 ms 20372 KB Output is correct
49 Correct 86 ms 24400 KB Output is correct
50 Correct 28 ms 9244 KB Output is correct
51 Correct 33 ms 12732 KB Output is correct
52 Correct 15 ms 9044 KB Output is correct
53 Correct 102 ms 26452 KB Output is correct
54 Correct 70 ms 14380 KB Output is correct
55 Correct 177 ms 20856 KB Output is correct
56 Correct 110 ms 15464 KB Output is correct
57 Correct 156 ms 25008 KB Output is correct
58 Correct 19 ms 13512 KB Output is correct
59 Correct 31 ms 12020 KB Output is correct
60 Correct 71 ms 22384 KB Output is correct
61 Correct 79 ms 23096 KB Output is correct
62 Correct 48 ms 19528 KB Output is correct
63 Correct 36 ms 19772 KB Output is correct
64 Correct 37 ms 21980 KB Output is correct
65 Correct 61 ms 32272 KB Output is correct
66 Correct 44 ms 11636 KB Output is correct
67 Correct 47 ms 24036 KB Output is correct
68 Correct 95 ms 32852 KB Output is correct
69 Correct 20 ms 7328 KB Output is correct
70 Correct 9 ms 5332 KB Output is correct
71 Correct 52 ms 17196 KB Output is correct
72 Correct 65 ms 27400 KB Output is correct
73 Correct 135 ms 36120 KB Output is correct
74 Correct 139 ms 32588 KB Output is correct
75 Correct 106 ms 36044 KB Output is correct
76 Correct 115 ms 34952 KB Output is correct
77 Correct 140 ms 32904 KB Output is correct