답안 #57608

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
57608 2018-07-15T12:14:51 Z model_code Cats or Dogs (JOI18_catdog) C++14
100 / 100
426 ms 28604 KB
#include "catdog.h"
#include <bits/stdc++.h>
using namespace std;

using ll=long long;

#define FOR(i,a,b) for(int i=int(a);i<int(b);i++)
#define REP(i,b) FOR(i,0,b)

using vi=vector<int>;
#define PB push_back
using pi=pair<int,int>;

const ll inf64=LLONG_MAX/3;

template<class T,class U>
void chmin(T&t,const U&u){
	if(t>u)t=u;
}
template<class T,class U>
void chmax(T&t,const U&u){
	if(t<u)t=u;
}

struct SegTree{
	struct Node{
		ll cost[2][2];
	};
	vector<Node> buf;
	int s;
	void Init(int n){
		s=1;
		while(s<n)s*=2;
		buf.resize(s*2);
		FOR(i,s,s*2){
			buf[i].cost[0][0]=0;
			buf[i].cost[1][1]=0;
			buf[i].cost[0][1]=inf64;
			buf[i].cost[1][0]=inf64;
		}
		for(int i=s-1;i>=1;i--)
			Update(i);
	}
	void Update(int i){
		REP(a,2)REP(b,2)
			buf[i].cost[a][b]=inf64;
		REP(a,2)REP(b,2)REP(c,2)REP(d,2)
			chmin(buf[i].cost[a][d],buf[i*2].cost[a][b]+(b^c)+buf[i*2+1].cost[c][d]);
	}
	void Add(int i,int v0,int v1){
		i+=s;
		buf[i].cost[0][0]+=v0;
		buf[i].cost[1][1]+=v1;
		while(i>>=1)
			Update(i);
	}
	void Get(int&v0,int&v1){
		int a=min(buf[1].cost[0][0],buf[1].cost[0][1]);
		int b=min(buf[1].cost[1][0],buf[1].cost[1][1]);
		v0=min(a,b+1);
		v1=min(a+1,b);
	}
};
const int Nmax=100010;
int n,par[Nmax],sub[Nmax],grp[Nmax],pos[Nmax],gsz[Nmax],gpar[Nmax],gord,state[Nmax];
vi tr[Nmax];
SegTree segs[Nmax];
int dfs1(int v,int p){
	par[v]=p;
	sub[v]=1;
	for(auto to:tr[v])if(to!=p)
		sub[v]+=dfs1(to,v);
	return sub[v];
}
void dfs2(int v,int p,int g){
	grp[v]=g;
	if(gsz[g]==0)
		gpar[g]=p;
	pos[v]=gsz[g]++;
	pi si(0,-1);
	for(auto to:tr[v])if(to!=p)
		chmax(si,pi(sub[to],to));
	for(auto to:tr[v])if(to!=p){
		if(to==si.second)
			dfs2(to,v,g);
		else
			dfs2(to,v,gord++);
	}
}
void initialize(int N,vi A,vi B){
	n=N;
	REP(i,n-1){
		tr[A[i]-1].PB(B[i]-1);
		tr[B[i]-1].PB(A[i]-1);
	}
	dfs1(0,-1);
	dfs2(0,-1,gord++);
	REP(i,gord)
		segs[i].Init(gsz[i]);
}
int update(int v,int v0,int v1){
	while(v!=-1){
		int g=grp[v],cur0,cur1,nx0,nx1;
		segs[g].Get(cur0,cur1);
		segs[g].Add(pos[v],v0,v1);
		segs[g].Get(nx0,nx1);
		v0=nx0-cur0;
		v1=nx1-cur1;
		v=gpar[g];
	}
	int a,b;
	segs[0].Get(a,b);
	return min(a,b);
}
int cat(int v){
	v--;
	int ret=update(v,Nmax,0);
	state[v]=1;
	return ret;
}
int dog(int v){
	v--;
	int ret=update(v,0,Nmax);
	state[v]=2;
	return ret;
}
int neighbor(int v){
	v--;
	int ret;
	if(state[v]==1)
		ret=update(v,-Nmax,0);
	else
		ret=update(v,0,-Nmax);
	state[v]=0;
	return ret;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 5880 KB Output is correct
2 Correct 8 ms 5996 KB Output is correct
3 Correct 7 ms 5996 KB Output is correct
4 Correct 6 ms 5996 KB Output is correct
5 Correct 8 ms 6152 KB Output is correct
6 Correct 8 ms 6152 KB Output is correct
7 Correct 6 ms 6152 KB Output is correct
8 Correct 7 ms 6152 KB Output is correct
9 Correct 8 ms 6152 KB Output is correct
10 Correct 6 ms 6152 KB Output is correct
11 Correct 9 ms 6152 KB Output is correct
12 Correct 8 ms 6152 KB Output is correct
13 Correct 8 ms 6152 KB Output is correct
14 Correct 6 ms 6152 KB Output is correct
15 Correct 7 ms 6152 KB Output is correct
16 Correct 7 ms 6152 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 5880 KB Output is correct
2 Correct 8 ms 5996 KB Output is correct
3 Correct 7 ms 5996 KB Output is correct
4 Correct 6 ms 5996 KB Output is correct
5 Correct 8 ms 6152 KB Output is correct
6 Correct 8 ms 6152 KB Output is correct
7 Correct 6 ms 6152 KB Output is correct
8 Correct 7 ms 6152 KB Output is correct
9 Correct 8 ms 6152 KB Output is correct
10 Correct 6 ms 6152 KB Output is correct
11 Correct 9 ms 6152 KB Output is correct
12 Correct 8 ms 6152 KB Output is correct
13 Correct 8 ms 6152 KB Output is correct
14 Correct 6 ms 6152 KB Output is correct
15 Correct 7 ms 6152 KB Output is correct
16 Correct 7 ms 6152 KB Output is correct
17 Correct 7 ms 6152 KB Output is correct
18 Correct 9 ms 6252 KB Output is correct
19 Correct 8 ms 6252 KB Output is correct
20 Correct 7 ms 6252 KB Output is correct
21 Correct 9 ms 6252 KB Output is correct
22 Correct 8 ms 6252 KB Output is correct
23 Correct 8 ms 6252 KB Output is correct
24 Correct 11 ms 6252 KB Output is correct
25 Correct 8 ms 6252 KB Output is correct
26 Correct 8 ms 6268 KB Output is correct
27 Correct 7 ms 6268 KB Output is correct
28 Correct 8 ms 6268 KB Output is correct
29 Correct 8 ms 6268 KB Output is correct
30 Correct 7 ms 6268 KB Output is correct
31 Correct 10 ms 6268 KB Output is correct
32 Correct 9 ms 6268 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 5880 KB Output is correct
2 Correct 8 ms 5996 KB Output is correct
3 Correct 7 ms 5996 KB Output is correct
4 Correct 6 ms 5996 KB Output is correct
5 Correct 8 ms 6152 KB Output is correct
6 Correct 8 ms 6152 KB Output is correct
7 Correct 6 ms 6152 KB Output is correct
8 Correct 7 ms 6152 KB Output is correct
9 Correct 8 ms 6152 KB Output is correct
10 Correct 6 ms 6152 KB Output is correct
11 Correct 9 ms 6152 KB Output is correct
12 Correct 8 ms 6152 KB Output is correct
13 Correct 8 ms 6152 KB Output is correct
14 Correct 6 ms 6152 KB Output is correct
15 Correct 7 ms 6152 KB Output is correct
16 Correct 7 ms 6152 KB Output is correct
17 Correct 7 ms 6152 KB Output is correct
18 Correct 9 ms 6252 KB Output is correct
19 Correct 8 ms 6252 KB Output is correct
20 Correct 7 ms 6252 KB Output is correct
21 Correct 9 ms 6252 KB Output is correct
22 Correct 8 ms 6252 KB Output is correct
23 Correct 8 ms 6252 KB Output is correct
24 Correct 11 ms 6252 KB Output is correct
25 Correct 8 ms 6252 KB Output is correct
26 Correct 8 ms 6268 KB Output is correct
27 Correct 7 ms 6268 KB Output is correct
28 Correct 8 ms 6268 KB Output is correct
29 Correct 8 ms 6268 KB Output is correct
30 Correct 7 ms 6268 KB Output is correct
31 Correct 10 ms 6268 KB Output is correct
32 Correct 9 ms 6268 KB Output is correct
33 Correct 204 ms 14548 KB Output is correct
34 Correct 147 ms 15772 KB Output is correct
35 Correct 151 ms 15772 KB Output is correct
36 Correct 287 ms 20708 KB Output is correct
37 Correct 29 ms 20708 KB Output is correct
38 Correct 355 ms 22344 KB Output is correct
39 Correct 310 ms 22344 KB Output is correct
40 Correct 319 ms 22344 KB Output is correct
41 Correct 319 ms 22408 KB Output is correct
42 Correct 334 ms 22412 KB Output is correct
43 Correct 358 ms 22412 KB Output is correct
44 Correct 388 ms 22412 KB Output is correct
45 Correct 426 ms 22500 KB Output is correct
46 Correct 289 ms 22500 KB Output is correct
47 Correct 351 ms 22500 KB Output is correct
48 Correct 158 ms 22500 KB Output is correct
49 Correct 150 ms 22500 KB Output is correct
50 Correct 52 ms 22500 KB Output is correct
51 Correct 62 ms 22500 KB Output is correct
52 Correct 30 ms 22500 KB Output is correct
53 Correct 226 ms 22500 KB Output is correct
54 Correct 141 ms 22500 KB Output is correct
55 Correct 245 ms 22500 KB Output is correct
56 Correct 188 ms 22500 KB Output is correct
57 Correct 273 ms 22500 KB Output is correct
58 Correct 61 ms 22500 KB Output is correct
59 Correct 47 ms 22500 KB Output is correct
60 Correct 120 ms 22500 KB Output is correct
61 Correct 172 ms 22500 KB Output is correct
62 Correct 100 ms 22500 KB Output is correct
63 Correct 104 ms 22500 KB Output is correct
64 Correct 86 ms 22500 KB Output is correct
65 Correct 146 ms 27120 KB Output is correct
66 Correct 79 ms 27120 KB Output is correct
67 Correct 122 ms 27120 KB Output is correct
68 Correct 174 ms 27120 KB Output is correct
69 Correct 42 ms 27120 KB Output is correct
70 Correct 20 ms 27120 KB Output is correct
71 Correct 100 ms 27120 KB Output is correct
72 Correct 122 ms 27120 KB Output is correct
73 Correct 231 ms 27648 KB Output is correct
74 Correct 244 ms 27648 KB Output is correct
75 Correct 283 ms 27708 KB Output is correct
76 Correct 204 ms 28604 KB Output is correct
77 Correct 277 ms 28604 KB Output is correct