#pragma GCC optimize(3)
#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:44:10: warning: suggest parentheses around comparison in operand of '|' [-Wparentheses]
44 | if(ll>r|l>rr)return dpstruct(0);
| ~~^~
catdog.cpp:46:25: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
46 | return query(i*2,l,l+r>>1,ll,rr)+
| ~^~
catdog.cpp:47:24: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
47 | query(i*2+1,l+r+2>>1,r,ll,rr);
| ~~~^~
catdog.cpp: In function 'void upd(int, int, int, int, dpstruct)':
catdog.cpp:51:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
51 | if(p<=l+r>>1)upd(i*2,l,l+r>>1,p,x);
| ~^~
catdog.cpp:51:29: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
51 | if(p<=l+r>>1)upd(i*2,l,l+r>>1,p,x);
| ~^~
catdog.cpp:52:23: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
52 | else upd(i*2+1,l+r+2>>1,r,p,x);
| ~~~^~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
8284 KB |
Output is correct |
2 |
Correct |
4 ms |
8284 KB |
Output is correct |
3 |
Correct |
4 ms |
8284 KB |
Output is correct |
4 |
Correct |
4 ms |
8508 KB |
Output is correct |
5 |
Correct |
4 ms |
8284 KB |
Output is correct |
6 |
Correct |
7 ms |
8284 KB |
Output is correct |
7 |
Correct |
4 ms |
8284 KB |
Output is correct |
8 |
Correct |
4 ms |
8284 KB |
Output is correct |
9 |
Correct |
4 ms |
8284 KB |
Output is correct |
10 |
Correct |
4 ms |
8284 KB |
Output is correct |
11 |
Correct |
4 ms |
8284 KB |
Output is correct |
12 |
Correct |
4 ms |
8284 KB |
Output is correct |
13 |
Correct |
4 ms |
8284 KB |
Output is correct |
14 |
Correct |
4 ms |
8284 KB |
Output is correct |
15 |
Correct |
4 ms |
8284 KB |
Output is correct |
16 |
Correct |
3 ms |
8464 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
8284 KB |
Output is correct |
2 |
Correct |
4 ms |
8284 KB |
Output is correct |
3 |
Correct |
4 ms |
8284 KB |
Output is correct |
4 |
Correct |
4 ms |
8508 KB |
Output is correct |
5 |
Correct |
4 ms |
8284 KB |
Output is correct |
6 |
Correct |
7 ms |
8284 KB |
Output is correct |
7 |
Correct |
4 ms |
8284 KB |
Output is correct |
8 |
Correct |
4 ms |
8284 KB |
Output is correct |
9 |
Correct |
4 ms |
8284 KB |
Output is correct |
10 |
Correct |
4 ms |
8284 KB |
Output is correct |
11 |
Correct |
4 ms |
8284 KB |
Output is correct |
12 |
Correct |
4 ms |
8284 KB |
Output is correct |
13 |
Correct |
4 ms |
8284 KB |
Output is correct |
14 |
Correct |
4 ms |
8284 KB |
Output is correct |
15 |
Correct |
4 ms |
8284 KB |
Output is correct |
16 |
Correct |
3 ms |
8464 KB |
Output is correct |
17 |
Correct |
5 ms |
8540 KB |
Output is correct |
18 |
Correct |
5 ms |
8540 KB |
Output is correct |
19 |
Correct |
4 ms |
8540 KB |
Output is correct |
20 |
Correct |
4 ms |
8284 KB |
Output is correct |
21 |
Correct |
6 ms |
8284 KB |
Output is correct |
22 |
Correct |
6 ms |
8540 KB |
Output is correct |
23 |
Correct |
5 ms |
8540 KB |
Output is correct |
24 |
Correct |
5 ms |
8424 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 |
8284 KB |
Output is correct |
28 |
Correct |
4 ms |
8544 KB |
Output is correct |
29 |
Correct |
5 ms |
8544 KB |
Output is correct |
30 |
Correct |
5 ms |
8284 KB |
Output is correct |
31 |
Correct |
4 ms |
8540 KB |
Output is correct |
32 |
Correct |
4 ms |
8540 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
8284 KB |
Output is correct |
2 |
Correct |
4 ms |
8284 KB |
Output is correct |
3 |
Correct |
4 ms |
8284 KB |
Output is correct |
4 |
Correct |
4 ms |
8508 KB |
Output is correct |
5 |
Correct |
4 ms |
8284 KB |
Output is correct |
6 |
Correct |
7 ms |
8284 KB |
Output is correct |
7 |
Correct |
4 ms |
8284 KB |
Output is correct |
8 |
Correct |
4 ms |
8284 KB |
Output is correct |
9 |
Correct |
4 ms |
8284 KB |
Output is correct |
10 |
Correct |
4 ms |
8284 KB |
Output is correct |
11 |
Correct |
4 ms |
8284 KB |
Output is correct |
12 |
Correct |
4 ms |
8284 KB |
Output is correct |
13 |
Correct |
4 ms |
8284 KB |
Output is correct |
14 |
Correct |
4 ms |
8284 KB |
Output is correct |
15 |
Correct |
4 ms |
8284 KB |
Output is correct |
16 |
Correct |
3 ms |
8464 KB |
Output is correct |
17 |
Correct |
5 ms |
8540 KB |
Output is correct |
18 |
Correct |
5 ms |
8540 KB |
Output is correct |
19 |
Correct |
4 ms |
8540 KB |
Output is correct |
20 |
Correct |
4 ms |
8284 KB |
Output is correct |
21 |
Correct |
6 ms |
8284 KB |
Output is correct |
22 |
Correct |
6 ms |
8540 KB |
Output is correct |
23 |
Correct |
5 ms |
8540 KB |
Output is correct |
24 |
Correct |
5 ms |
8424 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 |
8284 KB |
Output is correct |
28 |
Correct |
4 ms |
8544 KB |
Output is correct |
29 |
Correct |
5 ms |
8544 KB |
Output is correct |
30 |
Correct |
5 ms |
8284 KB |
Output is correct |
31 |
Correct |
4 ms |
8540 KB |
Output is correct |
32 |
Correct |
4 ms |
8540 KB |
Output is correct |
33 |
Correct |
215 ms |
14288 KB |
Output is correct |
34 |
Correct |
73 ms |
14168 KB |
Output is correct |
35 |
Correct |
210 ms |
13132 KB |
Output is correct |
36 |
Correct |
323 ms |
18072 KB |
Output is correct |
37 |
Correct |
14 ms |
11072 KB |
Output is correct |
38 |
Correct |
344 ms |
19164 KB |
Output is correct |
39 |
Correct |
335 ms |
19088 KB |
Output is correct |
40 |
Correct |
328 ms |
19012 KB |
Output is correct |
41 |
Correct |
330 ms |
19176 KB |
Output is correct |
42 |
Correct |
316 ms |
19008 KB |
Output is correct |
43 |
Correct |
315 ms |
19008 KB |
Output is correct |
44 |
Correct |
286 ms |
19004 KB |
Output is correct |
45 |
Correct |
287 ms |
19072 KB |
Output is correct |
46 |
Correct |
319 ms |
19008 KB |
Output is correct |
47 |
Correct |
339 ms |
19172 KB |
Output is correct |
48 |
Correct |
92 ms |
16024 KB |
Output is correct |
49 |
Correct |
95 ms |
17664 KB |
Output is correct |
50 |
Correct |
41 ms |
10588 KB |
Output is correct |
51 |
Correct |
48 ms |
12244 KB |
Output is correct |
52 |
Correct |
24 ms |
10336 KB |
Output is correct |
53 |
Correct |
140 ms |
17420 KB |
Output is correct |
54 |
Correct |
100 ms |
12656 KB |
Output is correct |
55 |
Correct |
262 ms |
16264 KB |
Output is correct |
56 |
Correct |
153 ms |
13556 KB |
Output is correct |
57 |
Correct |
198 ms |
17360 KB |
Output is correct |
58 |
Correct |
18 ms |
11988 KB |
Output is correct |
59 |
Correct |
40 ms |
11868 KB |
Output is correct |
60 |
Correct |
84 ms |
16720 KB |
Output is correct |
61 |
Correct |
97 ms |
17208 KB |
Output is correct |
62 |
Correct |
63 ms |
14988 KB |
Output is correct |
63 |
Correct |
32 ms |
15704 KB |
Output is correct |
64 |
Correct |
38 ms |
17240 KB |
Output is correct |
65 |
Correct |
41 ms |
22204 KB |
Output is correct |
66 |
Correct |
46 ms |
12120 KB |
Output is correct |
67 |
Correct |
52 ms |
18116 KB |
Output is correct |
68 |
Correct |
83 ms |
22612 KB |
Output is correct |
69 |
Correct |
25 ms |
10008 KB |
Output is correct |
70 |
Correct |
8 ms |
8536 KB |
Output is correct |
71 |
Correct |
43 ms |
15196 KB |
Output is correct |
72 |
Correct |
60 ms |
20568 KB |
Output is correct |
73 |
Correct |
138 ms |
25936 KB |
Output is correct |
74 |
Correct |
152 ms |
22352 KB |
Output is correct |
75 |
Correct |
119 ms |
25680 KB |
Output is correct |
76 |
Correct |
100 ms |
24404 KB |
Output is correct |
77 |
Correct |
160 ms |
22612 KB |
Output is correct |