<p>You are given two integers <code>m</code> and <code>n</code> representing a <strong>0-indexed</strong> <code>m x n</code> grid. You are also given two 2D integer arrays <code>guards</code> and <code>walls</code> where <code>guards[i] = [row<sub>i</sub>, col<sub>i</sub>]</code> and <code>walls[j] = [row<sub>j</sub>, col<sub>j</sub>]</code> represent the positions of the <code>i<sup>th</sup></code> guard and <code>j<sup>th</sup></code> wall respectively.</p>

<p>A guard can see <b>every</b> cell in the four cardinal directions (north, east, south, or west) starting from their position unless <strong>obstructed</strong> by a wall or another guard. A cell is <strong>guarded</strong> if there is <strong>at least</strong> one guard that can see it.</p>

<p>Return<em> the number of unoccupied cells that are <strong>not</strong> <strong>guarded</strong>.</em></p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/03/10/example1drawio2.png" style="width: 300px; height: 204px;" />
<pre>
<strong>Input:</strong> m = 4, n = 6, guards = [[0,0],[1,1],[2,3]], walls = [[0,1],[2,2],[1,4]]
<strong>Output:</strong> 7
<strong>Explanation:</strong> The guarded and unguarded cells are shown in red and green respectively in the above diagram.
There are a total of 7 unguarded cells, so we return 7.
</pre>

<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/03/10/example2drawio.png" style="width: 200px; height: 201px;" />
<pre>
<strong>Input:</strong> m = 3, n = 3, guards = [[1,1]], walls = [[0,1],[1,0],[2,1],[1,2]]
<strong>Output:</strong> 4
<strong>Explanation:</strong> The unguarded cells are shown in green in the above diagram.
There are a total of 4 unguarded cells, so we return 4.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= m, n &lt;= 10<sup>5</sup></code></li>
	<li><code>2 &lt;= m * n &lt;= 10<sup>5</sup></code></li>
	<li><code>1 &lt;= guards.length, walls.length &lt;= 5 * 10<sup>4</sup></code></li>
	<li><code>2 &lt;= guards.length + walls.length &lt;= m * n</code></li>
	<li><code>guards[i].length == walls[j].length == 2</code></li>
	<li><code>0 &lt;= row<sub>i</sub>, row<sub>j</sub> &lt; m</code></li>
	<li><code>0 &lt;= col<sub>i</sub>, col<sub>j</sub> &lt; n</code></li>
	<li>All the positions in <code>guards</code> and <code>walls</code> are <strong>unique</strong>.</li>
</ul>

class Solution:    
    def countUnguarded(self, m: int, n: int, guards: List[List[int]], walls: List[List[int]]) -> int:
        amt = (m * n) - len(walls)
        walls_x, walls_y = defaultdict(list), defaultdict(list)
        ranges_x, ranges_y = defaultdict(list), defaultdict(list)
        for x, y in walls:
            insort_left(walls_x[x], y)
            insort_left(walls_y[y], x)
        def range_search(a, i, lim):
            if not a:
                return (0, lim)
            j = bisect_left(a, i)
            if j == len(a):
                return (a[-1]+1, lim)
            if j == 0:
                if a[j] < i:
                    return (a[0]+1, lim)
                return (0, a[0])
            return (a[j-1]+1, a[j])
        for x, y in guards:
            insort_left(ranges_x[x], range_search(walls_x[x], y, n))
            insort_left(ranges_y[y], range_search(walls_y[y], x, m))
        for d in ranges_x, ranges_y:
            for s in d:
                d[s] = self.merge(d[s])
        y_list = sorted(ranges_y)
        for ranges in ranges_x, ranges_y:
            amt -= sum((c2 - c1) for cs in ranges.values() for c1, c2 in cs)
        for x, ys in ranges_x.items():
            for y1, y2 in ys:
                for y in y_list[bisect_left(y_list, y1):bisect_left(y_list, y2)]:
                    ip = min(bisect_left(ranges_y[y], (x,)), len(ranges_y[y])-1)
                    if x in range(*ranges_y[y][ip]) or (ip > 0 and x in range(*ranges_y[y][ip-1])):
                        amt += 1
        return amt
    
    def merge(self, intervals: List[List[int]]) -> List[List[int]]:
        out = []
        for start, end in intervals:
            if not out:
                out.append((start, end))
            elif out[-1][1] >= start:
                out[-1] = out[-1][0], max(end, out[-1][1])
            else:
                out.append((start, end))
        return out
2831