Блог пользователя nerd

Автор nerd, 14 лет назад, По-английски

Dear All,

How to access to i*th element of a set in c++ effectively (*O(1) or (logN))?

Thanks

  • Проголосовать: нравится
  • +1
  • Проголосовать: не нравится

»
14 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

There is no documented way to do this. But set is a RB-tree, so you probably can get access to tree's structure and get i-th element in by yourself. I don't think it's a good way

»
14 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

The trouble is that none of the tree-like data structures available in the standard libraries support a fast k-th element query ("given k, find the k-th smallest of the stored numbers").

The thing you have to add to a canonical balanced tree structure is information about subtree sizes: for each node in the tree, keep the count of elements in the subtree rooted at this node. For a particularly easy-to-implement balanced tree structures, check out splay trees and treaps.

(c) http://community.topcoder.com/tc?module=Static&d1=match_editorials&d2=srm310

  • »
    »
    14 лет назад, скрыть # ^ |
    Rev. 2  
    Проголосовать: нравится 0 Проголосовать: не нравится

    You forgot to mention the decreased efficiency of insert/remove for tree with info on branch sizes (up to O(n), I think). You see, it is just a way of precalculation.

    More obvious precalculation is, surely, to dump the set into an array and then to make as many queries as necessary.

    UPD: I think I am wrong and efficiency of inserting/removing may remain O(logN), sorry.

»
14 лет назад, скрыть # |
 
Проголосовать: нравится +14 Проголосовать: не нравится

»
14 лет назад, скрыть # |
Rev. 5  
Проголосовать: нравится +3 Проголосовать: не нравится

There is a cheat to access Parent, Left and Right nodes. But i have no idea how to use this information "online". There is problems because of tree modifications — rotations and other balancing stuff while inserting or erasing nodes.

»
11 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Hey! Looks like there's a way to do it after all. Please see http://mirror.codeforces.com/blog/entry/11080

»
11 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

You can use C++ SGI STL tree.

For example:


#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> //required #include <ext/pb_ds/tree_policy.hpp> //required using namespace __gnu_pbds; //required using namespace std; template <typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; ordered_set <int> s; /* or: typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set; ordered_set s; This works in C++98 but the above version only works in C++11 */ int main(){ // It has C++ `set` functions: for(auto i: {1,3,5,8}) s.insert(i); s.erase(8); s.erase(s.find(8)); cout << * s.begin() << endl; if(s.begin() == s.end() or s.empty()) cout << "empty" << endl; s.lower_bound(3); s.upper_bound(1); cout << s.size() << endl; // special `tree` functions: cout << s.order_of_key(3) << endl; // the number of elements in s less than 3 (in this case, 0-based index of element 3) cout << *s.find_by_order(1) << endl; // 1-st elemt in s (in sorted order, 0-based) }
»
11 лет назад, скрыть # |
Rev. 2  
Проголосовать: нравится 0 Проголосовать: не нравится

How about binary search? You have lower_bound() for set. Please correct me if I am wrong. The complexity is log(n)*log(L) though. L= range of values in set,n=no. of values in set.

int idx=(int)(S.lower_bound(start,end,value)-S.begin()) if idx>k , search again.

»
6 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Does anyone know of something similar in Java?