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 | 3985 |
2 | jiangly | 3814 |
3 | jqdai0815 | 3682 |
4 | Benq | 3529 |
5 | orzdevinwang | 3526 |
6 | ksun48 | 3517 |
7 | Radewoosh | 3410 |
8 | hos.lyric | 3399 |
9 | ecnerwala | 3392 |
9 | Um_nik | 3392 |
# | User | Contrib. |
---|---|---|
1 | cry | 169 |
2 | maomao90 | 162 |
2 | Um_nik | 162 |
4 | atcoder_official | 161 |
5 | djm03178 | 158 |
6 | -is-this-fft- | 157 |
7 | adamant | 155 |
8 | Dominater069 | 154 |
8 | awoo | 154 |
10 | luogu_official | 150 |
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.