Submission #97804

# Submission time Handle Problem Language Result Execution time Memory
97804 2019-02-18T14:47:18 Z fefe Cats or Dogs (JOI18_catdog) C++17
100 / 100
2721 ms 29824 KB
#include "catdog.h"
#include<bits/stdc++.h>
#define MAX_N 200005
#define MAX_M 100005
#define pb push_back
#define mp make_pair
#define all(V) (V).begin(),(V).end()
#define reset(V) (V).clear();(V).resize(0);
#define sq(x) ((x)*(x))
#define abs(x) ((x)>0?(x):(-(x)))
#define fi first
#define se second
#define LL_inf (1LL<<60)
#define full_inf 0x7fffffff
#define half_inf 0x3fffffff
#define inf 1000005
#define MOD 1000000007LL
#define cpx_mod(x) (((LD)(((LL)x.real())%MOD)),((LD)(((LL)x.imag())%MOD)))
using namespace std;
typedef long long LL;
typedef long double LD;
typedef pair<int,int> pii;
typedef pair<LL,LL> pil;
typedef pair<LL,string> pls;
typedef complex<LD> Complex;
typedef long double LD;
int x;
struct node{
	int cost[4];
}tree[4*MAX_N];
int n,m;
int num[MAX_N],sta[MAX_N],cnt[MAX_N],pa[MAX_N],C[MAX_N],D[MAX_N];
bool vis[MAX_N];
vector<int> E[MAX_N],lst[MAX_N];
void make_HLD(int x){
	cnt[x]=1;
	if(x!=1 && E[x].size()==1){
		E[x][0]=0;
		lst[m].pb(x);
		num[x]=m++;
		return;
	}
	int mi=0;
	for(int &y:E[x]){
		if(cnt[y]){y=0;continue;}
		make_HLD(y);pa[y]=x;
		cnt[x]+=cnt[y];
		if(cnt[y]>cnt[mi])	mi=y;
	}
	num[x]=num[mi];
	lst[num[x]].pb(x);
}
void init1(int x){for(int i=0;i<4;i++)	tree[x].cost[i]=(i%3?1:0)*inf;}
void initialize(int N, std::vector<int> A, std::vector<int> B) {
	cnt[0]=-1;
	n=N;
	for(int i=0;i<n-1;i++){
		E[A[i]].pb(B[i]);
		E[B[i]].pb(A[i]);
	}
	make_HLD(1);
	int x=n;
	for(int i=0;i<m;i++){
		for(int j:lst[i]){cnt[j]=x--;}
	}
	for(int i=1;i<=4*n;i++)	init1(i);
}
node merge(node x,node y){
	node p;
	for(int i=0;i<4;i++)	p.cost[i]=inf;
	for(int i=0;i<4;i++){
		for(int j=0;j<4;j++)	p.cost[(i/2)*2+j%2]=min(p.cost[(i/2)*2+j%2],x.cost[i]+y.cost[j]+((i%2)^(j/2)));
	}
	return p;
}
void udt_tree(int x,int l,int r,int p,node v){
	if(p<l || r<p)	return;
	if(l==r){
		tree[x]=v;
		return;
	}
	int mid=(l+r)>>1;
	udt_tree(2*x,l,mid,p,v);
	udt_tree(2*x+1,mid+1,r,p,v);
	tree[x]=merge(tree[2*x],tree[2*x+1]);
}
node get_cost(int x,int l,int r,int s,int e){
	if(e<l || s>r)	return {0,inf,inf,0};
	if(s<=l && r<=e)	return tree[x];
	int mid=(l+r)>>1;
	return merge(get_cost(2*x,l,mid,s,e),get_cost(2*x+1,mid+1,r,s,e));
}
void update(int x,int cat,int dog){
	if(!x)	return;
	int p=lst[num[x]].back();
	node q=get_cost(1,1,n,cnt[lst[num[x]].back()],cnt[lst[num[x]][0]]);
	C[pa[p]]-=min(min(q.cost[0],q.cost[1]),min(q.cost[2],q.cost[3])+1);
	D[pa[p]]-=min(min(q.cost[2],q.cost[3]),min(q.cost[0],q.cost[1])+1);
	udt_tree(1,1,n,cnt[x],{cat,inf,inf,dog});
	q=get_cost(1,1,n,cnt[lst[num[x]].back()],cnt[lst[num[x]][0]]);
	C[pa[p]]+=min(min(q.cost[0],q.cost[1]),min(q.cost[2],q.cost[3])+1);
	D[pa[p]]+=min(min(q.cost[2],q.cost[3]),min(q.cost[0],q.cost[1])+1);
	cat=(sta[pa[p]]==2)?inf:C[pa[p]];
	dog=(sta[pa[p]]==1)?inf:D[pa[p]];
	update(pa[p],cat,dog);
	return;
}
int get_min(){
	node p=get_cost(1,1,n,cnt[1],cnt[lst[num[1]][0]]);
	return min({p.cost[0],p.cost[1],p.cost[2],p.cost[3]});
}
int cat(int v){
	sta[v]=1;
	update(v,C[v],inf);
	return get_min();
}
int dog(int v){
	sta[v]=2;
	update(v,inf,D[v]);
	return get_min();
}
int neighbor(int v){
	sta[v]=0;
	update(v,C[v],D[v]);
	return get_min();
}
# Verdict Execution time Memory Grader output
1 Correct 13 ms 9728 KB Output is correct
2 Correct 12 ms 9728 KB Output is correct
3 Correct 11 ms 9728 KB Output is correct
4 Correct 12 ms 9728 KB Output is correct
5 Correct 12 ms 9856 KB Output is correct
6 Correct 10 ms 9728 KB Output is correct
7 Correct 13 ms 9828 KB Output is correct
8 Correct 13 ms 9728 KB Output is correct
9 Correct 10 ms 9728 KB Output is correct
10 Correct 13 ms 9856 KB Output is correct
11 Correct 11 ms 9728 KB Output is correct
12 Correct 10 ms 9728 KB Output is correct
13 Correct 13 ms 9728 KB Output is correct
14 Correct 12 ms 9728 KB Output is correct
15 Correct 11 ms 9728 KB Output is correct
16 Correct 11 ms 9728 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 9728 KB Output is correct
2 Correct 12 ms 9728 KB Output is correct
3 Correct 11 ms 9728 KB Output is correct
4 Correct 12 ms 9728 KB Output is correct
5 Correct 12 ms 9856 KB Output is correct
6 Correct 10 ms 9728 KB Output is correct
7 Correct 13 ms 9828 KB Output is correct
8 Correct 13 ms 9728 KB Output is correct
9 Correct 10 ms 9728 KB Output is correct
10 Correct 13 ms 9856 KB Output is correct
11 Correct 11 ms 9728 KB Output is correct
12 Correct 10 ms 9728 KB Output is correct
13 Correct 13 ms 9728 KB Output is correct
14 Correct 12 ms 9728 KB Output is correct
15 Correct 11 ms 9728 KB Output is correct
16 Correct 11 ms 9728 KB Output is correct
17 Correct 19 ms 9856 KB Output is correct
18 Correct 17 ms 9856 KB Output is correct
19 Correct 14 ms 9856 KB Output is correct
20 Correct 11 ms 9728 KB Output is correct
21 Correct 12 ms 9728 KB Output is correct
22 Correct 14 ms 9856 KB Output is correct
23 Correct 19 ms 9888 KB Output is correct
24 Correct 23 ms 9848 KB Output is correct
25 Correct 22 ms 9856 KB Output is correct
26 Correct 12 ms 9856 KB Output is correct
27 Correct 12 ms 9728 KB Output is correct
28 Correct 12 ms 9856 KB Output is correct
29 Correct 13 ms 9984 KB Output is correct
30 Correct 12 ms 9856 KB Output is correct
31 Correct 12 ms 9856 KB Output is correct
32 Correct 14 ms 9728 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 9728 KB Output is correct
2 Correct 12 ms 9728 KB Output is correct
3 Correct 11 ms 9728 KB Output is correct
4 Correct 12 ms 9728 KB Output is correct
5 Correct 12 ms 9856 KB Output is correct
6 Correct 10 ms 9728 KB Output is correct
7 Correct 13 ms 9828 KB Output is correct
8 Correct 13 ms 9728 KB Output is correct
9 Correct 10 ms 9728 KB Output is correct
10 Correct 13 ms 9856 KB Output is correct
11 Correct 11 ms 9728 KB Output is correct
12 Correct 10 ms 9728 KB Output is correct
13 Correct 13 ms 9728 KB Output is correct
14 Correct 12 ms 9728 KB Output is correct
15 Correct 11 ms 9728 KB Output is correct
16 Correct 11 ms 9728 KB Output is correct
17 Correct 19 ms 9856 KB Output is correct
18 Correct 17 ms 9856 KB Output is correct
19 Correct 14 ms 9856 KB Output is correct
20 Correct 11 ms 9728 KB Output is correct
21 Correct 12 ms 9728 KB Output is correct
22 Correct 14 ms 9856 KB Output is correct
23 Correct 19 ms 9888 KB Output is correct
24 Correct 23 ms 9848 KB Output is correct
25 Correct 22 ms 9856 KB Output is correct
26 Correct 12 ms 9856 KB Output is correct
27 Correct 12 ms 9728 KB Output is correct
28 Correct 12 ms 9856 KB Output is correct
29 Correct 13 ms 9984 KB Output is correct
30 Correct 12 ms 9856 KB Output is correct
31 Correct 12 ms 9856 KB Output is correct
32 Correct 14 ms 9728 KB Output is correct
33 Correct 1492 ms 18116 KB Output is correct
34 Correct 447 ms 19188 KB Output is correct
35 Correct 1621 ms 15856 KB Output is correct
36 Correct 2721 ms 24052 KB Output is correct
37 Correct 46 ms 14328 KB Output is correct
38 Correct 2540 ms 25512 KB Output is correct
39 Correct 2615 ms 25520 KB Output is correct
40 Correct 2667 ms 25544 KB Output is correct
41 Correct 2596 ms 25556 KB Output is correct
42 Correct 2678 ms 27616 KB Output is correct
43 Correct 2459 ms 27488 KB Output is correct
44 Correct 2519 ms 27460 KB Output is correct
45 Correct 2608 ms 27496 KB Output is correct
46 Correct 2528 ms 27496 KB Output is correct
47 Correct 2400 ms 27460 KB Output is correct
48 Correct 671 ms 23136 KB Output is correct
49 Correct 883 ms 26608 KB Output is correct
50 Correct 322 ms 13432 KB Output is correct
51 Correct 339 ms 16352 KB Output is correct
52 Correct 113 ms 13176 KB Output is correct
53 Correct 1042 ms 25712 KB Output is correct
54 Correct 857 ms 16972 KB Output is correct
55 Correct 2303 ms 22384 KB Output is correct
56 Correct 1702 ms 18080 KB Output is correct
57 Correct 1893 ms 24920 KB Output is correct
58 Correct 91 ms 17012 KB Output is correct
59 Correct 219 ms 15864 KB Output is correct
60 Correct 573 ms 24696 KB Output is correct
61 Correct 640 ms 25440 KB Output is correct
62 Correct 354 ms 22096 KB Output is correct
63 Correct 158 ms 19064 KB Output is correct
64 Correct 144 ms 20468 KB Output is correct
65 Correct 204 ms 26960 KB Output is correct
66 Correct 264 ms 14336 KB Output is correct
67 Correct 215 ms 21876 KB Output is correct
68 Correct 629 ms 27636 KB Output is correct
69 Correct 138 ms 11512 KB Output is correct
70 Correct 37 ms 10076 KB Output is correct
71 Correct 212 ms 17684 KB Output is correct
72 Correct 244 ms 23924 KB Output is correct
73 Correct 1120 ms 29824 KB Output is correct
74 Correct 1153 ms 27656 KB Output is correct
75 Correct 548 ms 29684 KB Output is correct
76 Correct 589 ms 28916 KB Output is correct
77 Correct 1100 ms 27892 KB Output is correct