As far as I understood the question, you have a range of [A, B] and form requests -
- Assign a specific category range
Eg
R1 R2 C1
R3 R4 C2
- Request a range for the total number of items in specific categories. For instance. find the number of categories in R1 R4
A simple implementation using dictionaries as above will not work, as I will describe in this example -
suppose we have a range of [1000, 5000]
and we make the assignment as follows:
1 2 C1
2 3 C2
3 4 C3
......
4999 5000 C4999
Now we do the following assignment
1 5000 C5555
This will update / change / delete previously assigned range ranges O (N), where N is the maximum range size (B - A)
D ['category'] = set (of_all_the_ranges_you_have_in_category)
In this case, for the last assignment (1 5000 C5555), you will need to delete the previous ranges from the corresponding sets in categories C1, C2 ... C4999
OR
1: {"stop": 5, "category": "C1"}, 6: {"stop": 19, "category": "C23"},
Here, for the last assignment (1,500 C5555), a category update will be required for each initial value (1,2,3,4 ..., 4999)
The best option that will make updating ranges in O (lg n) would be segment trees ( http://en.wikipedia.org/wiki/Segment_tree )
In the above example, the segment tree will look something like this:
1000:5000 | --------------------- | | 1000:3000 3001:5000 | | ---------------- -------------------- | | | | 1000:2000 2001:3000 3001:4000 4001:5000
.................................................. ............... ................................... ............................ etc.
Leaf nodes will have ranges (1: 2, 2: 3, ...)
You can assign a category value to each node and set the interval by going through the tree, dividing the interval accordingly (for example, from 2500 to 4500 divided by 2500: 3000 and 3001: 4500 and move in both directions until the nodes coincide ranges).
Now the interesting thing is to update child nodes when you need them. For example, do not start updating children immediately while completing activities such as type 1 5000 C5555. This thing is called lazy propaganda, and you can learn more about it ( http://www.spoj.pl/forum/viewtopic.php?f=27&t=8296 ).
Now for the request part. If the number of categories is very small, you can maintain a frequency table in each node and update the ranges when necessary, and if necessary use lazy propagation, you will need to cross the entire tree from the sheet to node, counting and complexity will become O (n).
I think a better query solution may exist. It does not occur to me.
UPDATE Let's take a small example.
Range [1.8]
Allowed Categories {C1, C2}
1:8 1:4 5:8 1:2 3:4 5:6 7:8 1:1 2:2 3:3 4:4 5:5 6:6 7:7 8:8
Each node will have 3 fields [category, category_counts [], children_update_required = false]
1 5 C1
The request will be split, and category 1: 4 nodes will be set to C1, and child_update_required will be set to true, its children will no longer be updated (remember updating only when necessary or lazy distribution). In addition, the node 5: 5 category will be set to C1
3 4 C2
The request will propagate along the tree in the 3: 4 direction (and in the process of reaching 3: 4, 1: 2 and 3: 4 the categories will be updated to C1, 1: 4 children_update_required will be set to false, 1: 2 and 3: 4 children_update_required will be set to true) and will now update the category from 3: 4 to C2 according to the current requirement. It will then be established that child_update is required from 3: 4 to be true for the future update of its children (which is already set in this case .. no harm).