Please take a look at the approach
ikegami has provided and also at the approaches discussed in answer to the startlingly similar question posed in
Need some help building a data structure. and tell us how they do or do not serve your purpose. Knowing this, we may be better able to advise on the most effective approach.