ecnerwala / cp-book

Book Code for Competitive Programming
Creative Commons Zero v1.0 Universal
550 stars 64 forks source link

Examples #8

Closed George-Ogden closed 3 years ago

George-Ogden commented 3 years ago

This is a great repo, but I'm not sure how to use it. Do you think you could add some examples or explanations on how to use the code, please?

ecnerwala commented 3 years ago

That's a good idea. For now, you can take a look at the tests to see sample usages.

ecnerwala commented 3 years ago

For what it's worth, this repo's code errs on the side of code that is generic/zero-cost over easy-to-use, so a lot of the interfaces are somewhat esoteric. If you're looking for simpler-to-use code, a lot of other code books are more straightforward and accessible.

ecnerwala commented 3 years ago

Is there some file in particular you're curious about?

George-Ogden commented 3 years ago

The one that I looked at was the segment tree, as that's a fundamental data structure, and I couldn't make any sense of it.

ecnerwala commented 3 years ago

The segment tree is the most esoteric interface in an effort to be extremely generic, and definitely requires a pretty strong knowledge of segment trees to use. Unlike many other segment tree templates, which manage an array of data for you, my template manages indices of a complete binary tree stored in the preorder layout, and you have to manage the data yourself. The fundamental ideas are that we decompose any range into O(log n) nodes of the tree, and you can use pass a callback to many iterator methods to iterate over that subset or their parents.

George-Ogden commented 3 years ago

I see - but do you still think you could give an example of it being used for a problem, please?

ecnerwala commented 3 years ago

You can check out https://codeforces.com/contest/1439/submission/98717653.

George-Ogden commented 3 years ago

That's great, thanks

GMbrianlaw commented 1 year ago

Can you give a quick explanation on how range::for_each_l_to_r and range::for_each_r_to_l work? Thanks.