##### Welcome to Subscribe On Youtube

Formatted question description: https://leetcode.ca/all/699.html

# 699. Falling Squares (Hard)

On an infinite number line (x-axis), we drop given squares in the order they are given.

The i-th square dropped (positions[i] = (left, side_length)) is a square with the left-most point being positions[i][0] and sidelength positions[i][1].

The square is dropped with the bottom edge parallel to the number line, and from a higher height than all currently landed squares. We wait for each square to stick before dropping the next.

The squares are infinitely sticky on their bottom edge, and will remain fixed to any positive length surface they touch (either the number line or another square). Squares dropped adjacent to each other will not stick together prematurely.

Return a list ans of heights. Each height ans[i] represents the current highest height of any square we have dropped, after dropping squares represented by positions[0], positions[1], ..., positions[i].

Example 1:

Input: [[1, 2], [2, 3], [6, 1]]
Output: [2, 5, 5]
Explanation:


After the first drop of positions[0] = [1, 2]: _aa _aa ------- The maximum height of any square is 2.

After the second drop of positions[1] = [2, 3]: __aaa __aaa __aaa _aa__ _aa__ -------------- The maximum height of any square is 5. The larger square stays on top of the smaller square despite where its center of gravity is, because squares are infinitely sticky on their bottom edge.

After the third drop of positions[1] = [6, 1]: __aaa __aaa __aaa _aa _aa___a -------------- The maximum height of any square is still 5. Thus, we return an answer of [2, 5, 5].

Example 2:

Input: [[100, 100], [200, 100]]
Output: [100, 100]
Explanation: Adjacent squares don't get stuck prematurely - only their bottom edge can stick to surfaces.


Note:

• 1 <= positions.length <= 1000.
• 1 <= positions[i][0] <= 10^8.
• 1 <= positions[i][1] <= 10^6.

Related Topics:
Segment Tree, Ordered Map

Similar Questions:

## Solution 1. Segment Tree

// OJ: https://leetcode.com/problems/falling-squares/
// Time: O(N^2 * logN)
// Space: O(N)
class Solution {
vector<int> tree;
int N = 0;
void updateAt(int i, int val) {
i += N;
tree[i] = val;
while (i > 0) {
i /= 2;
tree[i] = max(tree[2 * i], tree[2 * i + 1]);
}
}
void update(int begin, int end, int val) {
for (int i = begin; i < end; ++i) updateAt(i, val);
}
int maxRange(int i, int j) {
i += N;
j += N;
int ans = 0;
while (i <= j) {
if (i % 2) ans = max(ans, tree[i++]);
if (j % 2 == 0) ans = max(ans, tree[j--]);
i /= 2;
j /= 2;
}
return ans;
}
public:
vector<int> fallingSquares(vector<vector<int>>& P) {
set<int> ps;
for (auto &p : P) {
ps.insert(p[0]);
ps.insert(p[0] + p[1] - 1);
}
unordered_map<int, int> m;
for (int n : ps) m[n] = N++;
tree.resize(2 * N);
vector<int> ans;
int top = 0;
for (auto &p : P) {
int a = m[p[0]], b = m[p[0] + p[1] - 1];
int tmp = p[1] + maxRange(a, b);
update(a, b + 1, tmp);
top = max(top, tmp);
ans.push_back(top);
}
return ans;
}
};

• class Solution {
public List<Integer> fallingSquares(int[][] positions) {
List<Integer> maximumHeights = new ArrayList<Integer>();
int maximumHeight = 0;
TreeMap<Integer, Integer> map = new TreeMap<Integer, Integer>();
int length = positions.length;
for (int i = 0; i < length; i++) {
int[] position = positions[i];
int left = position[0], height = position[1];
int right = left + height;
int prevHeight = 0;
Integer prev = map.floorKey(left);
if (prev != null)
prevHeight = map.get(prev);
Integer next = map.ceilingKey(left);
int rightHeight = 0;
Integer rightFloor = map.floorKey(right);
if (rightFloor != null)
rightHeight = map.get(rightFloor);
while (next != null && next < right) {
prevHeight = Math.max(prevHeight, map.get(next));
map.remove(next);
next = map.ceilingKey(left);
}
int newHeight = prevHeight + height;
map.put(left, newHeight);
map.put(right, rightHeight);
maximumHeight = Math.max(maximumHeight, newHeight);
}
return maximumHeights;
}
}

############

class Node {
Node left;
Node right;
int l;
int r;
int mid;
int v;
public Node(int l, int r) {
this.l = l;
this.r = r;
this.mid = (l + r) >> 1;
}
}

class SegmentTree {
private Node root = new Node(1, (int) 1e9);

public SegmentTree() {
}

public void modify(int l, int r, int v) {
modify(l, r, v, root);
}

public void modify(int l, int r, int v, Node node) {
if (l > r) {
return;
}
if (node.l >= l && node.r <= r) {
node.v = v;
return;
}
pushdown(node);
if (l <= node.mid) {
modify(l, r, v, node.left);
}
if (r > node.mid) {
modify(l, r, v, node.right);
}
pushup(node);
}

public int query(int l, int r) {
return query(l, r, root);
}

public int query(int l, int r, Node node) {
if (l > r) {
return 0;
}
if (node.l >= l && node.r <= r) {
return node.v;
}
pushdown(node);
int v = 0;
if (l <= node.mid) {
v = Math.max(v, query(l, r, node.left));
}
if (r > node.mid) {
v = Math.max(v, query(l, r, node.right));
}
return v;
}

public void pushup(Node node) {
node.v = Math.max(node.left.v, node.right.v);
}

public void pushdown(Node node) {
if (node.left == null) {
node.left = new Node(node.l, node.mid);
}
if (node.right == null) {
node.right = new Node(node.mid + 1, node.r);
}
Node left = node.left, right = node.right;
}
}
}

