#include "catdog.h"
#define M 100100
#include<bits/stdc++.h>
using namespace std;
int type[M],sz[M],id[M],top[M],out[M],hev[M],CC,par[M],osc[M],BG=1e9;
struct dpstruct{
int cc,cd,dc,dd;
dpstruct(){cc=dd=0,cd=dc=1;}
dpstruct(int x){cc=dd=0,cd=dc=BG;}
inline friend dpstruct operator +(dpstruct a,dpstruct b){
dpstruct c;
swap(a,b);
c.cc=min({a.cc+b.cc,a.cd+b.dc,BG});
c.cd=min({a.cc+b.cd,a.cd+b.dd,BG});
c.dc=min({a.dd+b.dc,a.dc+b.cc,BG});
c.dd=min({a.dd+b.dd,a.dc+b.cd,BG});
return c;
}
inline void sub(dpstruct x){
int dog=min(x.cd,x.dd);
int cat=min(x.cc,x.dc);
dog=min(dog,cat+1);
cat=min(cat,dog+1);
cc-=cat;
cd-=dog;
dc-=cat;
dd-=dog;
}
inline void add(dpstruct x){
int dog=min(x.cd,x.dd);
int cat=min(x.cc,x.dc);
dog=min(dog,cat+1);
cat=min(cat,dog+1);
cc+=cat;
cd+=dog;
dc+=cat;
dd+=dog;
}
inline int an(){return min({cc,cd,dc,dd});}
} T[1<<18],fl[M];
vector<int>adj[M];
dpstruct query(int i,int l,int r,int ll,int rr){
if(ll>r|l>rr)return dpstruct(0);
if(ll<=l&&r<=rr)return T[i];
return query(i*2,l,l+r>>1,ll,rr)+
query(i*2+1,l+r+2>>1,r,ll,rr);
}
void upd(int i,int l,int r,int p,dpstruct x){
if(l==r)return void(T[i]=x);
if(p<=l+r>>1)upd(i*2,l,l+r>>1,p,x);
else upd(i*2+1,l+r+2>>1,r,p,x);
T[i]=T[i*2]+T[i*2+1];
}
void dfs1(int n){
sz[n]=1;
for(auto i:adj[n]) if(i-par[n])
par[i]=n,dfs1(i),sz[n]+=sz[i],
hev[n]=(sz[hev[n]]<sz[i]?i:hev[n]);
}
void dfs2(int n){
osc[id[n]=++CC]=n;
top[n]=(n-hev[par[n]]?n:top[par[n]]);
out[top[n]]=CC;
if(hev[n])dfs2(hev[n]);
for(auto i:adj[n])
if(i-par[n]&&i-hev[n])
dfs2(i);
}
void initialize(int N, std::vector<int> A, std::vector<int> B) {
for(int i=0;i<N-1;i++)
adj[A[i]].push_back(B[i]),
adj[B[i]].push_back(A[i]);
dfs1(1);
dfs2(1);
}
dpstruct gedan(int chain){
return query(1,1,CC,id[top[chain]],out[top[chain]]);
}
dpstruct idk(int t,dpstruct x){
if(t>1)x.cc=x.dc=BG;
else if(t)x.cd=x.dd=BG;
return x;
}
void redo(int n){
if(!n)return;
fl[par[top[n]]].sub(gedan(n));
upd(1,1,CC,id[n],idk(type[n],fl[n]));
fl[par[top[n]]].add(gedan(n));
return redo(par[top[n]]);
}
int cat(int v) {
type[v]=1;
redo(v);
return gedan(1).an();
}
int dog(int v) {
type[v]=2;
redo(v);
return gedan(1).an();
}
int neighbor(int v) {
type[v]=0;
redo(v);
return gedan(1).an();
}
Compilation message
catdog.cpp: In function 'dpstruct query(int, int, int, int, int)':
catdog.cpp:43:10: warning: suggest parentheses around comparison in operand of '|' [-Wparentheses]
43 | if(ll>r|l>rr)return dpstruct(0);
| ~~^~
catdog.cpp:45:25: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
45 | return query(i*2,l,l+r>>1,ll,rr)+
| ~^~
catdog.cpp:46:24: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
46 | query(i*2+1,l+r+2>>1,r,ll,rr);
| ~~~^~
catdog.cpp: In function 'void upd(int, int, int, int, dpstruct)':
catdog.cpp:50:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
50 | if(p<=l+r>>1)upd(i*2,l,l+r>>1,p,x);
| ~^~
catdog.cpp:50:29: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
50 | if(p<=l+r>>1)upd(i*2,l,l+r>>1,p,x);
| ~^~
catdog.cpp:51:23: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
51 | else upd(i*2+1,l+r+2>>1,r,p,x);
| ~~~^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
8280 KB |
Output is correct |
2 |
Correct |
3 ms |
8284 KB |
Output is correct |
3 |
Correct |
3 ms |
8284 KB |
Output is correct |
4 |
Correct |
4 ms |
8500 KB |
Output is correct |
5 |
Correct |
3 ms |
8284 KB |
Output is correct |
6 |
Correct |
4 ms |
8284 KB |
Output is correct |
7 |
Correct |
4 ms |
8280 KB |
Output is correct |
8 |
Correct |
4 ms |
8284 KB |
Output is correct |
9 |
Correct |
3 ms |
8284 KB |
Output is correct |
10 |
Correct |
4 ms |
8284 KB |
Output is correct |
11 |
Correct |
3 ms |
8284 KB |
Output is correct |
12 |
Correct |
3 ms |
8284 KB |
Output is correct |
13 |
Correct |
4 ms |
8284 KB |
Output is correct |
14 |
Correct |
3 ms |
8284 KB |
Output is correct |
15 |
Correct |
4 ms |
8284 KB |
Output is correct |
16 |
Correct |
4 ms |
8284 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
8280 KB |
Output is correct |
2 |
Correct |
3 ms |
8284 KB |
Output is correct |
3 |
Correct |
3 ms |
8284 KB |
Output is correct |
4 |
Correct |
4 ms |
8500 KB |
Output is correct |
5 |
Correct |
3 ms |
8284 KB |
Output is correct |
6 |
Correct |
4 ms |
8284 KB |
Output is correct |
7 |
Correct |
4 ms |
8280 KB |
Output is correct |
8 |
Correct |
4 ms |
8284 KB |
Output is correct |
9 |
Correct |
3 ms |
8284 KB |
Output is correct |
10 |
Correct |
4 ms |
8284 KB |
Output is correct |
11 |
Correct |
3 ms |
8284 KB |
Output is correct |
12 |
Correct |
3 ms |
8284 KB |
Output is correct |
13 |
Correct |
4 ms |
8284 KB |
Output is correct |
14 |
Correct |
3 ms |
8284 KB |
Output is correct |
15 |
Correct |
4 ms |
8284 KB |
Output is correct |
16 |
Correct |
4 ms |
8284 KB |
Output is correct |
17 |
Correct |
5 ms |
8384 KB |
Output is correct |
18 |
Correct |
5 ms |
8540 KB |
Output is correct |
19 |
Correct |
6 ms |
8552 KB |
Output is correct |
20 |
Correct |
4 ms |
8460 KB |
Output is correct |
21 |
Correct |
4 ms |
8284 KB |
Output is correct |
22 |
Correct |
4 ms |
8540 KB |
Output is correct |
23 |
Correct |
6 ms |
8376 KB |
Output is correct |
24 |
Correct |
5 ms |
8536 KB |
Output is correct |
25 |
Correct |
4 ms |
8540 KB |
Output is correct |
26 |
Correct |
4 ms |
8540 KB |
Output is correct |
27 |
Correct |
4 ms |
8464 KB |
Output is correct |
28 |
Correct |
4 ms |
8544 KB |
Output is correct |
29 |
Correct |
4 ms |
8540 KB |
Output is correct |
30 |
Correct |
3 ms |
8392 KB |
Output is correct |
31 |
Correct |
4 ms |
8540 KB |
Output is correct |
32 |
Correct |
4 ms |
8540 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
8280 KB |
Output is correct |
2 |
Correct |
3 ms |
8284 KB |
Output is correct |
3 |
Correct |
3 ms |
8284 KB |
Output is correct |
4 |
Correct |
4 ms |
8500 KB |
Output is correct |
5 |
Correct |
3 ms |
8284 KB |
Output is correct |
6 |
Correct |
4 ms |
8284 KB |
Output is correct |
7 |
Correct |
4 ms |
8280 KB |
Output is correct |
8 |
Correct |
4 ms |
8284 KB |
Output is correct |
9 |
Correct |
3 ms |
8284 KB |
Output is correct |
10 |
Correct |
4 ms |
8284 KB |
Output is correct |
11 |
Correct |
3 ms |
8284 KB |
Output is correct |
12 |
Correct |
3 ms |
8284 KB |
Output is correct |
13 |
Correct |
4 ms |
8284 KB |
Output is correct |
14 |
Correct |
3 ms |
8284 KB |
Output is correct |
15 |
Correct |
4 ms |
8284 KB |
Output is correct |
16 |
Correct |
4 ms |
8284 KB |
Output is correct |
17 |
Correct |
5 ms |
8384 KB |
Output is correct |
18 |
Correct |
5 ms |
8540 KB |
Output is correct |
19 |
Correct |
6 ms |
8552 KB |
Output is correct |
20 |
Correct |
4 ms |
8460 KB |
Output is correct |
21 |
Correct |
4 ms |
8284 KB |
Output is correct |
22 |
Correct |
4 ms |
8540 KB |
Output is correct |
23 |
Correct |
6 ms |
8376 KB |
Output is correct |
24 |
Correct |
5 ms |
8536 KB |
Output is correct |
25 |
Correct |
4 ms |
8540 KB |
Output is correct |
26 |
Correct |
4 ms |
8540 KB |
Output is correct |
27 |
Correct |
4 ms |
8464 KB |
Output is correct |
28 |
Correct |
4 ms |
8544 KB |
Output is correct |
29 |
Correct |
4 ms |
8540 KB |
Output is correct |
30 |
Correct |
3 ms |
8392 KB |
Output is correct |
31 |
Correct |
4 ms |
8540 KB |
Output is correct |
32 |
Correct |
4 ms |
8540 KB |
Output is correct |
33 |
Correct |
255 ms |
14340 KB |
Output is correct |
34 |
Correct |
74 ms |
14144 KB |
Output is correct |
35 |
Correct |
208 ms |
13132 KB |
Output is correct |
36 |
Correct |
381 ms |
18044 KB |
Output is correct |
37 |
Correct |
15 ms |
11096 KB |
Output is correct |
38 |
Correct |
439 ms |
19084 KB |
Output is correct |
39 |
Correct |
376 ms |
19012 KB |
Output is correct |
40 |
Correct |
375 ms |
19004 KB |
Output is correct |
41 |
Correct |
395 ms |
19004 KB |
Output is correct |
42 |
Correct |
369 ms |
19176 KB |
Output is correct |
43 |
Correct |
345 ms |
19072 KB |
Output is correct |
44 |
Correct |
362 ms |
19212 KB |
Output is correct |
45 |
Correct |
332 ms |
19008 KB |
Output is correct |
46 |
Correct |
375 ms |
19008 KB |
Output is correct |
47 |
Correct |
366 ms |
19008 KB |
Output is correct |
48 |
Correct |
109 ms |
15904 KB |
Output is correct |
49 |
Correct |
129 ms |
17652 KB |
Output is correct |
50 |
Correct |
50 ms |
10588 KB |
Output is correct |
51 |
Correct |
51 ms |
12120 KB |
Output is correct |
52 |
Correct |
22 ms |
10272 KB |
Output is correct |
53 |
Correct |
155 ms |
17500 KB |
Output is correct |
54 |
Correct |
120 ms |
12624 KB |
Output is correct |
55 |
Correct |
330 ms |
16152 KB |
Output is correct |
56 |
Correct |
183 ms |
13560 KB |
Output is correct |
57 |
Correct |
253 ms |
17528 KB |
Output is correct |
58 |
Correct |
26 ms |
11988 KB |
Output is correct |
59 |
Correct |
51 ms |
12140 KB |
Output is correct |
60 |
Correct |
111 ms |
16608 KB |
Output is correct |
61 |
Correct |
113 ms |
17212 KB |
Output is correct |
62 |
Correct |
67 ms |
15000 KB |
Output is correct |
63 |
Correct |
41 ms |
15536 KB |
Output is correct |
64 |
Correct |
40 ms |
17244 KB |
Output is correct |
65 |
Correct |
42 ms |
22104 KB |
Output is correct |
66 |
Correct |
54 ms |
12124 KB |
Output is correct |
67 |
Correct |
49 ms |
18268 KB |
Output is correct |
68 |
Correct |
101 ms |
22672 KB |
Output is correct |
69 |
Correct |
24 ms |
10076 KB |
Output is correct |
70 |
Correct |
8 ms |
8540 KB |
Output is correct |
71 |
Correct |
45 ms |
15448 KB |
Output is correct |
72 |
Correct |
65 ms |
20564 KB |
Output is correct |
73 |
Correct |
148 ms |
25752 KB |
Output is correct |
74 |
Correct |
175 ms |
22168 KB |
Output is correct |
75 |
Correct |
120 ms |
25856 KB |
Output is correct |
76 |
Correct |
110 ms |
24396 KB |
Output is correct |
77 |
Correct |
177 ms |
22616 KB |
Output is correct |