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 | 3823 |
3 | Benq | 3738 |
4 | Radewoosh | 3633 |
5 | jqdai0815 | 3620 |
6 | orzdevinwang | 3529 |
7 | ecnerwala | 3446 |
8 | Um_nik | 3396 |
9 | ksun48 | 3390 |
10 | gamegame | 3386 |
# | User | Contrib. |
---|---|---|
1 | cry | 166 |
2 | maomao90 | 163 |
2 | Um_nik | 163 |
4 | atcoder_official | 161 |
5 | adamant | 159 |
6 | -is-this-fft- | 158 |
7 | awoo | 157 |
8 | TheScrasse | 154 |
9 | nor | 153 |
9 | Dominater069 | 153 |
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).