forked from Ashishgup1/Competitive-Coding
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Heavy Light Decomposition (HLD).cpp
218 lines (193 loc) · 3.45 KB
/
Heavy Light Decomposition (HLD).cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
const int N = 1e5+5;
const int LG = log2(N) + 1;
int n, tim=0;
int a[N], level[N], tin[N], tout[N], rtin[N], nxt[N], subtree[N], parent[LG][N];
vector<int> g[N];
//Heavy Light Decomposition
void dfs(int u, int par, int lvl)
{
parent[0][u] = par;
level[u] = lvl;
for(auto &it:g[u])
{
if(it==par)
continue;
dfs(it, u, lvl+1);
}
}
void dfs1(int u, int par)
{
subtree[u] = 1;
for(auto &it:g[u])
{
if(it==par)
continue;
dfs1(it, u);
subtree[u] += subtree[it];
if(subtree[it] > subtree[g[u][0]])
swap(it, g[u][0]);
}
}
void dfs_hld(int u, int par)
{
tin[u] = ++tim;
rtin[tim] = u;
for(auto &v:g[u])
{
if(v==par)
continue;
nxt[v] = (v==g[u][0] ? nxt[u]:v);
dfs_hld(v, u);
}
tout[u] = tim;
}
//LCA
int walk(int u, int h)
{
for(int i=LG-1;i>=0;i--)
{
if((h>>i) & 1)
u = parent[i][u];
}
return u;
}
void precompute()
{
for(int i=1;i<LG;i++)
for(int j=1;j<=n;j++)
if(parent[i-1][j])
parent[i][j]=parent[i-1][parent[i-1][j]];
}
int LCA(int u, int v)
{
if(level[u]<level[v])
swap(u,v);
int diff=level[u]-level[v];
for(int i=LG-1;i>=0;i--)
{
if((1<<i) & diff)
{
u=parent[i][u];
}
}
if(u==v)
return u;
for(int i=LG-1;i>=0;i--)
{
if(parent[i][u] && parent[i][u]!=parent[i][v])
{
u=parent[i][u];
v=parent[i][v];
}
}
return parent[0][u];
}
int dist(int u, int v)
{
return level[u] + level[v] - 2 * level[LCA(u, v)];
}
//Segment Tree
int st[4*N], lazy[4*N];
void build(int node, int L, int R)
{
if(L==R)
{
st[node]=a[rtin[L]];
return;
}
int M=(L+R)/2;
build(node*2, L, M);
build(node*2+1, M+1, R);
st[node]=min(st[node*2], st[node*2+1]);
}
void propagate(int node, int L, int R)
{
if(L!=R)
{
lazy[node*2]+=lazy[node];
lazy[node*2+1]+=lazy[node];
}
st[node]+=lazy[node];
lazy[node]=0;
}
int query(int node, int L, int R, int i, int j)
{
if(lazy[node])
propagate(node, L, R);
if(j<L || i>R)
return 1e9;
if(i<=L && R<=j)
return st[node];
int M=(L+R)/2;
int left=query(node*2, L, M, i, j);
int right=query(node*2 + 1, M+1, R, i, j);
return min(left, right);
}
void update(int node, int L, int R, int i, int j, int val)
{
if(lazy[node])
propagate(node, L, R);
if(j<L || i>R)
return;
if(i<=L && R<=j)
{
lazy[node]+=val;
propagate(node, L, R);
return;
}
int M=(L+R)/2;
update(node*2, L, M, i, j, val);
update(node*2 + 1, M+1, R, i, j, val);
st[node]=min(st[node*2], st[node*2 + 1]);
}
void upd(int l, int r, int val)
{
update(1, 1, n, l, r, val);
}
int get(int l, int r)
{
return query(1, 1, n, l, r);
}
//Utility Functions
int query_up(int x, int y) //Assuming Y is an ancestor of X
{
int res = 0;
while(nxt[x] != nxt[y])
{
res += get(tin[nxt[x]], tin[x]);
x = parent[0][nxt[x]];
}
res += get(tin[y] + 1, tin[x]); //use tin[y] to include Y
return res;
}
int query_hld(int x, int y)
{
int lca = LCA(x, y);
int res = query_up(x, lca) + query_up(y, lca);
return res;
}
void update_up(int x, int y, int val) //Assuming Y is an ancestor of X
{
while(nxt[x] != nxt[y])
{
upd(tin[nxt[x]], tin[x], val);
x = parent[0][nxt[x]];
}
upd(tin[y] + 1, tin[x], val); //use tin[y] to include Y
}
void update_hld(int x, int y, int val)
{
int lca = LCA(x, y);
update_up(x, lca, val);
update_up(y, lca, val);
}
void hld()
{
dfs(1, 0, 1);
dfs1(1, 0);
dfs_hld(1, 0);
precompute();
build(1, 1, n);
}
//Problem 1 (Maximum Climb for nodes with same value): https://www.codechef.com/CONA2019/problems/COMA19E
//Solution 1: https://www.codechef.com/viewsolution/23271645