Codeforces and Polygon may be unavailable from December 6, 19:00 (UTC) to December 6, 21:00 (UTC) due to technical maintenance. ×

caterpillow's blog

By caterpillow, 32 hours ago, In English

Hallo codeforcers,

As some of you may know, the almighty treap is one of the most versatile data structures out there. It can serve as an ordered + indexed set, a lazy segment tree, and even a dynamic array — all at the same time.

However, its immense versatility comes at a cost: there are so many different variations that making a one-template-fits-all is impossible. Alas, is fate such that we must write it from scratch — every, single, time?

Fear no more! I present to you BYOT: Build your own treap!

Now, you can summon a hand-crafted treap to overkill your Div2D in mere seconds. Gone are the days of using your brain to come up with a clever solution — just go to the site, click on what you need, and enjoy your AC! To see how it is used, you can see some example implementations below.

If you find any bugged configurations or have any suggestions, please leave a comment under this blog. Be warned, no rating refunds will be issued if something breaks on you during contest.

Supported features

Expand

Example problems + solutions

Now, I understand if there are still some doubts. To prove the effectiveness of this tool, I have implemented solutions to all of SecondThread's Algorithms Thread Treaps Contest (+ one more problem) using the website! Below, I have linked solutions, where you can see which pieces of code were written by me, which bits came from the site, and what configuration of settings I used.

102787A - Shandom RuffleSolution

Basic implicit treap with split and merge, with $$$O(n)$$$ build and tour. No modifications were made to the code after being copied from the site.

102787B - Pear TreaPSolution

Basic implicit treap with split, merge, insert and range queries (by index). I use two rolling hashes to check for palindromes, which is implemented very simply with only a few lines + modular power.

102787C - Sneetches and Speeches 3Solution

Implicit treap with split, merge, range updates and queries (by index), and range reversals. The code is rather bashy, but the body of the treap was left untouched.

102787D - The Grim TreaperSolution

This problem requires range chmin, range add and range sums — a problem classically solved using segtree beats. However, the cut and paste operation requires you to use a treap. Fortunately, the site supports these operations, so no modifications were made to the generated code. You can see all the operations in action on this submission to Range Chmin Chmax Add Range Sum on https://judge.yosupo.jp/.

102787Z - Trick or TreapSolution

This problem requires parent pointers and range sums, as well as basic split and merge functionality. This did not require any modifications to the code generated by the site.

Bonus

Since these problems did not feature anything that required the usage of keys, I have a bonus solution (that has been rewritten) to a recent Div2 where I used the site to quickly implement a thoughtless solution in contest.

2042D - Recommendations294603712

If you consider a user (l, r) as a point on a plane, the range of "strongly recommended tracks" is simply the intersection of all the users in a rectangle. By answering queries offline and sweeping through one axis (such as leftmost track), we can turn these rectangle queries into range queries.

The usual solution here would be to do coordinate compression + segtree or lazy-create/sparse segtree, but instead I used (you guessed it) a treap!

Here, I sweep through increasing leftmost track, maintaining a treap of previous users. The users in the treap are ordered by their key (the rightmost track some user likes) and I perform range queries and insertions on it. With byot, this data structure can be implemented by modifying only 3 lines of code.

Instead of writing a clever solution, I simply brute forced the range of strongly recommended tracks by abusing OP data structures.

  • Vote: I like it
  • +219
  • Vote: I do not like it

»
26 hours ago, # |
  Vote: I like it +87 Vote: I do not like it

Bro felt the need to one-up Australia's segtree obsession...

»
25 hours ago, # |
  Vote: I like it -83 Vote: I do not like it

or just ask gpt smartass

  • »
    »
    18 hours ago, # ^ |
      Vote: I like it +23 Vote: I do not like it

    Been there, done that, asked chatGPT to code a simple range sum treap, didn't work.

    • »
      »
      »
      17 hours ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      it is because she doesn’t like you

»
24 hours ago, # |
  Vote: I like it +30 Vote: I do not like it

Cool builder! I can give you some inspiration of what else you can add to the BYOT:

  • Extract
  • Cyclic shift
  • Updating key/value by position/key
  • Finding closest from left/right to position $$$i$$$ element that satisfies some predicate

https://github.com/wery0/CP-library/blob/master/Data%20structures/Treap/Treap.cpp

  • »
    »
    16 hours ago, # ^ |
      Vote: I like it +13 Vote: I do not like it

    Thanks for the suggestions!

    I've changed both deletes (by key or index) to also return the removed node, which I believe does the same as your "extract".

    I've added a helper function for cyclic shifts, as well as two functions that can be made to let you update key/value by position/key.

    I'm not quite sure what you meant with your last point, but I added some functions for finding partition points and cumulative partition points (eg. finding smallest prefix with sum <= x) based on a predicate (that hopefully works as intended).

»
24 hours ago, # |
  Vote: I like it +21 Vote: I do not like it

wtf

»
13 hours ago, # |
  Vote: I like it +8 Vote: I do not like it

OTZ, time to max out that data structures tag.