Submission #98373

# Submission time Handle Problem Language Result Execution time Memory
98373 2019-02-22T19:10:34 Z scanhex Cats or Dogs (JOI18_catdog) C++17
100 / 100
1640 ms 28920 KB
#include<bits/stdc++.h>
#include "catdog.h"

using namespace std;
const int N=1<<17;
using val=array<array<int,2>,2>;
val t[2*N];

int par[N];
int cur[N];

#define chmi(x,y) if(x>y)x=y
#define chma(x,y) if(x<y)x=y

const int oo=100001;

int invind[N];

val gett(int x){
	if(x<N)
		return t[x];
	val ans;
	int u=invind[x-N];
	for(int i=0;i<2;++i)
		for(int j=0;j<2;++j)
			if(i==j&&((i+1)&cur[u]))
				ans[i][j]=t[x][i][j];
			else
				ans[i][j]=oo;
	return ans;
}

val merge(val a,val b){
	val ans;
	for(int st=0;st<2;++st)
		for(int en=0;en<2;++en){
			ans[st][en]=oo;
			for(int le=0;le<2;++le)
				for(int ri=0;ri<2;++ri){
					chmi(ans[st][en],a[st][le]+b[ri][en]+(le!=ri));
				}
		}
	return ans;
}

void upd1(int x){
	t[x]=merge(gett(x<<1),gett(x<<1^1));
}

void upd(int x){
	x+=N;
	while(x>1)
		upd1(x>>1),x>>=1;
}
val neut={0,oo,oo,0};
val get(int l,int r){
	l+=N;
	r+=N;
	val ansle=neut,ansri=neut;
	while(l<r){
		if(l&1)
			ansle=merge(ansle,gett(l++));
		if(r&1)
			ansri=merge(gett(--r),ansri);
		l/=2,r/=2;
	}
	return merge(ansle,ansri);
}

vector<int> ch[N];
int ind[N];
int sz[N];
int top[N];
int listok[N];

void update_c(int x,int coef){
	upd(ind[x]);
	vector<int>memos;
	while(true){
		x=top[x];
		if(x==0)break;
		memos.push_back(x);
		x=par[x];
	}
	if(coef==-1)reverse(memos.begin(),memos.end());
	for(int x:memos){
		val kek=get(ind[x],ind[listok[x]]+1);
		x=par[x];
		int y=ind[x]+N;
		int k0=min(kek[0][0],kek[0][1]),k1=min(kek[1][0],kek[1][1]);
		if(k0>=oo&&k1>=oo){
			int aoeustnaoeu=22390;
		}
		assert(k0<oo||k1<oo);
		t[y][0][0]+=coef*(min(k0,k1+1));
		t[y][1][1]+=coef*(min(k1,k0+1));
		upd(ind[x]);
	}
}

void update(int x,int nw){
	update_c(x,-1);
	cur[x]=nw;
	update_c(x,1);
}


vector<int>g[N];

void d1(int u,int p=-1){
	par[u]=p;
	sz[u]=1;
	for(int v:g[u]){
		if(v==p)continue;
		ch[u].push_back(v);
		d1(v,u);
		sz[u]+=sz[v];
		if(sz[v]>sz[ch[u][0]])
			swap(ch[u].back(),ch[u][0]);
	}
	if(ch[u].size())listok[u]=listok[ch[u][0]];
	else listok[u]=u;
}

int tot=0;


void d2(int u){
	ind[u]=tot++;
	invind[ind[u]]=u;
	for(int v:ch[u]){
		if(v==ch[u][0])
			top[v]=top[u];
		else
			top[v]=v;
		d2(v);
	}
}
void d3(int u){
	update_c(u,1);
	for(int v:ch[u])d3(v);
}

int n;

void initialize(int N1, std::vector<int> A, std::vector<int> B) {
	n=N1;
	for(int i=0;i<n-1;++i)
		g[A[i]-1].push_back(B[i]-1),g[B[i]-1].push_back(A[i]-1);
	fill(cur,cur+n,3);
	d1(0);
	d2(0);
	d3(0);
}

int get(){
	val mem=get(ind[0],ind[listok[0]]+1);
	int ans=oo;
	for(int i=0;i<2;++i)
		for(int j=0;j<2;++j)
			ans=min(ans,mem[i][j]);
	return ans;
}

int cat(int v) {
	update(v-1,1);
	return get();
}

int dog(int v) {
	update(v-1,2);
	return get();
}

int neighbor(int v) {
	update(v-1,3);
	return get();
}

Compilation message

catdog.cpp: In function 'void update_c(int, int)':
catdog.cpp:92:8: warning: unused variable 'aoeustnaoeu' [-Wunused-variable]
    int aoeustnaoeu=22390;
        ^~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 8 ms 6528 KB Output is correct
