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

Автор Timosh, история, 3 месяца назад, По-английски

std::set is a powerful data structure: it allows to do insertions/deletions pretty fast, while also being able to access the smallest/biggest element. On top of that, you can also find the smallest element in the set which is greater than some $$$x$$$. If you can use it properly, it is already enough to solve some of the 1800-2000 rated problems.

On the other hand, what could ordered_set possibly offer? It can do 2 things (other than std::set):

  • st.find_by_order(x) — returns the iterator of the $$$x$$$-th smallest element
  • st.order_of_key(x) — returns the number of elements less than $$$x$$$ (i.e. index of the smallest element $$$\ge x$$$)

One of the basic applications of ordered_set would be counting the number of inversions of an arbitrary permutation.

For comparison:

Ordered set way

ordered_set<int> st;
int ans = 0;
for (int i = n - 1; i >= 0; i--)
   ans += st.order_of_key(p[i]), st.insert(p[i]);

Merge sort way

// too long to fit in 65536 characters

"There's not much you can do with this, other than count inversions", you might foolishly say, without having solved 2400-2500 rated problems using just ordered_set. Let's take 2064E - Mycraft Sand Sort as an example. In the editorial it says "blah blah blah, use dsu and segtree". But little does the foolish author of this problem know, is the existence of ordered_set (Intellegent is actually noob). As you can see in 306417199, it can easily be solved using 2 ordered_sets. Why use 2 different data structures when you can use a single one twice? Another example would be 2059E1 - Stop Gaming (Easy Version), which is also intended to have a long-implemented solution, yet still solvable using just the ordered_set, which will end up being a lot shorter than the intended code. There are a lot more problems than these which could be solved using ordered_set, it's just that I couldn't find worthy candidates for this blog(maybe you can help).

Another funny (but not really useful) application, is handling point update and range sum queries in $$$O(log\ n\ log\ A)$$$(where $$$A$$$ is the max element). It can be done by creating $$$log\ A$$$ ordered sets, and inserting each bit of the element being added to the corresponding ordered set. To get a sum in $$$[l, r]$$$, we can just count the number of elements between $$$l$$$ and $$$r$$$ for each bit, and sum everything up.

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

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

What the hell Intellegent slander will not be tolerated

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

You can actually solve Mycraft Sand Sort with just a normal set: 306419534

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

Sparse seg tree: Hi

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

I dont like ordered set because it is much slower than compression + normal segtree

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

personally i consider ordered_set a crutch, but i do understand why people use it

there is a way to replicate ordered_set's .find_by_order() and .order_of_key() (and possibly even iterator increment/decrement too, but i haven't tried) with the same complexity (and less constant) using coordinate compression and a fenwick tree, and i prefer understanding and getting to interact with what im working with rather than using some weird library ds

also you can solve 2064E - Mycraft Sand Sort with only DSU in $$$O(n\alpha(n))$$$ 359696669

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

You mean to say "imagine not using PBDS". You just gave your own, somewhat misleading (set is already ordered, just not random-access) name to a particular PBDS template class.

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

sqrt decomposition gaming: hi

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

:(

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

Bruh, What did Intellegent do to you? You are so agressive!

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

I do not know why your blog reads so aggressive. What's more, you may not know there is a C++ version of SortedList (which is largely used in Python), and it is more efficient than PBDS.

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

Intellegent slander in big 2026 :(