-
Notifications
You must be signed in to change notification settings - Fork 110
/
solution.java
126 lines (122 loc) · 4.71 KB
/
solution.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
import java.io.*;
import java.math.*;
import java.text.*;
import java.util.*;
import java.util.regex.*;
public class Solution {
//Method to find parent of the current node
static int find(int node, int[] parent) {
while(parent[node]>=0)
node=parent[node];
return node;
}
// Method to find the lower bound for the target node
static int lower_bound(int target, long[][] dp) {
// Set left to zero and right to the length of dp
int left=0;
int right=dp.length-1;
// Reach the half with value closer to the target
while(left<=right) {
int middle=(left+right)>>1;
if(dp[middle][0]>=target)
right=middle-1;
else
left=middle+1;
}
//Return the lower bound of the half containing the target value
return left;
}
static long[] solve(int[][] tree, int[][] queries) {
// cost value from low to high
Arrays.sort(tree, new Comparator<int[]>(){
public int compare(int[] a, int[] b) {
return a[2]-b[2];
}
});
TreeMap<Integer, Long> map=new TreeMap<Integer, Long>();
// Create a parent array to keep record of parent of each node
int[] parent=new int[tree.length+2];
// Initially set all the elements as -1
Arrays.fill(parent, -1);
//Traverse through the nodes and call the find function for each node
for(int i=0; i<tree.length; i++) {
int u=find(tree[i][0], parent);
int v=find(tree[i][1], parent);
int cost=tree[i][2];
long number1=-parent[u];
long number2=-parent[v];
//Find the possible cost
map.put(cost, map.getOrDefault(cost, new Long(0))+number1*number2);
if(parent[u]>parent[v]) {
parent[v]+=parent[u];
parent[u]=v;
}
else{
parent[u]+=parent[v];
parent[v]=u;
}
}
//Set total sum as zero
long totalSum=0;
int index=1;
//Create a dp for the cost
long[][] dp=new long[map.size()+1][2];
dp[0][0]=dp[0][1]=0;
for (Map.Entry<Integer, Long> entry : map.entrySet()) {
totalSum+=entry.getValue();
dp[index][0]=entry.getKey();
dp[index][1]=totalSum;
++index;
}
//Run the queries and extract the values from dp
long[] result=new long[queries.length];
long max=dp[dp.length-1][0];
for(int i=0; i<queries.length; i++) {
int left=lower_bound(queries[i][0], dp)-1;
int right=lower_bound(queries[i][1], dp);
if(right>=dp.length)
--right;
if(dp[right][0]>queries[i][1])
--right;
result[i]=dp[right][1]-dp[left][1];
}
//Finally return the result array
return result;
}
/* Driver Code */
private static final Scanner scanner = new Scanner(System.in);
public static void main(String[] args) throws IOException {
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));
String[] nq = scanner.nextLine().split(" ");
int n = Integer.parseInt(nq[0]);
int q = Integer.parseInt(nq[1]);
int[][] tree = new int[n-1][3];
for (int treeRowItr = 0; treeRowItr < n-1; treeRowItr++) {
String[] treeRowItems = scanner.nextLine().split(" ");
scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
for (int treeColumnItr = 0; treeColumnItr < 3; treeColumnItr++) {
int treeItem = Integer.parseInt(treeRowItems[treeColumnItr]);
tree[treeRowItr][treeColumnItr] = treeItem;
}
}
int[][] queries = new int[q][2];
for (int queriesRowItr = 0; queriesRowItr < q; queriesRowItr++) {
String[] queriesRowItems = scanner.nextLine().split(" ");
scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
for (int queriesColumnItr = 0; queriesColumnItr < 2; queriesColumnItr++) {
int queriesItem = Integer.parseInt(queriesRowItems[queriesColumnItr]);
queries[queriesRowItr][queriesColumnItr] = queriesItem;
}
}
long[] result = solve(tree, queries);
for (int resultItr = 0; resultItr < result.length; resultItr++) {
bufferedWriter.write(String.valueOf(result[resultItr]));
if (resultItr != result.length - 1) {
bufferedWriter.write("\n");
}
}
bufferedWriter.newLine();
bufferedWriter.close();
scanner.close();
}
}