2 Correct 8 ms 6500 KB Output is correct
3 Correct 9 ms 6528 KB Output is correct
4 Correct 9 ms 6528 KB Output is correct
5 Correct 7 ms 6528 KB Output is correct
6 Correct 8 ms 6528 KB Output is correct
7 Correct 7 ms 6544 KB Output is correct
8 Correct 7 ms 6528 KB Output is correct
9 Correct 9 ms 6528 KB Output is correct
10 Correct 7 ms 6528 KB Output is correct
11 Correct 9 ms 6528 KB Output is correct
12 Correct 9 ms 6528 KB Output is correct
13 Correct 8 ms 6528 KB Output is correct
14 Correct 8 ms 6656 KB Output is correct
15 Correct 7 ms 6528 KB Output is correct
16 Correct 8 ms 6528 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 6528 KB Output is correct
2 Correct 8 ms 6500 KB Output is correct
3 Correct 9 ms 6528 KB Output is correct
4 Correct 9 ms 6528 KB Output is correct
5 Correct 7 ms 6528 KB Output is correct
6 Correct 8 ms 6528 KB Output is correct
7 Correct 7 ms 6544 KB Output is correct
8 Correct 7 ms 6528 KB Output is correct
9 Correct 9 ms 6528 KB Output is correct
10 Correct 7 ms 6528 KB Output is correct
11 Correct 9 ms 6528 KB Output is correct
12 Correct 9 ms 6528 KB Output is correct
13 Correct 8 ms 6528 KB Output is correct
14 Correct 8 ms 6656 KB Output is correct
15 Correct 7 ms 6528 KB Output is correct
16 Correct 8 ms 6528 KB Output is correct
17 Correct 11 ms 6656 KB Output is correct
18 Correct 16 ms 6656 KB Output is correct
19 Correct 14 ms 6656 KB Output is correct
20 Correct 10 ms 6656 KB Output is correct
21 Correct 12 ms 6656 KB Output is correct
22 Correct 10 ms 6656 KB Output is correct
23 Correct 12 ms 6656 KB Output is correct
24 Correct 13 ms 6656 KB Output is correct
25 Correct 12 ms 6656 KB Output is correct
26 Correct 10 ms 6656 KB Output is correct
27 Correct 9 ms 6656 KB Output is correct
28 Correct 9 ms 6784 KB Output is correct
29 Correct 13 ms 6784 KB Output is correct
30 Correct 9 ms 6656 KB Output is correct
31 Correct 11 ms 6656 KB Output is correct
32 Correct 9 ms 6656 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 6528 KB Output is correct
2 Correct 8 ms 6500 KB Output is correct
3 Correct 9 ms 6528 KB Output is correct
4 Correct 9 ms 6528 KB Output is correct
5 Correct 7 ms 6528 KB Output is correct
6 Correct 8 ms 6528 KB Output is correct
7 Correct 7 ms 6544 KB Output is correct
8 Correct 7 ms 6528 KB Output is correct
9 Correct 9 ms 6528 KB Output is correct
10 Correct 7 ms 6528 KB Output is correct
11 Correct 9 ms 6528 KB Output is correct
12 Correct 9 ms 6528 KB Output is correct
13 Correct 8 ms 6528 KB Output is correct
14 Correct 8 ms 6656 KB Output is correct
15 Correct 7 ms 6528 KB Output is correct
16 Correct 8 ms 6528 KB Output is correct
17 Correct 11 ms 6656 KB Output is correct
18 Correct 16 ms 6656 KB Output is correct
19 Correct 14 ms 6656 KB Output is correct
20 Correct 10 ms 6656 KB Output is correct
21 Correct 12 ms 6656 KB Output is correct
22 Correct 10 ms 6656 KB Output is correct
23 Correct 12 ms 6656 KB Output is correct
24 Correct 13 ms 6656 KB Output is correct
25 Correct 12 ms 6656 KB Output is correct
26 Correct 10 ms 6656 KB Output is correct
27 Correct 9 ms 6656 KB Output is correct
28 Correct 9 ms 6784 KB Output is correct
29 Correct 13 ms 6784 KB Output is correct
30 Correct 9 ms 6656 KB Output is correct
31 Correct 11 ms 6656 KB Output is correct
32 Correct 9 ms 6656 KB Output is correct
33 Correct 917 ms 13784 KB Output is correct
34 Correct 391 ms 14944 KB Output is correct
35 Correct 766 ms 12020 KB Output is correct
36 Correct 1474 ms 18824 KB Output is correct
37 Correct 114 ms 10488 KB Output is correct
38 Correct 1555 ms 20152 KB Output is correct
39 Correct 1600 ms 20156 KB Output is correct
40 Correct 1497 ms 20332 KB Output is correct
41 Correct 1640 ms 20204 KB Output is correct
42 Correct 1517 ms 20344 KB Output is correct
43 Correct 1609 ms 20172 KB Output is correct
44 Correct 1494 ms 20164 KB Output is correct
45 Correct 1454 ms 20296 KB Output is correct
46 Correct 1365 ms 20200 KB Output is correct
47 Correct 1426 ms 20104 KB Output is correct
48 Correct 435 ms 14420 KB Output is correct
49 Correct 462 ms 16364 KB Output is correct
50 Correct 141 ms 8952 KB Output is correct
51 Correct 197 ms 10612 KB Output is correct
52 Correct 75 ms 8828 KB Output is correct
53 Correct 797 ms 19316 KB Output is correct
54 Correct 523 ms 12340 KB Output is correct
55 Correct 1129 ms 16216 KB Output is correct
56 Correct 772 ms 12940 KB Output is correct
57 Correct 920 ms 18584 KB Output is correct
58 Correct 104 ms 10996 KB Output is correct
59 Correct 226 ms 10488 KB Output is correct
60 Correct 351 ms 15344 KB Output is correct
61 Correct 418 ms 15728 KB Output is correct
62 Correct 240 ms 14060 KB Output is correct
63 Correct 160 ms 16576 KB Output is correct
64 Correct 166 ms 18640 KB Output is correct
65 Correct 269 ms 25464 KB Output is correct
66 Correct 140 ms 11256 KB Output is correct
67 Correct 215 ms 19816 KB Output is correct
68 Correct 384 ms 25336 KB Output is correct
69 Correct 60 ms 8312 KB Output is correct
70 Correct 23 ms 6912 KB Output is correct
71 Correct 139 ms 15268 KB Output is correct
72 Correct 256 ms 22520 KB Output is correct
73 Correct 532 ms 28920 KB Output is correct
74 Correct 518 ms 24696 KB Output is correct
75 Correct 349 ms 28900 KB Output is correct
76 Correct 373 ms 27300 KB Output is correct
77 Correct 530 ms 25080 KB Output is correct