A 'person' is represented by a combination of postal code and mailbox number(both are natural numbers)
No two postal codes are alike, yet a postal code can contain many different mailboxes.
At any given postal code, all mailboxes have different numbers.
Display a data structure that supports the following methods in best complexity:
$Insert(z,p)$: insert a new person into the structure, who's postal code is $z$ and mailbox number is $p$.
$Delete(z,p)$: delete a person with postal code $z$ and mailbox number $p$ .
$Median(z)$: at postal code $z$, who has a mailbox that is the median? (half of people at postal code $z$ have mailbox with lower number)
$Max(z_1,z_2)$: out of the people living in the range $z_1 \rightarrow z_2$, who has the maximal mailbox number?
$HowMany(x)$: how many postal codes have exactly $x$ mailboxes?
The DS I've come up with was an AVL tree, where each key is a postal code and each node has a pointer to another AVL tree which represents the mailboxes in that postal code. That solves $insert/delete$ in
$O(max\{log$(amount of postal codes)$, log$(amount of mailboxes in postal code z)$\})$
which seems pretty efficient.
From this point forward I'm not sure how to continue, Median seems like $O(log$(amount of postal codes)$)$ to me since the mailboxes are in an AVL tree, so we can keep track of how many mailboxes we got, if number of mailboxes is uneven, the median is at the root, else we can find the heavier subtree and claim the median is the mean of the root and the root of said subtree, yet I'm not sure that works.
About $max$ and $howmany$, I still can't think of a solution that yields anything better than $O(n)$ where $n=$number of postal codes so far.
What would be some efficient data structure for my situation?