As a follow up to my comment: using red-black binary search trees (instead of sets), if each node stores the number of nodes rooted in that node (updated every time you insert/delete a node) then you can get at "number of nodes more than/less than X" statistics quite fast.