I want to add numbers to a set of integers and answer a query how many numbers are there which are less than a given number K. What is an efficient way to implement this other than BIT?
# | User | Rating |
---|---|---|
1 | ecnerwala | 3649 |
2 | Benq | 3581 |
3 | orzdevinwang | 3570 |
4 | Geothermal | 3569 |
4 | cnnfls_csy | 3569 |
6 | tourist | 3565 |
7 | maroonrk | 3531 |
8 | Radewoosh | 3521 |
9 | Um_nik | 3482 |
10 | jiangly | 3468 |
# | User | Contrib. |
---|---|---|
1 | maomao90 | 174 |
2 | awoo | 164 |
3 | adamant | 161 |
4 | TheScrasse | 159 |
5 | nor | 158 |
6 | maroonrk | 156 |
7 | -is-this-fft- | 152 |
8 | SecondThread | 147 |
9 | orz | 146 |
10 | pajenegod | 145 |
I want to add numbers to a set of integers and answer a query how many numbers are there which are less than a given number K. What is an efficient way to implement this other than BIT?
Name |
---|
I don't know what is BIT, but you can simply use a vector as a set (find exact position for a newer element using
lower_bound
and inserting if element at this position isn't equal to the one you're trying to add). Then, just usingdistance(vect.begin(), lower_bound(vect.begin(), vect.end(), K))
— you can obtain the answer for a query.inserting in the middle of a vector is O(n).
Well, yes. Then, you could insert in a
set
first, then docopy(iset.begin(), iset.end(), back_inserter(vect))
and perform queries on the vector as stated above. I expect copying to have linear complexity.Just sort the vector using
sort(v.begin(),v.end())
C++ STL: Policy based data structures
Using a treap to implement a set(set in STL),it is O(log n).