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?
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 | Benq | 3792 |
| 2 | VivaciousAubergine | 3647 |
| 3 | Kevin114514 | 3603 |
| 4 | jiangly | 3583 |
| 5 | turmax | 3559 |
| 6 | tourist | 3541 |
| 7 | strapple | 3515 |
| 8 | ksun48 | 3461 |
| 9 | dXqwq | 3436 |
| 10 | Otomachi_Una | 3413 |
| # | User | Contrib. |
|---|---|---|
| 1 | Qingyu | 157 |
| 2 | adamant | 153 |
| 3 | Um_nik | 147 |
| 4 | Proof_by_QED | 146 |
| 5 | Dominater069 | 145 |
| 6 | errorgorn | 141 |
| 7 | cry | 139 |
| 8 | YuukiS | 135 |
| 9 | TheScrasse | 134 |
| 10 | chromate00 | 133 |
| 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_boundand 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
setfirst, 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).