답안 #128338

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
128338 2019-07-10T18:17:32 Z eriksuenderhauf Cats or Dogs (JOI18_catdog) C++11
100 / 100
288 ms 25124 KB
//#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#include "catdog.h"
#define mem(a,v) memset((a), (v), sizeof (a))
#define enl printf("\n")
#define case(t) printf("Case #%d: ", (t))
#define ni(n) scanf("%d", &(n))
#define nl(n) scanf("%I64d", &(n))
#define nai(a, n) for (int i = 0; i < (n); i++) ni(a[i])
#define nal(a, n) for (int i = 0; i < (n); i++) nl(a[i])
#define pri(n) printf("%d\n", (n))
#define prl(n) printf("%I64d\n", (n))
#define pii pair<int, int>
#define pil pair<int, long long>
#define pll pair<long long, long long>
#define vii vector<pii>
#define vil vector<pil>
#define vll vector<pll>
#define vi vector<int>
#define vl vector<long long>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
using namespace std;
typedef long long ll;
const double pi = acos(-1);
const int MOD = 1e9 + 7;
const int INF = 1e9 + 7;
const int MAXN = 1e5 + 5;
const double eps = 1e-9;

struct tree {
	struct node {
		int dp[2][2];
	};
	vector<node> seg;
	int sz;

	void merge(int idx) {
		seg[idx].dp[0][0] = seg[idx].dp[0][1] = INF;
		seg[idx].dp[1][0] = seg[idx].dp[1][1] = 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++)
						seg[idx].dp[i][l] = min(seg[idx].dp[i][l], seg[idx*2].dp[i][j] + (j ^ k) + seg[idx*2+1].dp[k][l]);
	}

	void upd(int idx, int l, int r) {
		idx += sz;
		seg[idx].dp[0][0] += l;
		seg[idx].dp[1][1] += r;
		for (idx /= 2; idx > 0; idx /= 2)
			merge(idx);
	}

	void qry(int &l, int &r) {
		l = min(min(seg[1].dp[0][0],seg[1].dp[0][1]), min(seg[1].dp[1][0],seg[1].dp[1][1])+1);
		r = min(min(seg[1].dp[0][0],seg[1].dp[0][1])+1, min(seg[1].dp[1][0],seg[1].dp[1][1]));
	}

	void init(int n) {
		sz = 1; while (sz < n) sz <<= 1;
		seg.resize(sz * 2);
		for (int i = sz; i < 2*sz; i++) {
			seg[i].dp[0][0] = seg[i].dp[1][1] = 0;
			seg[i].dp[1][0] = seg[i].dp[0][1] = INF;
		}
		for (int i = sz-1; i > 0; i--)
			merge(i);
	}
} seg[MAXN];

vi adj[MAXN];
int sz[MAXN], par[MAXN], cnt = 0, len[MAXN], root[MAXN], pos[MAXN];
int n, cur[MAXN], col[MAXN];

int upd(int v, int l, int r) {
	int a = 0, b = 0, c = 0, d = 0;
	while (v != -1) {
		int cur = col[v];
		seg[cur].qry(a, b);
		seg[cur].upd(pos[v], l, r);
		seg[cur].qry(c, d);
		l = c - a;
		r = d - b;
		v = root[cur];
	}
	seg[0].qry(a, b);
	return min(a, b);
}

int getSz(int u, int p) {
	sz[u] = 1;
	par[u] = p;
	for (int v: adj[u])
		if (v != p)
			sz[u] += getSz(v, u);
	return sz[u];
}

void dfs(int u, int p, int cur) {
	col[u] = cur;
	if (len[cur] == 0)
		root[cur] = p;
	pos[u] = len[cur]++;
	pii nx = mp(0, -1);
	for (int v: adj[u])
		if (v != p)
			nx = max(nx, mp(sz[v], v));
	for (int v: adj[u]) {
		if (v == p) continue;
		if (v == nx.se)
			dfs(v, u, cur);
		else
			dfs(v, u, cnt++);
	}
}

