/*Author: KawakiMeido*/
#include <bits/stdc++.h>
#define pb push_back
#define endl "\n"
#define ll long long
#define int long long
#define all(x) (x).begin(),(x).end()
#define pii pair<int,int>
#define fi first
#define se second
using namespace std;
/*Constants*/
const int N = 1e5+10;
const int INF = 1e9+7;
const long long LLINF = 1e15+3;
/*Global Variables*/
int n;
vector<int> adj[N];
int pre[N];
struct Node{
vector<int> val;
Node(){
val.resize(4,LLINF);
}
int mn(){
return min({val[0],val[1],val[2],val[3]});
}
};
Node Comb(Node x, Node y){
Node res;
if (x.val[0] == LLINF) return y;
if (y.val[0] == LLINF) return x;
for (int i=0; i<4; i++){
for (int j=0; j<4; j++){
int idx = (i&(1<<1)) + (j&1);
int val = x.val[i] + y.val[j] + (((i&1)^((j>>1)&1))?1:0);
res.val[idx] = min(res.val[idx],val);
}
}
return res;
}
struct SegTree{
int n;
vector<Node> ST;
void Build_Rec(int idx, int l, int r){
if (l==r){
ST[idx].val[0] = ST[idx].val[3] = 0;
ST[idx].val[1] = ST[idx].val[2] = INF;
return;
}
int mid = (l+r)/2;
Build_Rec(idx*2,l,mid); Build_Rec(idx*2+1,mid+1,r);
ST[idx] = Comb(ST[idx*2],ST[idx*2+1]);
}
void Build(int _n){
n = _n;
ST.resize(4*n+10);
Build_Rec(1,1,n);
}
void Update(int idx, int l, int r, int x, pii val){
if (x<l || r<x) return;
if (l==r){
ST[idx].val[0] += val.fi;
ST[idx].val[3] += val.se;
return;
}
int mid = (l+r)/2;
Update(idx*2,l,mid,x,val); Update(idx*2+1,mid+1,r,x,val);
ST[idx] = Comb(ST[idx*2],ST[idx*2+1]);
}
Node Get(int idx, int l, int r, int x, int y){
if (y<l || r<x) return Node();
if (x<=l && r<=y) return ST[idx];
int mid = (l+r)/2;
return Comb(Get(idx*2,l,mid,x,y),Get(idx*2+1,mid+1,r,x,y));
}
} ST;
//HLD
int curpos = 0;
int parent[N],sz[N],depth[N];
int root[N],pos[N];
int tin[N],tout[N];
vector<pii> residue(N,make_pair(0,0));
void DFS(int u, int p){
sz[u] = 1;
for (auto v:adj[u]){
if (v==p) continue;
parent[v] = u;
depth[v] = depth[u]+1;
DFS(v,u);
sz[u]+=sz[v];
}
}
void HLD(int u, int r){
root[u] = r;
pos[u] = ++curpos;
if (u==r) tin[r] = curpos;
int nxt = 0;
for (auto v:adj[u]){
if (v==parent[u]) continue;
if (!nxt || sz[nxt]<sz[v]) nxt = v;
}
if (nxt) HLD(nxt,r);
else tout[r] = curpos;
for (auto v:adj[u]){
if (v==parent[u] || v==nxt) continue;
HLD(v,v);
}
}
Node CBT(int x){
while (root[x]!=1){
Node tmp = ST.Get(1,1,n,tin[root[x]],tout[root[x]]);
int p = parent[root[x]];
ST.Update(1,1,n,pos[p],make_pair(-residue[root[x]].fi,-residue[root[x]].se));
residue[root[x]].fi = min({tmp.val[0],tmp.val[1],tmp.val[2]+1,tmp.val[3]+1});
residue[root[x]].se = min({tmp.val[0]+1,tmp.val[1]+1,tmp.val[2],tmp.val[3]});
ST.Update(1,1,n,pos[p],residue[root[x]]);
x = p;
}
return ST.Get(1,1,n,tin[1],tout[1]);
}
int cat(signed x){
ST.Update(1,1,n,pos[x],make_pair(0,INF));
Node tmp = CBT(x);
// cout << tmp.mn() << endl;
pre[x] = 1;
return tmp.mn();
}
int dog(signed x){
ST.Update(1,1,n,pos[x],make_pair(INF,0));
Node tmp = CBT(x);
// cout << tmp.mn() << endl;
pre[x] = 2;
return tmp.mn();
}
signed neighbor(signed x){
if (pre[x] == 1){
ST.Update(1,1,n,pos[x],make_pair(0,-INF));
}
if (pre[x] == 2){
ST.Update(1,1,n,pos[x],make_pair(-INF,0));
}
Node tmp = CBT(x);
// cout << tmp.mn() << endl;
pre[x] = 0;
return tmp.mn();
}
void initialize(signed _n, vector<signed> u, vector<signed> v){
n = _n;
for (int i=0; i<n-1; i++){
adj[u[i]].push_back(v[i]);
adj[v[i]].push_back(u[i]);
}
DFS(1,0);
HLD(1,1);
ST.Build(n);
}
//int _n,q;
//int u[N],v[N];
//
//void Solve(){
// cin >> _n;
// for (int i=0; i<_n-1; i++){
// cin >> u[i] >> v[i];
// }
// initialize(_n,u,v);
//
// cin >> q;
// while (q--){
// int t,x;
// cin >> t >> x;
// if (t==1) cout << cat(x) << endl;
// if (t==2) cout << dog(x) << endl;
// if (t==3) cout << neighbor(x) << endl;
// }
//
//}
//
//signed main(){
//// freopen("catsordogs.inp","r",stdin);
//// freopen("catsordogs.out","w",stdout);
//// ios_base::sync_with_stdio(0);
//// cin.tie(0);
//
// Solve();
//
// return 0;
//}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
10320 KB |
Output is correct |
2 |
Correct |
3 ms |
10320 KB |
Output is correct |
3 |
Correct |
2 ms |
10320 KB |
Output is correct |
4 |
Correct |
3 ms |
10320 KB |
Output is correct |
5 |
Correct |
3 ms |
10320 KB |
Output is correct |
6 |
Correct |
2 ms |
10320 KB |
Output is correct |
7 |
Correct |
3 ms |
10332 KB |
Output is correct |
8 |
Correct |
2 ms |
10408 KB |
Output is correct |
9 |
Correct |
3 ms |
10320 KB |
Output is correct |
10 |
Correct |
3 ms |
10320 KB |
Output is correct |
11 |
Correct |
2 ms |
10320 KB |
Output is correct |
12 |
Correct |
3 ms |
10320 KB |
Output is correct |
13 |
Correct |
3 ms |
10488 KB |
Output is correct |
14 |
Correct |
2 ms |
10320 KB |
Output is correct |
15 |
Correct |
2 ms |
10320 KB |
Output is correct |
16 |
Correct |
3 ms |
10488 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
10320 KB |
Output is correct |
2 |
Correct |
3 ms |
10320 KB |
Output is correct |
3 |
Correct |
2 ms |
10320 KB |
Output is correct |
4 |
Correct |
3 ms |
10320 KB |
Output is correct |
5 |
Correct |
3 ms |
10320 KB |
Output is correct |
6 |
Correct |
2 ms |
10320 KB |
Output is correct |
7 |
Correct |
3 ms |
10332 KB |
Output is correct |
8 |
Correct |
2 ms |
10408 KB |
Output is correct |
9 |
Correct |
3 ms |
10320 KB |
Output is correct |
10 |
Correct |
3 ms |
10320 KB |
Output is correct |
11 |
Correct |
2 ms |
10320 KB |
Output is correct |
12 |
Correct |
3 ms |
10320 KB |
Output is correct |
13 |
Correct |
3 ms |
10488 KB |
Output is correct |
14 |
Correct |
2 ms |
10320 KB |
Output is correct |
15 |
Correct |
2 ms |
10320 KB |
Output is correct |
16 |
Correct |
3 ms |
10488 KB |
Output is correct |
17 |
Correct |
7 ms |
10576 KB |
Output is correct |
18 |
Correct |
7 ms |
10576 KB |
Output is correct |
19 |
Correct |
8 ms |
10576 KB |
Output is correct |
20 |
Correct |
3 ms |
10488 KB |
Output is correct |
21 |
Correct |
4 ms |
10320 KB |
Output is correct |
22 |
Correct |
5 ms |
10320 KB |
Output is correct |
23 |
Correct |
8 ms |
10576 KB |
Output is correct |
24 |
Correct |
7 ms |
10576 KB |
Output is correct |
25 |
Correct |
6 ms |
10576 KB |
Output is correct |
26 |
Correct |
4 ms |
10576 KB |
Output is correct |
27 |
Correct |
3 ms |
10320 KB |
Output is correct |
28 |
Correct |
3 ms |
10576 KB |
Output is correct |
29 |
Correct |
4 ms |
10576 KB |
Output is correct |
30 |
Correct |
4 ms |
10320 KB |
Output is correct |
31 |
Correct |
3 ms |
10576 KB |
Output is correct |
32 |
Correct |
5 ms |
10320 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
10320 KB |
Output is correct |
2 |
Correct |
3 ms |
10320 KB |
Output is correct |
3 |
Correct |
2 ms |
10320 KB |
Output is correct |
4 |
Correct |
3 ms |
10320 KB |
Output is correct |
5 |
Correct |
3 ms |
10320 KB |
Output is correct |
6 |
Correct |
2 ms |
10320 KB |
Output is correct |
7 |
Correct |
3 ms |
10332 KB |
Output is correct |
8 |
Correct |
2 ms |
10408 KB |
Output is correct |
9 |
Correct |
3 ms |
10320 KB |
Output is correct |
10 |
Correct |
3 ms |
10320 KB |
Output is correct |
11 |
Correct |
2 ms |
10320 KB |
Output is correct |
12 |
Correct |
3 ms |
10320 KB |
Output is correct |
13 |
Correct |
3 ms |
10488 KB |
Output is correct |
14 |
Correct |
2 ms |
10320 KB |
Output is correct |
15 |
Correct |
2 ms |
10320 KB |
Output is correct |
16 |
Correct |
3 ms |
10488 KB |
Output is correct |
17 |
Correct |
7 ms |
10576 KB |
Output is correct |
18 |
Correct |
7 ms |
10576 KB |
Output is correct |
19 |
Correct |
8 ms |
10576 KB |
Output is correct |
20 |
Correct |
3 ms |
10488 KB |
Output is correct |
21 |
Correct |
4 ms |
10320 KB |
Output is correct |
22 |
Correct |
5 ms |
10320 KB |
Output is correct |
23 |
Correct |
8 ms |
10576 KB |
Output is correct |
24 |
Correct |
7 ms |
10576 KB |
Output is correct |
25 |
Correct |
6 ms |
10576 KB |
Output is correct |
26 |
Correct |
4 ms |
10576 KB |
Output is correct |
27 |
Correct |
3 ms |
10320 KB |
Output is correct |
28 |
Correct |
3 ms |
10576 KB |
Output is correct |
29 |
Correct |
4 ms |
10576 KB |
Output is correct |
30 |
Correct |
4 ms |
10320 KB |
Output is correct |
31 |
Correct |
3 ms |
10576 KB |
Output is correct |
32 |
Correct |
5 ms |
10320 KB |
Output is correct |
33 |
Correct |
1296 ms |
28236 KB |
Output is correct |
34 |
Correct |
328 ms |
32332 KB |
Output is correct |
35 |
Correct |
1117 ms |
22620 KB |
Output is correct |
36 |
Correct |
1888 ms |
41104 KB |
Output is correct |
37 |
Correct |
30 ms |
20816 KB |
Output is correct |
38 |
Correct |
2232 ms |
46908 KB |
Output is correct |
39 |
Correct |
2045 ms |
46848 KB |
Output is correct |
40 |
Correct |
2078 ms |
46852 KB |
Output is correct |
41 |
Correct |
2061 ms |
46844 KB |
Output is correct |
42 |
Correct |
1945 ms |
46904 KB |
Output is correct |
43 |
Correct |
1995 ms |
46852 KB |
Output is correct |
44 |
Correct |
1845 ms |
46856 KB |
Output is correct |
45 |
Correct |
1953 ms |
46860 KB |
Output is correct |
46 |
Correct |
1930 ms |
46848 KB |
Output is correct |
47 |
Correct |
2106 ms |
46840 KB |
Output is correct |
48 |
Correct |
468 ms |
36492 KB |
Output is correct |
49 |
Correct |
525 ms |
43452 KB |
Output is correct |
50 |
Correct |
198 ms |
17052 KB |
Output is correct |
51 |
Correct |
220 ms |
23240 KB |
Output is correct |
52 |
Correct |
83 ms |
17100 KB |
Output is correct |
53 |
Correct |
791 ms |
45372 KB |
Output is correct |
54 |
Correct |
699 ms |
25424 KB |
Output is correct |
55 |
Correct |
1758 ms |
35264 KB |
Output is correct |
56 |
Correct |
1074 ms |
26864 KB |
Output is correct |
57 |
Correct |
1323 ms |
42444 KB |
Output is correct |
58 |
Correct |
62 ms |
25032 KB |
Output is correct |
59 |
Correct |
184 ms |
22116 KB |
Output is correct |
60 |
Correct |
450 ms |
39612 KB |
Output is correct |
61 |
Correct |
446 ms |
40892 KB |
Output is correct |
62 |
Correct |
273 ms |
35268 KB |
Output is correct |
63 |
Correct |
95 ms |
31692 KB |
Output is correct |
64 |
Correct |
91 ms |
34384 KB |
Output is correct |
65 |
Correct |
94 ms |
49656 KB |
Output is correct |
66 |
Correct |
178 ms |
19528 KB |
Output is correct |
67 |
Correct |
142 ms |
37704 KB |
Output is correct |
68 |
Correct |
311 ms |
50388 KB |
Output is correct |
69 |
Correct |
65 ms |
13384 KB |
Output is correct |
70 |
Correct |
16 ms |
10832 KB |
Output is correct |
71 |
Correct |
119 ms |
27368 KB |
Output is correct |
72 |
Correct |
178 ms |
41544 KB |
Output is correct |
73 |
Correct |
626 ms |
53636 KB |
Output is correct |
74 |
Correct |
714 ms |
50004 KB |
Output is correct |
75 |
Correct |
336 ms |
53576 KB |
Output is correct |
76 |
Correct |
320 ms |
52320 KB |
Output is correct |
77 |
Correct |
745 ms |
50504 KB |
Output is correct |