Solving Rectangle Area II in go. Please try yourself first to solve the problem and submit your implementation to LeetCode before looking into solution.

Problem Description

Example 1:

Input: [[0,0,2,2],[1,0,2,3],[1,0,3,1]]
Output: 6
Explanation: As illustrated in the picture.

Example 2:

Input: [[0,0,1000000000,1000000000]]
Output: 49
Explanation: The answer is 10^18 modulo (10^9 + 7), which is (10^9)^2 = (-7)^2 = 49.

Note:

  • 1 <= rectangles.length <= 200

  • rectanges[i].length = 4

  • 0 <= rectangles[i][j] <= 10^9

  • 1 <= rectangles.length <= 200

  • rectanges[i].length = 4

  • 0 <= rectangles[i][j] <= 10^9

      func (sat *SegmentAreaTree) pushUp(treeIndex, leftTreeIndex, rightTreeIndex int) {
          newCount, newValue := sat.merge(sat.tree[leftTreeIndex].count, sat.tree[rightTreeIndex].count), 0
          if sat.tree[leftTreeIndex].count > 0 && sat.tree[rightTreeIndex].count > 0 {
              newValue = sat.merge(sat.tree[leftTreeIndex].val, sat.tree[rightTreeIndex].val)
          } else if sat.tree[leftTreeIndex].count > 0 && sat.tree[rightTreeIndex].count == 0 {
              newValue = sat.tree[leftTreeIndex].val
          } else if sat.tree[leftTreeIndex].count == 0 && sat.tree[rightTreeIndex].count > 0 {
              newValue = sat.tree[rightTreeIndex].val
          }
          sat.tree[treeIndex] = SegmentItem{count: newCount, val: newValue}
      }

See the full details of the problem Rectangle Area II at LeetCode

Originally posted at: @github.com/halfrost/LeetCode-Go