void initialize(int N, vi A, vi B) {
	n = N;
	for (int i = 0; i+1 < N; i++) {
		A[i]--, B[i]--;
		adj[A[i]].pb(B[i]);
		adj[B[i]].pb(A[i]);
	}
	getSz(0, -1);
	dfs(0, -1, cnt++);
	for (int i = 0; i < cnt; i++)
		seg[i].init(len[i]);
}

int cat(int v) {
	v--;
	int ret = upd(v, MAXN, 0);
	cur[v] = 1;
	return ret;
}

int dog(int v) {
	v--;
	int ret = upd(v, 0, MAXN);
	cur[v] = 2;
	return ret;
}

int neighbor(int v) {
	v--;
	int ret;
	if (cur[v] == 1)
		ret = upd(v, -MAXN, 0);
	else
		ret = upd(v, 0, -MAXN);
	cur[v] = 0;
	return ret;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 5752 KB Output is correct
2 Correct 7 ms 5880 KB Output is correct
3 Correct 7 ms 5880 KB Output is correct
4 Correct 7 ms 5880 KB Output is correct
5 Correct 7 ms 5752 KB Output is correct
6 Correct 8 ms 5880 KB Output is correct
7 Correct 7 ms 5880 KB Output is correct
8 Correct 7 ms 5880 KB Output is correct
9 Correct 7 ms 5752 KB Output is correct
10 Correct 6 ms 5880 KB Output is correct
11 Correct 7 ms 5880 KB Output is correct
12 Correct 7 ms 5880 KB Output is correct
13 Correct 7 ms 5756 KB Output is correct
14 Correct 7 ms 5752 KB Output is correct
15 Correct 7 ms 5752 KB Output is correct
16 Correct 7 ms 5880 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 5752 KB Output is correct
2 Correct 7 ms 5880 KB Output is correct
3 Correct 7 ms 5880 KB Output is correct
4 Correct 7 ms 5880 KB Output is correct
5 Correct 7 ms 5752 KB Output is correct
6 Correct 8 ms 5880 KB Output is correct
7 Correct 7 ms 5880 KB Output is correct
8 Correct 7 ms 5880 KB Output is correct
9 Correct 7 ms 5752 KB Output is correct
10 Correct 6 ms 5880 KB Output is correct
11 Correct 7 ms 5880 KB Output is correct
12 Correct 7 ms 5880 KB Output is correct
13 Correct 7 ms 5756 KB Output is correct
14 Correct 7 ms 5752 KB Output is correct
15 Correct 7 ms 5752 KB Output is correct
16 Correct 7 ms 5880 KB Output is correct
17 Correct 8 ms 6008 KB Output is correct
18 Correct 8 ms 5880 KB Output is correct
19 Correct 8 ms 6008 KB Output is correct
20 Correct 7 ms 5880 KB Output is correct
21 Correct 8 ms 5880 KB Output is correct
22 Correct 8 ms 5880 KB Output is correct
23 Correct 9 ms 6008 KB Output is correct
24 Correct 9 ms 6008 KB Output is correct
25 Correct 8 ms 5880 KB Output is correct
26 Correct 8 ms 5880 KB Output is correct
27 Correct 8 ms 5884 KB Output is correct
28 Correct 8 ms 6008 KB Output is correct
29 Correct 9 ms 6008 KB Output is correct
30 Correct 7 ms 5880 KB Output is correct
31 Correct 8 ms 6008 KB Output is correct
32 Correct 8 ms 5884 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 5752 KB Output is correct
2 Correct 7 ms 5880 KB Output is correct
3 Correct 7 ms 5880 KB Output is correct
4 Correct 7 ms 5880 KB Output is correct
5 Correct 7 ms 5752 KB Output is correct
6 Correct 8 ms 5880 KB Output is correct
7 Correct 7 ms 5880 KB Output is correct
8 Correct 7 ms 5880 KB Output is correct
9 Correct 7 ms 5752 KB Output is correct
10 Correct 6 ms 5880 KB Output is correct
11 Correct 7 ms 5880 KB Output is correct
12 Correct 7 ms 5880 KB Output is correct
13 Correct 7 ms 5756 KB Output is correct
14 Correct 7 ms 5752 KB Output is correct
15 Correct 7 ms 5752 KB Output is correct
16 Correct 7 ms 5880 KB Output is correct
17 Correct 8 ms 6008 KB Output is correct
18 Correct 8 ms 5880 KB Output is correct
19 Correct 8 ms 6008 KB Output is correct
20 Correct 7 ms 5880 KB Output is correct
21 Correct 8 ms 5880 KB Output is correct
22 Correct 8 ms 5880 KB Output is correct
23 Correct 9 ms 6008 KB Output is correct
24 Correct 9 ms 6008 KB Output is correct
25 Correct 8 ms 5880 KB Output is correct
26 Correct 8 ms 5880 KB Output is correct
27 Correct 8 ms 5884 KB Output is correct
28 Correct 8 ms 6008 KB Output is correct
29 Correct 9 ms 6008 KB Output is correct
30 Correct 7 ms 5880 KB Output is correct
31 Correct 8 ms 6008 KB Output is correct
32 Correct 8 ms 5884 KB Output is correct
33 Correct 139 ms 13544 KB Output is correct
34 Correct 91 ms 13944 KB Output is correct
35 Correct 120 ms 11760 KB Output is correct
36 Correct 236 ms 18748 KB Output is correct
37 Correct 32 ms 9592 KB Output is correct
38 Correct 264 ms 20176 KB Output is correct
39 Correct 243 ms 20200 KB Output is correct
40 Correct 288 ms 20200 KB Output is correct
41 Correct 282 ms 20240 KB Output is correct
42 Correct 264 ms 20112 KB Output is correct
43 Correct 257 ms 20224 KB Output is correct
44 Correct 268 ms 20196 KB Output is correct
45 Correct 270 ms 20204 KB Output is correct
46 Correct 268 ms 20196 KB Output is correct
47 Correct 271 ms 20204 KB Output is correct
48 Correct 103 ms 16704 KB Output is correct
49 Correct 120 ms 19284 KB Output is correct
50 Correct 38 ms 8904 KB Output is correct
51 Correct 53 ms 11100 KB Output is correct
52 Correct 28 ms 8568 KB Output is correct
53 Correct 171 ms 18424 KB Output is correct
54 Correct 88 ms 11640 KB Output is correct
55 Correct 203 ms 16112 KB Output is correct
56 Correct 126 ms 12576 KB Output is correct
57 Correct 190 ms 18168 KB Output is correct
58 Correct 39 ms 11380 KB Output is correct
59 Correct 45 ms 10744 KB Output is correct
60 Correct 106 ms 17836 KB Output is correct
61 Correct 113 ms 18596 KB Output is correct
62 Correct 77 ms 15728 KB Output is correct
63 Correct 64 ms 14200 KB Output is correct
64 Correct 74 ms 16092 KB Output is correct
65 Correct 115 ms 22908 KB Output is correct
66 Correct 62 ms 10364 KB Output is correct
67 Correct 95 ms 17536 KB Output is correct
68 Correct 164 ms 22392 KB Output is correct
69 Correct 32 ms 7544 KB Output is correct
70 Correct 13 ms 6136 KB Output is correct
71 Correct 69 ms 13944 KB Output is correct
72 Correct 115 ms 20728 KB Output is correct
73 Correct 215 ms 25124 KB Output is correct
74 Correct 212 ms 22264 KB Output is correct
75 Correct 175 ms 25100 KB Output is correct
76 Correct 175 ms 24976 KB Output is correct
77 Correct 212 ms 22544 KB Output is correct