-
Notifications
You must be signed in to change notification settings - Fork 0
/
BinarySearchTree.java
38 lines (34 loc) · 1.03 KB
/
BinarySearchTree.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
import java.util.*;
import java.util.Map.Entry;
import java.util.regex.*;
import java.util.stream.*;
import java.io.*;
import java.math.*;
import java.text.*;
/*
* @author Theodoric Keith Lim
*/
class BinarySearchTree {
public static void main(String[] args) {
Kattio sc = new Kattio(System.in);
StringBuilder sb = new StringBuilder();
int n = sc.getInt();
int[] counts = new int[300000 + 1];
long currCount = 0;
TreeSet<Integer> set = new TreeSet<>();
while(n-- > 0) {
int x = sc.getInt();
set.add(x);
Integer left = set.lower(x);
Integer right = set.higher(x);
int count = 0;
count = left != null ? Math.max(count, counts[left]) : count;
count = right != null ? Math.max(count, counts[right]) : count;
currCount += count;
counts[x] = ++count;
sb.append(currCount).append("\n");
}
System.out.print(sb.toString());
sc.close();
}
}