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 | tourist | 4009 |
2 | jiangly | 3839 |
3 | Radewoosh | 3646 |
4 | jqdai0815 | 3620 |
4 | Benq | 3620 |
6 | orzdevinwang | 3612 |
7 | Geothermal | 3569 |
7 | cnnfls_csy | 3569 |
9 | ecnerwala | 3494 |
10 | Um_nik | 3396 |
# | User | Contrib. |
---|---|---|
1 | Um_nik | 164 |
2 | maomao90 | 160 |
3 | -is-this-fft- | 159 |
4 | atcoder_official | 158 |
4 | awoo | 158 |
4 | cry | 158 |
7 | adamant | 155 |
8 | nor | 154 |
9 | TheScrasse | 152 |
10 | maroonrk | 151 |
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).