Java 手打线段树,不是通用模板,后续写成模板吧。

代码是leetcode 218. The Skyline Problem.

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
class Node {
Node left;
Node right;
long l;
long r;
int flag = 0;
int value = 0;

Node(int x, long l, long r) {
this.value = x;
this.l = l;
this.r = r;
}

void makeChildren() {
if (this.l == this.r){
return;
}
long m = (this.l + this.r)/2;
if (this.left == null && this.l != this.r) {
this.left = new Node(0, this.l, m);
}
if (this.right == null && this.l != this.r) {
this.right = new Node(0,m+1, this.r);
}
}

int get(int l, int r) {
if (l < 0)
return 0;

//System.out.println(this.toString());
if(l <= this.l && r >= this.r) {
return this.value;
}
long m = (this.l + this.r) / 2;
makeChildren();
if (this.flag > 0) {
this.left.value = Math.max(this.left.value, this.flag);
this.right.value = Math.max(this.right.value, this.flag);
this.left.flag = Math.max(this.left.flag, this.flag);
this.right.flag = Math.max(this.right.flag, this.flag);
this.flag = 0;
}
int max = 0;
if (l <= m){
//System.out.println(this.toString());
max = Math.max(max, this.left.get(l, r));
}
if (r > m){
max = Math.max(max, this.right.get(l, r));
}
return max;
}

void update(long l, long r, int value) {
//System.out.println("update:" + this.toString());
if (l <= this.l && r >= this.r) {
this.value = Math.max(this.value, value);
this.flag = Math.max(this.flag, value);
} else {
long m = (this.l + this.r)/2;
makeChildren();

if(this.flag > 0 && this.l != this.r) {
this.left.value = Math.max(this.left.value, this.flag);
this.right.value = Math.max(this.right.value, this.flag);
this.left.flag = Math.max(this.left.flag, this.flag);
this.right.flag = Math.max(this.right.flag, this.flag);
this.flag = 0;
}

if (l <= m){
this.left.update(l, r, Math.max(this.flag, value));
}
if (r > m){
this.right.update(l, r, Math.max(this.flag, value));
}

this.value = Math.max(this.left.value, this.right.value);
}
}

@Override
public String toString(){
return String.format("[%d,%d]:%d,%d", this.l, this.r, this.value, this.flag);
}
}
class Solution {
public static List<List<Integer>> getSkyline(int[][] buildings) {
Node root = new Node(0, 0, Integer.MAX_VALUE);

for(int[] building: buildings){
root.update(building[0]+1, building[1], building[2]);
}
// for (int i = 0; i < 25; i++) {
// System.out.println(i+"\t"+root.get(i,i));
// }

//"[[2,10],[3,15],[7,12],[12,0],[15,10],[20,8],[24,0]]"
List<List<Integer>> ret = new ArrayList<>();
Map<Integer, Integer> history = new HashMap<>();
for(int[] building: buildings){
history.put(building[0], root.get(building[0],building[0]));
history.put(building[0] + 1, root.get(building[0] + 1,building[0] + 1));
history.put(building[1], root.get(building[1],building[1]));
history.put(building[1] + 1, root.get(building[1] + 1,building[1] + 1));

if(history.get(building[0]) < history.get(building[0]+1)) {
List<Integer> list = new ArrayList<>();
list.add(building[0]);
list.add(history.get(building[0]+1));
ret.add(list);
}
if(history.get(building[1]+1) < history.get(building[1])) {
List<Integer> list = new ArrayList<>();
list.add(building[1]);
list.add(history.get(building[1]+1));
ret.add(list);
}
}

Collections.sort(ret, (x1,x2) -> x1.get(0) - x2.get(0));

//System.out.println(history);
//System.out.println(ret);

// for (int i = 0; i < 25; i++) {
// System.out.println(i+"\t"+root.get(i,i));
// }

// Queue<Pair<Node, Integer>> queue = new LinkedList<>();
// queue.offer(new Pair<>(root, 0));
// Map<Integer, List<Node>> map = new HashMap<>();
// while(!queue.isEmpty()){
// Pair<Node, Integer> p = queue.poll();
// map.computeIfAbsent(p.getValue(), k->new ArrayList<>()).add(p.getKey());
// if(p.getKey().left != null) {
// queue.offer(new Pair<>(p.getKey().left, p.getValue()+1));
// }
// if(p.getKey().right != null) {
// queue.offer(new Pair<>(p.getKey().right, p.getValue()+1));
// }
// }

// for (int i = 0; i < 100; i++) {
// if (!map.containsKey(i)){
// break;
// }
// for(Node n: map.get(i)){
// System.out.print(n.toString()+"\t");
// }
// System.out.println();
// }
List<List<Integer>> omitDuplicated = new ArrayList<>();
omitDuplicated.add(ret.get(0));
for (int i = 1; i < ret.size(); i++) {
if(!ret.get(i).get(0).equals(ret.get(i-1).get(0)) || !ret.get(i).get(1).equals(ret.get(i-1).get(1))){
omitDuplicated.add(ret.get(i));
}
}

return omitDuplicated;
}
}