#include "catdog.h"
typedef long long ll;
ll pie(ll army){return (1ll<<army);}
#include <bits/stdc++.h>
#define fr first
#define sc second
#define pb push_back
#define endl '\n'
#define mid ((left+right)>>1)
const ll inf=2000000000000000005;
const int sonsuz=100000005;
using namespace std;
ll fpow(ll x,ll y,ll m=0){if(y<0){cout<<"powError";return -1;}if(m)x%=m;ll res=1;while(y>0){if(y&1)res*=x;x*=x;if(m){x%=m;res%=m;}y>>=1;}return res;}
array<array<int,2>,2> comb(array<array<int,2>,2> a,array<array<int,2>,2> b){
array<array<int,2>,2> res;
for(int i=0;i<2;i++){
for(int j=0;j<2;j++){
res[i][j]=min(min(a[i][0]+b[0][j],a[i][0]+b[1][j]+1),min(a[i][1]+b[0][j]+1,a[i][1]+b[1][j]));
}
}
return res;
}
struct Seg{
int n;
vector<array<array<int,2>,2>>tree;
void build(int node=1,int left=0,int right=-1){
if(right==-1)right=n-1;
if(left==right){
tree[node]={array<int,2>{0,sonsuz*2},array<int,2>{sonsuz*2,0}};
return;
}
build(node*2,left,mid);
build(node*2+1,mid+1,right);
tree[node]=comb(tree[node*2],tree[node*2+1]);
}
void init(int N){
n=N;
tree.resize(n<<2, array<array<int,2>,2>{array<int,2>{0,0},array<int,2>{0,0}});
build();
}
int l,r,c;
void up(int node=1,int left=0,int right=-1){
if(right==-1)right=n-1;
if(left==right){
tree[node][r][r]+=c;
return;
}
if(l>mid)up(node*2+1,mid+1,right);
else up(node*2,left,mid);
tree[node]=comb(tree[node*2],tree[node*2+1]);
}
void update(int tar,int i,int val){
l=tar;
r=i;
c=val;
up();
}
array<array<int,2>,2> qu(int node=1,int left=0,int right=-1){
if(right==-1)right=n-1;
if(left>r||right<l)return array<array<int,2>,2>{array<int,2>{0,2*sonsuz},array<int,2>{2*sonsuz,0}};
if(left>=l&&right<=r)return tree[node];
return comb(qu(node*2,left,mid),qu(node*2+1,mid+1,right));
}
array<array<int,2>,2> query(int lef,int rig){
l=lef;
r=rig;
return qu();
}
};
int n;
int tim=0;
int tip[100001],par[100001],pre[100001],sub[100001],bas[100001],bottom[100001];
Seg seg;
vector<int>komsu[100001];
void dfs1(int pos){
sub[pos]=1;
for(int x:komsu[pos]){
if(x==pre[pos])continue;
pre[x]=pos;
dfs1(x);
sub[pos]+=sub[x];
}
}
void dfs2(int pos){
int mx=-1;
bottom[par[pos]]=pos;
bas[pos]=tim++;
for(int x:komsu[pos]){
if(x==pre[pos])continue;
if(mx==-1||sub[mx]<sub[x])mx=x;
}
if(mx!=-1){
par[mx]=par[pos];
dfs2(mx);
for(int x:komsu[pos]){
if(x==mx||x==pre[pos])continue;
par[x]=x;
dfs2(x);
}
}
}
void initialize(int N,vector<int> A,vector<int> B){
n=N;
for(int i=0;i<n-1;i++){
komsu[A[i]].pb(B[i]);
komsu[B[i]].pb(A[i]);
}
par[1]=1;
dfs1(1);
dfs2(1);
seg.init(tim);
}
void sil(int loc){
if(pre[par[loc]]==0)return;
sil(pre[par[loc]]);
array<array<int,2>,2>sum=seg.query(bas[par[loc]],bas[bottom[par[loc]]]);
seg.update(bas[pre[par[loc]]],0,-min(min(sum[0][0],sum[0][1]),min(sum[1][0],sum[1][1])+1));
seg.update(bas[pre[par[loc]]],1,-min(min(sum[0][0],sum[0][1])+1,min(sum[1][0],sum[1][1])));
}
void op(int loc){
array<array<int,2>,2>sum={array<int,2>{0,0},array<int,2>{0,0}};
while(loc){
seg.update(bas[loc],0,min(min(sum[0][0],sum[0][1]),min(sum[1][0],sum[1][1])+1));
seg.update(bas[loc],1,min(min(sum[0][0],sum[0][1])+1,min(sum[1][0],sum[1][1])));
sum=seg.query(bas[par[loc]],bas[bottom[par[loc]]]);
loc=pre[par[loc]];
}
}
int cat(int v){
sil(v);
seg.update(bas[v],0,sonsuz);
op(v);
tip[v]=1;
array<array<int,2>,2>res=seg.query(bas[1],bas[bottom[1]]);
return min(min(res[0][0],res[0][1]),min(res[1][0],res[1][1]));
}
int dog(int v){
sil(v);
seg.update(bas[v],1,sonsuz);
op(v);
tip[v]=2;
array<array<int,2>,2>res=seg.query(bas[1],bas[bottom[1]]);
return min(min(res[0][0],res[0][1]),min(res[1][0],res[1][1]));
}
int neighbor(int v){
sil(v);
seg.update(bas[v],tip[v]-1,-sonsuz);
op(v);
tip[v]=0;
array<array<int,2>,2>res=seg.query(bas[1],bas[bottom[1]]);
return min(min(res[0][0],res[0][1]),min(res[1][0],res[1][1]));
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4688 KB |
Output is correct |
2 |
Correct |
2 ms |
4688 KB |
Output is correct |
3 |
Correct |
2 ms |
4688 KB |
Output is correct |
4 |
Correct |
2 ms |
4688 KB |
Output is correct |
5 |
Correct |
2 ms |
4688 KB |
Output is correct |
6 |
Correct |
2 ms |
4688 KB |
Output is correct |
7 |
Correct |
2 ms |
4688 KB |
Output is correct |
8 |
Correct |
2 ms |
4700 KB |
Output is correct |
9 |
Correct |
2 ms |
4688 KB |
Output is correct |
10 |
Correct |
1 ms |
4688 KB |
Output is correct |
11 |
Correct |
2 ms |
4688 KB |
Output is correct |
12 |
Correct |
2 ms |
4688 KB |
Output is correct |
13 |
Correct |
2 ms |
4688 KB |
Output is correct |
14 |
Correct |
2 ms |
4704 KB |
Output is correct |
15 |
Correct |
2 ms |
4688 KB |
Output is correct |
16 |
Correct |
1 ms |
4688 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4688 KB |
Output is correct |
2 |
Correct |
2 ms |
4688 KB |
Output is correct |
3 |
Correct |
2 ms |
4688 KB |
Output is correct |
4 |
Correct |
2 ms |
4688 KB |
Output is correct |
5 |
Correct |
2 ms |
4688 KB |
Output is correct |
6 |
Correct |
2 ms |
4688 KB |
Output is correct |
7 |
Correct |
2 ms |
4688 KB |
Output is correct |
8 |
Correct |
2 ms |
4700 KB |
Output is correct |
9 |
Correct |
2 ms |
4688 KB |
Output is correct |
10 |
Correct |
1 ms |
4688 KB |
Output is correct |
11 |
Correct |
2 ms |
4688 KB |
Output is correct |
12 |
Correct |
2 ms |
4688 KB |
Output is correct |
13 |
Correct |
2 ms |
4688 KB |
Output is correct |
14 |
Correct |
2 ms |
4704 KB |
Output is correct |
15 |
Correct |
2 ms |
4688 KB |
Output is correct |
16 |
Correct |
1 ms |
4688 KB |
Output is correct |
17 |
Correct |
5 ms |
4688 KB |
Output is correct |
18 |
Correct |
5 ms |
4828 KB |
Output is correct |
19 |
Correct |
4 ms |
4856 KB |
Output is correct |
20 |
Correct |
1 ms |
4688 KB |
Output is correct |
21 |
Correct |
3 ms |
4708 KB |
Output is correct |
22 |
Correct |
3 ms |
4708 KB |
Output is correct |
23 |
Correct |
7 ms |
4708 KB |
Output is correct |
24 |
Correct |
5 ms |
4716 KB |
Output is correct |
25 |
Correct |
4 ms |
4688 KB |
Output is correct |
26 |
Correct |
3 ms |
4688 KB |
Output is correct |
27 |
Correct |
2 ms |
4688 KB |
Output is correct |
28 |
Correct |
2 ms |
4688 KB |
Output is correct |
29 |
Correct |
3 ms |
4688 KB |
Output is correct |
30 |
Correct |
3 ms |
4688 KB |
Output is correct |
31 |
Correct |
2 ms |
4856 KB |
Output is correct |
32 |
Correct |
3 ms |
4688 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4688 KB |
Output is correct |
2 |
Correct |
2 ms |
4688 KB |
Output is correct |
3 |
Correct |
2 ms |
4688 KB |
Output is correct |
4 |
Correct |
2 ms |
4688 KB |
Output is correct |
5 |
Correct |
2 ms |
4688 KB |
Output is correct |
6 |
Correct |
2 ms |
4688 KB |
Output is correct |
7 |
Correct |
2 ms |
4688 KB |
Output is correct |
8 |
Correct |
2 ms |
4700 KB |
Output is correct |
9 |
Correct |
2 ms |
4688 KB |
Output is correct |
10 |
Correct |
1 ms |
4688 KB |
Output is correct |
11 |
Correct |
2 ms |
4688 KB |
Output is correct |
12 |
Correct |
2 ms |
4688 KB |
Output is correct |
13 |
Correct |
2 ms |
4688 KB |
Output is correct |
14 |
Correct |
2 ms |
4704 KB |
Output is correct |
15 |
Correct |
2 ms |
4688 KB |
Output is correct |
16 |
Correct |
1 ms |
4688 KB |
Output is correct |
17 |
Correct |
5 ms |
4688 KB |
Output is correct |
18 |
Correct |
5 ms |
4828 KB |
Output is correct |
19 |
Correct |
4 ms |
4856 KB |
Output is correct |
20 |
Correct |
1 ms |
4688 KB |
Output is correct |
21 |
Correct |
3 ms |
4708 KB |
Output is correct |
22 |
Correct |
3 ms |
4708 KB |
Output is correct |
23 |
Correct |
7 ms |
4708 KB |
Output is correct |
24 |
Correct |
5 ms |
4716 KB |
Output is correct |
25 |
Correct |
4 ms |
4688 KB |
Output is correct |
26 |
Correct |
3 ms |
4688 KB |
Output is correct |
27 |
Correct |
2 ms |
4688 KB |
Output is correct |
28 |
Correct |
2 ms |
4688 KB |
Output is correct |
29 |
Correct |
3 ms |
4688 KB |
Output is correct |
30 |
Correct |
3 ms |
4688 KB |
Output is correct |
31 |
Correct |
2 ms |
4856 KB |
Output is correct |
32 |
Correct |
3 ms |
4688 KB |
Output is correct |
33 |
Correct |
740 ms |
12216 KB |
Output is correct |
34 |
Correct |
200 ms |
12624 KB |
Output is correct |
35 |
Correct |
691 ms |
10436 KB |
Output is correct |
36 |
Correct |
1144 ms |
17560 KB |
Output is correct |
37 |
Correct |
18 ms |
8528 KB |
Output is correct |
38 |
Correct |
1214 ms |
19028 KB |
Output is correct |
39 |
Correct |
1335 ms |
18984 KB |
Output is correct |
40 |
Correct |
1267 ms |
19000 KB |
Output is correct |
41 |
Correct |
1332 ms |
18984 KB |
Output is correct |
42 |
Correct |
1179 ms |
18992 KB |
Output is correct |
43 |
Correct |
1198 ms |
18984 KB |
Output is correct |
44 |
Correct |
1149 ms |
19000 KB |
Output is correct |
45 |
Correct |
1140 ms |
18996 KB |
Output is correct |
46 |
Correct |
1229 ms |
18980 KB |
Output is correct |
47 |
Correct |
1313 ms |
18976 KB |
Output is correct |
48 |
Correct |
317 ms |
14916 KB |
Output is correct |
49 |
Correct |
323 ms |
17392 KB |
Output is correct |
50 |
Correct |
127 ms |
7496 KB |
Output is correct |
51 |
Correct |
138 ms |
9692 KB |
Output is correct |
52 |
Correct |
50 ms |
7248 KB |
Output is correct |
53 |
Correct |
456 ms |
17224 KB |
Output is correct |
54 |
Correct |
374 ms |
10320 KB |
Output is correct |
55 |
Correct |
1014 ms |
14772 KB |
Output is correct |
56 |
Correct |
612 ms |
11368 KB |
Output is correct |
57 |
Correct |
861 ms |
16776 KB |
Output is correct |
58 |
Correct |
32 ms |
9932 KB |
Output is correct |
59 |
Correct |
110 ms |
9256 KB |
Output is correct |
60 |
Correct |
245 ms |
15944 KB |
Output is correct |
61 |
Correct |
280 ms |
16436 KB |
Output is correct |
62 |
Correct |
146 ms |
14024 KB |
Output is correct |
63 |
Correct |
59 ms |
13904 KB |
Output is correct |
64 |
Correct |
60 ms |
15688 KB |
Output is correct |
65 |
Correct |
59 ms |
22344 KB |
Output is correct |
66 |
Correct |
123 ms |
9200 KB |
Output is correct |
67 |
Correct |
87 ms |
16968 KB |
Output is correct |
68 |
Correct |
209 ms |
22736 KB |
Output is correct |
69 |
Correct |
50 ms |
6472 KB |
Output is correct |
70 |
Correct |
12 ms |
4944 KB |
Output is correct |
71 |
Correct |
94 ms |
12904 KB |
Output is correct |
72 |
Correct |
119 ms |
19844 KB |
Output is correct |
73 |
Correct |
398 ms |
25928 KB |
Output is correct |
74 |
Correct |
449 ms |
22344 KB |
Output is correct |
75 |
Correct |
223 ms |
25928 KB |
Output is correct |
76 |
Correct |
216 ms |
24660 KB |
Output is correct |
77 |
Correct |
442 ms |
22856 KB |
Output is correct |