-
Notifications
You must be signed in to change notification settings - Fork 80
/
TripleSum.java
89 lines (66 loc) · 2.36 KB
/
TripleSum.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
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;
public class Solution {
// Complete the triplets function below.
static long triplets(int[] a, int[] b, int[] c) {
int[] ar = Arrays.stream(a).sorted().distinct().toArray();
int[] br = Arrays.stream(b).sorted().distinct().toArray();
int[] cr = Arrays.stream(c).sorted().distinct().toArray();
long left = 0;
long right = 0;
int l = 0;
int r = 0;
long sum = 0;
for(int i=0; i<br.length; i++) {
while(l<ar.length&&ar[l]<=br[i]) {
left++;
l++;
}
while(r<cr.length&&cr[r]<=br[i]) {
right++;
r++;
}
sum += left*right;
}
return sum;
}
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[] lenaLenbLenc = scanner.nextLine().split(" ");
int lena = Integer.parseInt(lenaLenbLenc[0]);
int lenb = Integer.parseInt(lenaLenbLenc[1]);
int lenc = Integer.parseInt(lenaLenbLenc[2]);
int[] arra = new int[lena];
String[] arraItems = scanner.nextLine().split(" ");
scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
for (int i = 0; i < lena; i++) {
int arraItem = Integer.parseInt(arraItems[i]);
arra[i] = arraItem;
}
int[] arrb = new int[lenb];
String[] arrbItems = scanner.nextLine().split(" ");
scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
for (int i = 0; i < lenb; i++) {
int arrbItem = Integer.parseInt(arrbItems[i]);
arrb[i] = arrbItem;
}
int[] arrc = new int[lenc];
String[] arrcItems = scanner.nextLine().split(" ");
scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
for (int i = 0; i < lenc; i++) {
int arrcItem = Integer.parseInt(arrcItems[i]);
arrc[i] = arrcItem;
}
long ans = triplets(arra, arrb, arrc);
bufferedWriter.write(String.valueOf(ans));
bufferedWriter.newLine();
bufferedWriter.close();
scanner.close();
}
}