-
Notifications
You must be signed in to change notification settings - Fork 0
/
HLD.java
249 lines (210 loc) · 4.93 KB
/
HLD.java
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
package data_structures;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;
public class HLD {
//1.Take input and processing(precomputation + query)
static int N, nodeVal[]; //input
static ArrayList<Integer>[] adjList; //input
@SuppressWarnings("unchecked")
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
N = sc.nextInt();
int E = sc.nextInt();
nodeVal = new int[N];
for(int i = 0; i < N; ++i)
nodeVal[i] = sc.nextInt();
adjList = new ArrayList[N];
for(int i = 0; i < N; ++i)
adjList[i] = new ArrayList<Integer>();
while(E-->0)
{
int u = sc.nextInt(), v = sc.nextInt();
adjList[u].add(v);
adjList[v].add(u);
}
go(0);
int q = sc.nextInt();
while(q-->0)
{
int comm = sc.nextInt();
if(comm == 1)
{
int node = sc.nextInt(), val = sc.nextInt();
update(node, val);
}
else //comm == 2
{
int a = sc.nextInt(), b = sc.nextInt();
System.out.println(query(a, b));
}
}
sc.close();
}
//3.Query
static void update(int node, int val)
{
st.update_point(segIdx[node], val);
nodeVal[node] = val;
}
static int query(int u, int v)
{
int lca = lca_query(u, v);
return query_up(u, lca) + query_up(v, lca) - nodeVal[lca];
}
static int query_up(int v, int u)
{
int uChain = chainIdx[u], vChain = chainIdx[v], ans = 0;
while(uChain != vChain)
{
ans += st.query(segIdx[chainHead[vChain]], segIdx[v]);
v = P[chainHead[vChain]][0];
vChain = chainIdx[v];
}
ans += st.query(segIdx[u], segIdx[v]);
return ans;
}
//1.Precomputation
static SegmentTree st;
static void go(int root)
{
//prepare for LCA (part 1)
int k = (int)(Math.floor(Math.log(N)/Math.log(2))) + 1;
P = new int[N][k];
for(int i = 0; i < N; i++)
Arrays.fill(P[i], -1);
//BuildTree: assign depth, parent, subtree size
subSize = new int[N];
level = new int[N];
P[root][0] = -1;
dfs(root, -1, 0);
//prepare for LCA (part 2)
for(int j = 1; j < k; j++)
for(int i = 1; i < N; i++)
if(P[i][j-1] != -1)
P[i][j] = P[P[i][j-1]][j-1];
//HL decompose
chainNo = 0; sIdx = 0;
chainHead = new int[N];
chainPos = new int[N];
chainIdx = new int[N];
chainSize = new int[N];
segIdx = new int[N];
Arrays.fill(chainHead, -1);
hld(root);
//create segment tree
int n = (int) Math.pow(2, Math.ceil(Math.log(N)/Math.log(2)));
int[] in = new int[n+1];
for(int i = 0; i < N; i++)
in[segIdx[i]] = nodeVal[i];
st = new SegmentTree(in);
}
static int[][] P;
static int[] subSize, level;
static void dfs(int u, int parent, int depth)
{
level[u] = depth;
subSize[u] = 1;
for(int v : adjList[u])
if(v != parent)
{
P[v][0] = u;
dfs(v, u, depth + 1);
subSize[u] += subSize[v];
}
}
static int chainNo, sIdx;
static int[] chainHead, chainPos, chainIdx, chainSize, segIdx;
static void hld(int cur)
{
if(chainHead[chainNo] == -1)
chainHead[chainNo] = cur;
chainIdx[cur] = chainNo;
chainPos[cur] = chainSize[chainNo]++;
segIdx[cur] = ++sIdx;
int nxt = -1, maxSize = -1;
for(int v : adjList[cur])
if(P[cur][0] != v && subSize[v] > maxSize)
{
maxSize = subSize[v];
nxt = v;
}
if(nxt != -1)
hld(nxt);
for(int v : adjList[cur])
if(P[cur][0] != v && v != nxt)
{
chainNo++;
hld(v);
}
}
static int lca_query(int p, int q)
{
int tmp, log, i;
//if p is situated on a higher level than q then we swap them
if (level[p] < level[q])
{
tmp = p; p = q; q = tmp;
}
//we compute the value of [log(level[p)]
for (log = 1; 1 << log <= level[p]; log++);
log--;
//we find the ancestor of node p situated on the same level
//with q using the values in P
for (i = log; i >= 0; i--)
if (level[p] - (1 << i) >= level[q])
p = P[p][i];
if (p == q)
return p;
//we compute LCA(p, q) using the values in P
for (i = log; i >= 0; i--)
if (P[p][i] != -1 && P[p][i] != P[q][i])
{
p = P[p][i]; q = P[q][i];
}
return P[p][0];
}
static class SegmentTree {
int N;
int[] array, sTree;
SegmentTree(int[] in)
{
array = in; N = in.length - 1;
sTree = new int[N<<1];
build(1,1,N);
}
void build(int node, int b, int e)
{
if(b == e)
sTree[node] = array[b];
else
{
build(node<<1,b,(b+e)/2);
build((node<<1)+1,(b+e)/2+1,e);
sTree[node] = sTree[node<<1]+sTree[(node<<1)+1];
}
}
void update_point(int index, int val)
{
index += N - 1;
sTree[index] = val;
while(index>1)
{
index >>= 1;
sTree[index] = sTree[index<<1] + sTree[(index<<1) + 1];
}
}
int query(int i, int j)
{
return query(1,1,N,i,j);
}
int query(int node, int b, int e, int i, int j)
{
if(i>e || j <b)
return 0;
if(b>= i && e <= j)
return sTree[node];
return query(node<<1,b,(b+e)/2,i,j) + query((node<<1)+1,(b+e)/2+1,e,i,j);
}
}
}