Bruteforceman's blog

By Bruteforceman, history, 4 months ago, In English

Segment trees are not as memory efficient as fenwick trees which often becomes the bottleneck of many problems. There has been attempts at reducing the space required to build a segment tree (e.g. iterative implementation takes $$$2N$$$ space instead of $$$4N$$$ space). But there's a very simple solution to this that can asymptotically improve the space complexity (sorry if this is already well-known).

When building the segment tree, we can simply stop the recursion when $$$r - l \leq \lg n$$$. This means the size of the tree is $$$O \left( \frac{n}{\lg n} \right)$$$ instead of $$$O(n)$$$.

Fortunately we can still answer queries (or perform updates) in $$$O(\lg n)$$$ time for most problems. For the internal nodes, we can simply collect the contribution of that node just like usual. For the leaf nodes with range $$$[l, r]$$$, we will have to scan through the original array in $$$a[l \cdots r]$$$ with a loop and calculate their contribution to the query. Since $$$r - l \leq \lg n$$$, the loop still takes $$$O(\lg n)$$$ time. Any range update/query visits only two leaf nodes, so the time required to process the leaf nodes are also $$$O(\lg n)$$$.

This trick may not be that useful if we need only one segment tree in our problem. However, many problems require building multiple segment trees. For example, one common pattern is to create a segment tree for each bit position. Applying this trick to such problems can reduce the space complexity from $$$O(n \lg n)$$$ to $$$O(n)$$$.

Example Problem:

Full text and comments »

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

By Bruteforceman, history, 2 years ago, In English

Hi, I made a duel bot where you can challenge other users in a OI styled duel. It is sort of like a combination of regular duel matches and lockout challenges. You can find the bot at OIDuelBot. The bot is currently available only for Telegram, but I have plans to make one for discord as well.

Rules

You can challenge other users in a OI styled duel. In the duel, you have to race against time to solve the subtasks before your opponent. If your opponent solves a particular subtask before you, they gets the point for that subtask, and you won't get any points for it even if you solve it in the future. The player with more points at the end of the duel wins the challenge.

How to use

First, you need to add the bot in a telegram group. Here are the commands you can use in the bot

/register [oj.uz username] — you have to register yourself in order to enter your duel

/challenge [telegram username] — you can challenge other users in a OI styled duel

/accept — you can accept the challenge from other users

/decline — you can decline the challenge from other users

/duration [minutes] — you can set the duration of your challenge between 10 and 180 minutes

/difficulty [number] — you can set a difficulty from 1 to 10

/withdraw — you can withdraw from your current challenge

/rules — you can see the rules of the duel

/help — shows help text

Note that you have to start all commands with a forward slash which is the convention for telegram bots.

How the bot works

When you challenge a user, you will get a random problem unsolved by both users from oj.uz. The difficulty is measured by the number of ACs, which is probably not a very good measure, but I couldn't think of anything better for now. So you may sometimes get problems with lower or higher difficulty than you expect. If you don't like the problem, you can withdraw the match with your opponent. Also, since I am hosting the bot in a free heroku server, it will be inactive for about 6 hours every day (from 3:30 AM BST onwards). So if the bot doesn't respond, it's probably inactive at that moment.

Here is the github link if you want to look into the code or host the bot yourself: Github

Future plans

  1. Make a discord version
  2. Duel on problems from specified sources (IOI, BOI etc).
  3. Duel with three or more users
  4. Add rating system

Thanks to _blaNk_ for helping me test the bot. If you find any bugs/issues, let me know.

Full text and comments »

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