@@ -27,6 +27,9 @@ public class HeavyLightDecomposition {
2727 @ SuppressWarnings ("unchecked" )
2828 public HeavyLightDecomposition (int n ) {
2929 tree = new ArrayList [n + 1 ];
30+ for (int i = 0 ; i <= n ; i ++) {
31+ tree [i ] = new ArrayList <>();
32+ }
3033 parent = new int [n + 1 ];
3134 depth = new int [n + 1 ];
3235 subtreeSize = new int [n + 1 ];
@@ -35,7 +38,6 @@ public HeavyLightDecomposition(int n) {
3538 nodeValue = new int [n + 1 ];
3639 segmentTree = new int [4 * (n + 1 )];
3740 for (int i = 0 ; i <= n ; i ++) {
38- tree [i ] = new ArrayList <>();
3941 chainHead [i ] = -1 ;
4042 }
4143 positionIndex = 0 ;
@@ -89,7 +91,7 @@ private void decompose(int node, int head) {
8991
9092 private void buildSegmentTree (int node , int start , int end ) {
9193 if (start == end ) {
92- segmentTree [node ] = nodeValue [start ];
94+ segmentTree [node ] = nodeValue [start ];
9395 return ;
9496 }
9597 int mid = (start + end ) / 2 ;
@@ -149,7 +151,9 @@ public void initialize(int root, int[] values) {
149151 dfsSize (root , -1 );
150152 decompose (root , root );
151153 for (int i = 0 ; i < values .length ; i ++) {
152- nodeValue [position [i ]] = values [i ];
154+ if (i < positionIndex ) {
155+ nodeValue [position [i ]] = values [i ];
156+ }
153157 }
154158 buildSegmentTree (1 , 0 , positionIndex - 1 );
155159 }
0 commit comments