#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 |
8284 KB |
Output is correct |
2 |
Correct |
3 ms |
8284 KB |
Output is correct |
3 |
Correct |
3 ms |
8388 KB |
Output is correct |
4 |
Correct |
3 ms |
8284 KB |
Output is correct |
5 |
Correct |
3 ms |
8284 KB |
Output is correct |
6 |
Correct |
4 ms |
8284 KB |
Output is correct |
7 |
Correct |
3 ms |
8284 KB |
Output is correct |
8 |
Correct |
3 ms |
8284 KB |
Output is correct |
9 |
Correct |
3 ms |
8284 KB |
Output is correct |
10 |
Correct |
3 ms |
8284 KB |
Output is correct |
11 |
Correct |
3 ms |
8284 KB |
Output is correct |
12 |
Correct |
4 ms |
8280 KB |
Output is correct |
13 |
Correct |
3 ms |
8284 KB |
Output is correct |
14 |
Correct |
3 ms |
8284 KB |
Output is correct |
15 |
Correct |
3 ms |
8284 KB |
Output is correct |
16 |
Correct |
4 ms |
8284 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
8284 KB |
Output is correct |
2 |
Correct |
3 ms |
8284 KB |
Output is correct |
3 |
Correct |
3 ms |
8388 KB |
Output is correct |
4 |
Correct |
3 ms |
8284 KB |
Output is correct |
5 |
Correct |
3 ms |
8284 KB |
Output is correct |
6 |
Correct |
4 ms |
8284 KB |
Output is correct |
7 |
Correct |
3 ms |
8284 KB |
Output is correct |
8 |
Correct |
3 ms |
8284 KB |
Output is correct |
9 |
Correct |
3 ms |
8284 KB |
Output is correct |
10 |
Correct |
3 ms |
8284 KB |
Output is correct |
11 |
Correct |
3 ms |
8284 KB |
Output is correct |
12 |
Correct |
4 ms |
8280 KB |
Output is correct |
13 |
Correct |
3 ms |
8284 KB |
Output is correct |
14 |
Correct |
3 ms |
8284 KB |
Output is correct |
15 |
Correct |
3 ms |
8284 KB |
Output is correct |
16 |
Correct |
4 ms |
8284 KB |
Output is correct |
17 |
Correct |
4 ms |
8540 KB |
Output is correct |
18 |
Correct |
6 ms |
8472 KB |
Output is correct |
19 |
Correct |
4 ms |
8536 KB |
Output is correct |
20 |
Correct |
4 ms |
8468 KB |
Output is correct |
21 |
Correct |
3 ms |
8284 KB |
Output is correct |
22 |
Correct |
4 ms |
8472 KB |
Output is correct |
23 |
Correct |
5 ms |
8540 KB |
Output is correct |
24 |
Correct |
4 ms |
8472 KB |
Output is correct |
25 |
Correct |
4 ms |
8548 KB |
Output is correct |
26 |
Correct |
3 ms |
8540 KB |
Output is correct |
27 |
Correct |
4 ms |
8284 KB |
Output is correct |
28 |
Correct |
4 ms |
8544 KB |
Output is correct |
29 |
Correct |
4 ms |
8504 KB |
Output is correct |
30 |
Correct |
4 ms |
8280 KB |
Output is correct |
31 |
Correct |
4 ms |
8536 KB |
Output is correct |
32 |
Correct |
5 ms |
8540 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
8284 KB |
Output is correct |
2 |
Correct |
3 ms |
8284 KB |
Output is correct |
3 |
Correct |
3 ms |
8388 KB |
Output is correct |
4 |
Correct |
3 ms |
8284 KB |
Output is correct |
5 |
Correct |
3 ms |
8284 KB |
Output is correct |
6 |
Correct |
4 ms |
8284 KB |
Output is correct |
7 |
Correct |
3 ms |
8284 KB |
Output is correct |
8 |
Correct |
3 ms |
8284 KB |
Output is correct |
9 |
Correct |
3 ms |
8284 KB |
Output is correct |
10 |
Correct |
3 ms |
8284 KB |
Output is correct |
11 |
Correct |
3 ms |
8284 KB |
Output is correct |
12 |
Correct |
4 ms |
8280 KB |
Output is correct |
13 |
Correct |
3 ms |
8284 KB |
Output is correct |
14 |
Correct |
3 ms |
8284 KB |
Output is correct |
15 |
Correct |
3 ms |
8284 KB |
Output is correct |
16 |
Correct |
4 ms |
8284 KB |
Output is correct |
17 |
Correct |
4 ms |
8540 KB |
Output is correct |
18 |
Correct |
6 ms |
8472 KB |
Output is correct |
19 |
Correct |
4 ms |
8536 KB |
Output is correct |
20 |
Correct |
4 ms |
8468 KB |
Output is correct |
21 |
Correct |
3 ms |
8284 KB |
Output is correct |
22 |
Correct |
4 ms |
8472 KB |
Output is correct |
23 |
Correct |
5 ms |
8540 KB |
Output is correct |
24 |
Correct |
4 ms |
8472 KB |
Output is correct |
25 |
Correct |
4 ms |
8548 KB |
Output is correct |
26 |
Correct |
3 ms |
8540 KB |
Output is correct |
27 |
Correct |
4 ms |
8284 KB |
Output is correct |
28 |
Correct |
4 ms |
8544 KB |
Output is correct |
29 |
Correct |
4 ms |
8504 KB |
Output is correct |
30 |
Correct |
4 ms |
8280 KB |
Output is correct |
31 |
Correct |
4 ms |
8536 KB |
Output is correct |
32 |
Correct |
5 ms |
8540 KB |
Output is correct |
33 |
Correct |
227 ms |
14292 KB |
Output is correct |
34 |
Correct |
81 ms |
14156 KB |
Output is correct |
35 |
Correct |
239 ms |
13168 KB |
Output is correct |
36 |
Correct |
353 ms |
18136 KB |
Output is correct |
37 |
Correct |
13 ms |
11096 KB |
Output is correct |
38 |
Correct |
390 ms |
19056 KB |
Output is correct |
39 |
Correct |
379 ms |
19012 KB |
Output is correct |
40 |
Correct |
369 ms |
19188 KB |
Output is correct |
41 |
Correct |
353 ms |
19176 KB |
Output is correct |
42 |
Correct |
340 ms |
19004 KB |
Output is correct |
43 |
Correct |
399 ms |
19168 KB |
Output is correct |
44 |
Correct |
345 ms |
19056 KB |
Output is correct |
45 |
Correct |
340 ms |
19220 KB |
Output is correct |
46 |
Correct |
352 ms |
19112 KB |
Output is correct |
47 |
Correct |
363 ms |
19008 KB |
Output is correct |
48 |
Correct |
123 ms |
15856 KB |
Output is correct |
49 |
Correct |
126 ms |
17532 KB |
Output is correct |
50 |
Correct |
48 ms |
10592 KB |
Output is correct |
51 |
Correct |
53 ms |
11984 KB |
Output is correct |
52 |
Correct |
23 ms |
10328 KB |
Output is correct |
53 |
Correct |
157 ms |
17432 KB |
Output is correct |
54 |
Correct |
122 ms |
12624 KB |
Output is correct |
55 |
Correct |
306 ms |
16204 KB |
Output is correct |
56 |
Correct |
182 ms |
13564 KB |
Output is correct |
57 |
Correct |
267 ms |
17516 KB |
Output is correct |
58 |
Correct |
23 ms |
11988 KB |
Output is correct |
59 |
Correct |
51 ms |
11868 KB |
Output is correct |
60 |
Correct |
124 ms |
16736 KB |
Output is correct |
61 |
Correct |
125 ms |
17212 KB |
Output is correct |
62 |
Correct |
72 ms |
15060 KB |
Output is correct |
63 |
Correct |
34 ms |
15696 KB |
Output is correct |
64 |
Correct |
35 ms |
17244 KB |
Output is correct |
65 |
Correct |
41 ms |
22108 KB |
Output is correct |
66 |
Correct |
53 ms |
12184 KB |
Output is correct |
67 |
Correct |
49 ms |
18268 KB |
Output is correct |
68 |
Correct |
126 ms |
22612 KB |
Output is correct |
69 |
Correct |
24 ms |
9852 KB |
Output is correct |
70 |
Correct |
9 ms |
8536 KB |
Output is correct |
71 |
Correct |
44 ms |
15188 KB |
Output is correct |
72 |
Correct |
63 ms |
20676 KB |
Output is correct |
73 |
Correct |
158 ms |
25940 KB |
Output is correct |
74 |
Correct |
181 ms |
22356 KB |
Output is correct |
75 |
Correct |
109 ms |
25772 KB |
Output is correct |
76 |
Correct |
139 ms |
24404 KB |
Output is correct |
77 |
Correct |
174 ms |
22568 KB |
Output is correct |