Submission #57607

# Submission time Handle Problem Language Result Execution time Memory
57607 2018-07-15T12:14:06 Z model_code Cats or Dogs (JOI18_catdog) C++17
100 / 100
460 ms 28480 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;
}
# Verdict Execution time Memory Grader output
1 Correct 7 ms 5752 KB Output is correct
2 Correct 6 ms 5864 KB Output is correct
3 Correct 8 ms 5900 KB Output is correct
4 Correct 8 ms 5900 KB Output is correct
5 Correct 9 ms 5976 KB Output is correct
6 Correct 7 ms 6052 KB Output is correct
7 Correct 7 ms 6052 KB Output is correct
8 Correct 9 ms 6128 KB Output is correct
9 Correct 8 ms 6128 KB Output is correct
10 Correct 7 ms 6128 KB Output is correct
11 Correct 8 ms 6248 KB Output is correct
12 Correct 7 ms 6248 KB Output is correct
13 Correct 8 ms 6248 KB Output is correct
14 Correct 9 ms 6248 KB Output is correct
15 Correct 7 ms 6248 KB Output is correct
16 Correct 9 ms 6248 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 5752 KB Output is correct
2 Correct 6 ms 5864 KB Output is correct
3 Correct 8 ms 5900 KB Output is correct
4 Correct 8 ms 5900 KB Output is correct
5 Correct 9 ms 5976 KB Output is correct
6 Correct 7 ms 6052 KB Output is correct
7 Correct 7 ms 6052 KB Output is correct
8 Correct 9 ms 6128 KB Output is correct
9 Correct 8 ms 6128 KB Output is correct
10 Correct 7 ms 6128 KB Output is correct
11 Correct 8 ms 6248 KB Output is correct
12 Correct 7 ms 6248 KB Output is correct
13 Correct 8 ms 6248 KB Output is correct
14 Correct 9 ms 6248 KB Output is correct
15 Correct 7 ms 6248 KB Output is correct
16 Correct 9 ms 6248 KB Output is correct
17 Correct 9 ms 6248 KB Output is correct
18 Correct 8 ms 6256 KB Output is correct
19 Correct 8 ms 6256 KB Output is correct
20 Correct 7 ms 6256 KB Output is correct
21 Correct 8 ms 6256 KB Output is correct
22 Correct 9 ms 6256 KB Output is correct
23 Correct 10 ms 6256 KB Output is correct
24 Correct 10 ms 6256 KB Output is correct
25 Correct 9 ms 6256 KB Output is correct
26 Correct 8 ms 6256 KB Output is correct
27 Correct 8 ms 6256 KB Output is correct
28 Correct 9 ms 6272 KB Output is correct
29 Correct 9 ms 6272 KB Output is correct
30 Correct 8 ms 6272 KB Output is correct
31 Correct 7 ms 6272 KB Output is correct
32 Correct 8 ms 6272 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 5752 KB Output is correct
2 Correct 6 ms 5864 KB Output is correct
3 Correct 8 ms 5900 KB Output is correct
4 Correct 8 ms 5900 KB Output is correct
5 Correct 9 ms 5976 KB Output is correct
6 Correct 7 ms 6052 KB Output is correct
7 Correct 7 ms 6052 KB Output is correct
8 Correct 9 ms 6128 KB Output is correct
9 Correct 8 ms 6128 KB Output is correct
10 Correct 7 ms 6128 KB Output is correct
11 Correct 8 ms 6248 KB Output is correct
12 Correct 7 ms 6248 KB Output is correct
13 Correct 8 ms 6248 KB Output is correct
14 Correct 9 ms 6248 KB Output is correct
15 Correct 7 ms 6248 KB Output is correct
16 Correct 9 ms 6248 KB Output is correct
17 Correct 9 ms 6248 KB Output is correct
18 Correct 8 ms 6256 KB Output is correct
19 Correct 8 ms 6256 KB Output is correct
20 Correct 7 ms 6256 KB Output is correct
21 Correct 8 ms 6256 KB Output is correct
22 Correct 9 ms 6256 KB Output is correct
23 Correct 10 ms 6256 KB Output is correct
24 Correct 10 ms 6256 KB Output is correct
25 Correct 9 ms 6256 KB Output is correct
26 Correct 8 ms 6256 KB Output is correct
27 Correct 8 ms 6256 KB Output is correct
28 Correct 9 ms 6272 KB Output is correct
29 Correct 9 ms 6272 KB Output is correct
30 Correct 8 ms 6272 KB Output is correct
31 Correct 7 ms 6272 KB Output is correct
32 Correct 8 ms 6272 KB Output is correct
33 Correct 202 ms 14576 KB Output is correct
34 Correct 129 ms 15876 KB Output is correct
35 Correct 179 ms 15876 KB Output is correct
36 Correct 393 ms 20708 KB Output is correct
37 Correct 46 ms 20708 KB Output is correct
38 Correct 389 ms 22304 KB Output is correct
39 Correct 428 ms 22328 KB Output is correct
40 Correct 415 ms 22328 KB Output is correct
41 Correct 364 ms 22328 KB Output is correct
42 Correct 460 ms 22448 KB Output is correct
43 Correct 370 ms 22516 KB Output is correct
44 Correct 313 ms 22516 KB Output is correct
45 Correct 419 ms 22516 KB Output is correct
46 Correct 415 ms 22516 KB Output is correct
47 Correct 376 ms 22516 KB Output is correct
48 Correct 119 ms 22516 KB Output is correct
49 Correct 213 ms 22516 KB Output is correct
50 Correct 64 ms 22516 KB Output is correct
51 Correct 82 ms 22516 KB Output is correct
52 Correct 27 ms 22516 KB Output is correct
53 Correct 198 ms 22516 KB Output is correct
54 Correct 145 ms 22516 KB Output is correct
55 Correct 320 ms 22516 KB Output is correct
56 Correct 134 ms 22516 KB Output is correct
57 Correct 205 ms 22516 KB Output is correct
58 Correct 41 ms 22516 KB Output is correct
59 Correct 61 ms 22516 KB Output is correct
60 Correct 142 ms 22516 KB Output is correct
61 Correct 140 ms 22516 KB Output is correct
62 Correct 72 ms 22516 KB Output is correct
63 Correct 104 ms 22516 KB Output is correct
64 Correct 93 ms 22516 KB Output is correct
65 Correct 152 ms 27184 KB Output is correct
66 Correct 82 ms 27184 KB Output is correct
67 Correct 162 ms 27184 KB Output is correct
68 Correct 212 ms 27184 KB Output is correct
69 Correct 43 ms 27184 KB Output is correct
70 Correct 16 ms 27184 KB Output is correct
71 Correct 99 ms 27184 KB Output is correct
72 Correct 143 ms 27184 KB Output is correct
73 Correct 254 ms 27740 KB Output is correct
74 Correct 331 ms 27740 KB Output is correct
75 Correct 287 ms 27840 KB Output is correct
76 Correct 261 ms 28480 KB Output is correct
77 Correct 287 ms 28480 KB Output is correct