Lemma 1. For this modified algorithm, regardless of the random choices of the algorithm, the time is alwaysLemma 1. $O(n\,m)$.For this modified algorithm, regardless of the random choices of the algorithm, the time is always $O(n\,m)$.
Proof.Proof. Fix an input $A[1..n]$ and $B[1..m]$ and fix the random choices
choices of the algorithm arbitrarily. In each (possibly recursive) call
call to partition(), the two arguments correspond respectively to two subarrays
subarrays: a subarray $A[i_1..i_2]$ of $A$ and a subarray
$B[j_1..j_2]$ of $B$. Identify such a call with the rectangle
$[i_1-1,i_2] \times [j_1-1,j_2]$. Thus, the top-level call is
$[0,n]\times[0,m]$, and each recursive call corresponds to a sub
sub-rectangle within this $n\times m$ rectangle. These rectangles form
form a tree, where the rectangle corresponding to one call has as children
children the rectangles that correspond to the calls made directly by that
that call. Each parent rectangle is partitioned by its child rectangles
rectangles, which form a $k\times k$ grid (of non-uniform rectangles) with $k$ at least 2. Of course each rectangle's corners have only integer coordinates.
Proof sketch.Proof sketch. Fixing every random choice except the number of duplicates of each rectangle, the time is proportional to
$$\sum_{r\in R} (1+X_r) |r|$$
where $R$ is the set of rectangles generated, $X_r$ is the number of times $r$ is duplicated (i.e., times when $k=1$ for that rectangle), and $|r|$ is the perimeter of $r$.
As an aside, if: if the algorithm were to choose $k$ randomly in $\min(n,m)$ instead of $\min(c,n)$ and then do $O(m n)$ work in each call (instead of $O(m+n)$), the total time would still be $O(mn)$ in expectation.
Proof. First note that the algorithm weretotal time would be proportional to choosethe sum of the $k$ inareas of all the rectangles. This sum equals the sum, over the integer coordinates $\min(n,m)$ instead$(i,j)$, of the number of rectangles that $\min(c,n)$ and then do$(i,j)$ occurs in. This is $O(m n)$ work$O(nm)$ in eachexpectation because, in expectation, any given $(i,j)$ in the original rectangle occurs in $O(1)$ rectangles.
To see this, suppose $(i,j)$ is contained in a rectangle $r$, and consider the call to partition on $r$. Let $q(r) = \min(n_r,m_r)$. Let $r'$ be the rectangle that is the child (instead ofcontaining $O(m+n)$$(i,j)$) of $r$. With probability at least $1/3$, the total time would still$k$ is chosen to be at least $O(mn)$$(2/3)q(r)$. Conditioned on that, $E[q(r')] \le 3/2$, so with constant probability $q(r')$ is at most 1. If that happens, then $r'$ is a leaf (has no children). It follows from this that the expected number of rectangles that $(i,j)$ is in expectation andis $O(1)$. QED
(Whether the $O(nm)$ bound would hold with high probability is an interesting question.. I think it would. Certainly $O(nm\log(nm))$ would hold w.h.p.)