class Solution {
public List<Integer> fallingSquares(int[][] positions) {
List<Integer> ans = new ArrayList<>();
SegmentTree tree = new SegmentTree();
int mx = 0;
for (int[] p : positions) {
int l = p[0], w = p[1], r = l + w - 1;
int h = tree.query(l, r) + w;
mx = Math.max(mx, h);
tree.modify(l, r, h);
}
return ans;
}
}

• // OJ: https://leetcode.com/problems/falling-squares/
// Time: O(N^2 * logN)
// Space: O(N)
class Solution {
vector<int> tree;
int N = 0;
void updateAt(int i, int val) {
i += N;
tree[i] = val;
while (i > 0) {
i /= 2;
tree[i] = max(tree[2 * i], tree[2 * i + 1]);
}
}
void update(int begin, int end, int val) {
for (int i = begin; i < end; ++i) updateAt(i, val);
}
int maxRange(int i, int j) {
i += N;
j += N;
int ans = 0;
while (i <= j) {
if (i % 2) ans = max(ans, tree[i++]);
if (j % 2 == 0) ans = max(ans, tree[j--]);
i /= 2;
j /= 2;
}
return ans;
}
public:
vector<int> fallingSquares(vector<vector<int>>& P) {
set<int> ps;
for (auto &p : P) {
ps.insert(p[0]);
ps.insert(p[0] + p[1] - 1);
}
unordered_map<int, int> m;
for (int n : ps) m[n] = N++;
tree.resize(2 * N);
vector<int> ans;
int top = 0;
for (auto &p : P) {
int a = m[p[0]], b = m[p[0] + p[1] - 1];
int tmp = p[1] + maxRange(a, b);
update(a, b + 1, tmp);
top = max(top, tmp);
ans.push_back(top);
}
return ans;
}
};

• class Node:
def __init__(self, l, r):
self.left = None
self.right = None
self.l = l
self.r = r
self.mid = (l + r) >> 1
self.v = 0

class SegmentTree:
def __init__(self):
self.root = Node(1, int(1e9))

def modify(self, l, r, v, node=None):
if l > r:
return
if node is None:
node = self.root
if node.l >= l and node.r <= r:
node.v = v
return
self.pushdown(node)
if l <= node.mid:
self.modify(l, r, v, node.left)
if r > node.mid:
self.modify(l, r, v, node.right)
self.pushup(node)

def query(self, l, r, node=None):
if l > r:
return 0
if node is None:
node = self.root
if node.l >= l and node.r <= r:
return node.v
self.pushdown(node)
v = 0
if l <= node.mid:
v = max(v, self.query(l, r, node.left))
if r > node.mid:
v = max(v, self.query(l, r, node.right))
return v

def pushup(self, node):
node.v = max(node.left.v, node.right.v)

def pushdown(self, node):
if node.left is None:
node.left = Node(node.l, node.mid)
if node.right is None:
node.right = Node(node.mid + 1, node.r)

class Solution:
def fallingSquares(self, positions: List[List[int]]) -> List[int]:
ans = []
mx = 0
tree = SegmentTree()
for l, w in positions:
r = l + w - 1
h = tree.query(l, r) + w
mx = max(mx, h)
ans.append(mx)
tree.modify(l, r, h)
return ans


• type node struct {
left      *node
right     *node
l, mid, r int
}

func newNode(l, r int) *node {
return &node{
l:   l,
r:   r,
mid: int(uint(l+r) >> 1),
}
}

func max(x, y int) int {
if x > y {
return x
}
return y
}

type segmentTree struct {
root *node
}

func newSegmentTree() *segmentTree {
return &segmentTree{
root: newNode(1, 1e9),
}
}

func (t *segmentTree) modify(l, r, v int, n *node) {
if l > r {
return
}
if n.l >= l && n.r <= r {
n.v = v
return
}
t.pushdown(n)
if l <= n.mid {
t.modify(l, r, v, n.left)
}
if r > n.mid {
t.modify(l, r, v, n.right)
}
t.pushup(n)
}

func (t *segmentTree) query(l, r int, n *node) int {
if l > r {
return 0
}
if n.l >= l && n.r <= r {
return n.v
}
t.pushdown(n)
v := 0
if l <= n.mid {
v = max(v, t.query(l, r, n.left))
}
if r > n.mid {
v = max(v, t.query(l, r, n.right))
}
return v
}

func (t *segmentTree) pushup(n *node) {
n.v = max(n.left.v, n.right.v)
}

func (t *segmentTree) pushdown(n *node) {
if n.left == nil {
n.left = newNode(n.l, n.mid)
}
if n.right == nil {
n.right = newNode(n.mid+1, n.r)
}
}
}

func fallingSquares(positions [][]int) []int {
ans := make([]int, len(positions))
t := newSegmentTree()
mx := 0
for i, p := range positions {
l, w, r := p[0], p[1], p[0]+p[1]-1
h := t.query(l, r, t.root) + w
mx = max(mx, h)
ans[i] = mx
t.modify(l, r, h, t.root)
}
return ans
}