-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.cs
More file actions
139 lines (125 loc) · 3.68 KB
/
Solution.cs
File metadata and controls
139 lines (125 loc) · 3.68 KB
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
public class SegmentTree
{
private int[] count;
private int[] covered;
private int[] xs;
private int n;
public SegmentTree(int[] xs_)
{
xs = xs_;
n = xs.Length - 1;
count = new int[4 * n];
covered = new int[4 * n];
}
private void Modify(int qleft, int qright, int qval, int left, int right,
int pos)
{
if (xs[right + 1] <= qleft || xs[left] >= qright)
{
return;
}
if (qleft <= xs[left] && xs[right + 1] <= qright)
{
count[pos] += qval;
}
else
{
int mid = (left + right) / 2;
Modify(qleft, qright, qval, left, mid, pos * 2 + 1);
Modify(qleft, qright, qval, mid + 1, right, pos * 2 + 2);
}
if (count[pos] > 0)
{
covered[pos] = xs[right + 1] - xs[left];
}
else
{
if (left == right)
{
covered[pos] = 0;
}
else
{
covered[pos] = covered[pos * 2 + 1] + covered[pos * 2 + 2];
}
}
}
public void Update(int qleft, int qright, int qval)
{
Modify(qleft, qright, qval, 0, n - 1, 0);
}
public int Query()
{
return covered[0];
}
}
public class Solution
{
public double SeparateSquares(int[][] squares)
{
// save events: (y-coordinate, type, left boundary, right boundary)
List<int[]> events = new List<int[]>();
SortedSet<int> xsSet = new SortedSet<int>();
foreach (var sq in squares)
{
int x = sq[0], y = sq[1], l = sq[2];
int xr = x + l;
events.Add(new int[] { y, 1, x, xr });
events.Add(new int[] { y + l, -1, x, xr });
xsSet.Add(x);
xsSet.Add(xr);
}
// sort events by y-coordinate
events.Sort((a, b) => a[0].CompareTo(b[0]));
// discrete coordinates
int[] xs = xsSet.ToArray();
// initialize the segment tree
SegmentTree segTree = new SegmentTree(xs);
List<long> psum = new List<long>();
List<int> widths = new List<int>();
long totalArea = 0;
int prev = events[0][0];
// scan: calculate total area and record intermediate states
foreach (var eventItem in events)
{
int y = eventItem[0], delta = eventItem[1], xl = eventItem[2],
xr = eventItem[3];
int len = segTree.Query();
totalArea += (long)len * (y - prev);
segTree.Update(xl, xr, delta);
// record prefix sums and widths
psum.Add(totalArea);
widths.Add(segTree.Query());
prev = y;
}
// calculate the target area (half rounded up)
long target = (totalArea + 1) / 2;
// find the first position greater than or equal to target using binary
// search
int idx = BinarySearch(psum, target);
// get the corresponding area, width, and height
double area = psum[idx];
int width = widths[idx], height = events[idx][0];
return height + (totalArea - area * 2) / (width * 2.0);
}
private int BinarySearch(List<long> list, long target)
{
int left = 0;
int right = list.Count - 1;
int result = 0;
while (left <= right)
{
int mid = left + (right - left) / 2;
if (list[mid] < target)
{
result = mid;
left = mid + 1;
}
else
{
right = mid - 1;
}
}
return result;
}
}