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

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

This is a C++ blog...

In this blog, I'll quickly discuss what PMR is at a very high level and discuss std::pmr::map's practicality and feasibility in competitive programming.

Containers in the pmr namespace allow you to separate memory allocation from data structure logic, which means you can plug in custom memory resources to control how and where memory is allocated.

This setup usually comes in handy in actual softwares where you want full control of allocated memory, but what about cp?

When can I use it?

If you know a clear and tight upper bound on the number of elements you will insert into your container. This is commonly the case when you use std::map.

How do I use it?

Suppose you are using an std::map<int,int> and you know that you will access at most 10^5 keys. We can use the expression: sizeof(std::_Rb_tree_node<std::pair<const int, int>>) to know the size of a single node in the underlying red-black tree used by std::map.

We need to allocate some memory (on the heap, stack or even statically) with the size of a single node multiplied by the number of nodes your upper bound requires.

static constexpr node_size = sizeof(std::_Rb_tree_node<std::pair<const int, int>>);
std::byte mem[node_size * 100'000];

Finally we need to declare some wrapper to help our containers use this memory, that is std::pmr::monotonic_buffer_resource

std::pmr::monotonic_buffer_resource resource(mem, sizeof(mem));

Now we can declare our std::pmr::map:

std::pmr::map<int,int> is_this_fast(&resource);

Should I use it?

Short answer: No

But... if you are writing a very nasty data structure just for a small speedup, maybe you should try this first.

As you can see, even if std::pmr::map is faster, the difference between std::map and std::pmr::map is very small and since this blog isn't sponsored by pmr I threw in std::unordered_map.

I didn't really test other containers like set or multiset and since we already have std::vector::reserve I felt its not necessary to discuss that.

Полный текст и комментарии »

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

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

Not sure if any blogs have been written on this recently. I found one from 10ish years ago.

Codeforces [GCC 14] compiler allows you to perform 33,554,432 operations during compile time. This is pretty small, with respect to normal time limits, but its still interesting to question whether or not it can be used to boost some solutions.

Heres an attempt to cheese a CSES problem with a solution which performs large matrix multiplications: https://cses.fi/paste/1052a83da5315cb7c5d15b/

There is an implementation of the solution mentioned above which passes CSES constraints but without compile time computations and heavily optimized.

My goal was to perform as many multiplications as possible during compilation and my results were:

  • CSES: 0 multiplications (they set a tight limit on the output file's size apparently)
  • Codeforces (custom invocation): 0 multiplication (but it let me construct and fill the base matrix)

Of course, these results can be predicted with basic math... after I was aware of the 33,554,432 limit.

This is probably a bad algorithm to test on (_or maybe bad constraints_). Anyways, can you think of ways to use this feature?

Forgot to mention the problem: Dice Probability

Полный текст и комментарии »

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

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

There is no such thing as wrong place or wrong time when it comes to this matter.

What is stopping you from posting about this? Is it because you aren't sure or because you are afraid of downvotes?

Post blogs and let everyone know!

Post, post, post!

Полный текст и комментарии »

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

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

Recently there has been mass hacking for quite some tasks on CSES like Inversion Probability, Fixed-Length Paths(I-II) and more.

Inversion Probability has particularly annoying new tests and (i think) an edited statement saying "(rounding half to even)". I have no clue what its supposed to mean, but luckily same idea but in python passed... for now.

Anyways at the time of this blog's making the task has only 84 solves, so if you caught an L on cses like the blog.

RIP Rainboy for losing #1.

Полный текст и комментарии »

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

Автор Syrian, история, 4 года назад, По-английски

Hello codeforces queens

its t7ya back at it again with the fire blogs

so peep this submission:

https://codeforces.me/contest/1702/submission/163553251

this guy avoided getting hacked without having to use neal's unordered map stuff or similar hashing functions. just one include. can anybody explain how this works. is it hackable?

shout out to my man almosabhali. mans a genius

Полный текст и комментарии »

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

Автор Syrian, история, 5 лет назад, По-английски

So i was browsing the web as always and i stumbled upon a sussy blog on codeforces:

https://codeforces.me/blog/entry/15898

When i clicked the link i was presented with some rather interesting imagery. I was confused so i clicked the link again, and again, and again and every time i clicked it, some more... graphic content was displayed.

I would like to thank nicola for his blog but cmon man

p.s cant wait for that v2 drop

Полный текст и комментарии »

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