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 | 3676 |
| 2 | Kevin114514 | 3659 |
| 3 | jiangly | 3640 |
| 4 | turmax | 3450 |
| 5 | tourist | 3382 |
| 6 | strapple | 3360 |
| 7 | Um_nik | 3354 |
| 8 | squareOf105 | 3348 |
| 9 | maroonrk | 3312 |
| 10 | idontwannadothis…e | 3191 |
| # | User | Contrib. |
|---|---|---|
| 1 | Qingyu | 155 |
| 2 | nik_exists | 151 |
| 3 | maspy | 149 |
| 4 | AmShZ | 145 |
| 5 | Um_nik | 143 |
| 6 | Errichto | 139 |
| 7 | BledDest | 136 |
| 8 | adamant | 134 |
| 9 | maroonrk | 132 |
| 10 | cry | 130 |
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_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).