How can find the value from set given an index. suppose our set is st. st.insert(2); st.insert(6); st.insert(1);
Now i want to know the value of index 2 from set. Is there any logarithmic away? Thanks advance .
# | 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 | 167 |
2 | Um_nik | 163 |
3 | maomao90 | 162 |
3 | atcoder_official | 162 |
5 | adamant | 159 |
6 | -is-this-fft- | 158 |
7 | awoo | 157 |
8 | TheScrasse | 154 |
9 | Dominater069 | 153 |
9 | nor | 153 |
How can find the value from set given an index. suppose our set is st. st.insert(2); st.insert(6); st.insert(1);
Now i want to know the value of index 2 from set. Is there any logarithmic away? Thanks advance .
Name |
---|
See here Your text to link here...
UPD: IGNORE
There is no log-time solution.
You cannot do it with a set.
However, there is another data structure with can answer such queries. See this blog post.
The link dose not work.
Sorry, fixed it.
You can find in logarithmic time with policy based data structures. See my this comment.
You can use a vector.
Let's define a vector S.
insert a value x like this : S.insert(lower_bound(S.begin( ),S.end( ),x),x);
find the position of x like this : pos = upper_bound(S.begin( ),S.end( ),x)-S.begin( )
The complexity of vector's INSERT can be seen as LOG . However , it's slower than set. And make sure that values in the vector are ordered at the beginning.
According to your hints I got TLE this problem . http://www.spoj.com/problems/ORDERSET/ my code: http://www.spoj.com/submit/ORDERSET/id=19746888
SPOJ is too slow to accept the complexity of Vector's INSERT .. You can try to use segment-tree / binary-search-tree instead of STL.