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

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

Hi chat!

TeamsCode Summer 2025 has concluded, thank you as always to the setters and organizers :]

Announcement here: https://codeforces.me/blog/entry/145231

The official broadcast will discuss some solutions here so you should definitely check that out as they will explain the problems better than I do, but since the discord channels are all locked and nothing has been started yet I figured I'd post the unofficial editorial I wrote while testing.

A lot of my explanations are needlessly wordy and I consider myself a very bad editorialist but I hope it is helpful anyway.

https://www.overleaf.com/read/fmnvspbmcpss#6226b5 Doink

I hope you all enjoyed the contest (I certainly did, especially since it will be the last competitive programming contest I help out with / participate in before college). hope they let me test again next time XD

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

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

Auto comment: topic has been updated by omeganot (previous revision, new revision, compare).

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

Thank you so much for writing this! Your explanations are not overly wordy at all. I appreciate the extra detail.

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

holy moly omeganot

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

I will share the slightly different solution for problem I (Advanced Division) that AlesL0 and I came up with.

Let's consider a prefix of the query range of length $$$K$$$ with distinct elements, where the maximum element is $$$M$$$. The answer for that prefix is therefore $$$M - K$$$. If I extend this prefix by one position by adding element $$$i$$$, three things can happen:

  • If I find a duplicate element, I'm done because I can't extend any further.
  • If $$$a_i \lt M$$$, the answer decreases by $$$1$$$.
  • If $$$a_i \gt M$$$, the answer increases and becomes $$$a_i - (K + 1)$$$. This happens when the minimum of the prefix changes.

So I'm interested in the local minima of the answer before the maximum of the prefix changes.

To do this, I can consider the tree in which each element is the child of the next greater element. The weight of the edge is the value of the answer (without considering the query offset) in the local minimum before going to the parent.

By performing binary lifting on this tree, it is possible to answer queries online.

To check for duplicates, it is enough, for example, to have a sparse table that tells the minimum index of the next equal element for a range.

Submission code here

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

    Very cool! It's cool that the monotonic structure also allows for this sort of interpretation.

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

    That's a cool online solution!

    I also solved the problem online during the contest, using a different approach.

    Since the problem asks us to answer range queries, we naturally think about using a segment tree and storing minimum values of prefix max minus index.

    The issue is that it is somewhat hard to directly merge nodes $$$c$$$ and $$$d$$$ when the subtree of $$$d$$$ is split into two sets:

    • The set with prefix max $$$\leq$$$ the max of subtree $$$c$$$
    • The set with prefix max $$$\gt$$$ the max of subtree $$$c$$$

    We can resolve this issue by walking down the subtree of $$$d$$$, carefully updating the minimum in our merged node while descending to one of the children based on the maximum in the left subtree.

    To answer a query, we need to make sure we combine segment tree nodes from left to right. We can deal with the duplicate restriction using a sparse table or segment tree to bound the right endpoint of our query.

    This approach also supports updates to the array $$$a$$$ in $$$O(\log^2 n)$$$.

    Complexity is $$$O(n \log n + q \log^2 n)$$$.

    Submission code

    This problem is somewhat similar to CTT 2012 Day 1 A.

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

As a tester, here is a different solution to stones:

Note that we can always some operations to do the following $$$2$$$ moves.

1) add ($$$-1$$$, $$$-1$$$) to $$$i$$$ and ($$$+1$$$, $$$+1$$$) to $$$j$$$, $$$i \neq j$$$.

2) If you have $$$\ge n - 2$$$ white and $$$\ge n - 2$$$ black stones at at least one other stone of any color in a pile we can add $$$(-(n - 2), -(n - 2))$$$ to this pile.

It makes sense that we would want to stack as many pairs of opposite colored stones in one pile and then use the second move as much as we can.

Let $$$c$$$ be $$$\sum_{i=1}^n \text{min}(b[i], w[i])$$$. We can increase $$$c$$$ further by doing a move we subtract each pile of black or each pile of white by $$$1$$$ as long as they are all positive.

Note that at this point each pile has either a positive number of black or white stones and then we can pretend we set all the $$$c$$$ pairs on the side.

However, it may be optimal do more type $$$1$$$ or type $$$2$$$ operations to increase $$$c$$$ before we subtract $$$n - 2$$$ as many times as possible.

When we increase the entire array in a type $$$1$$$ operation, $$$c$$$ increases by the amount of piles with positive black stones minus $$$1$$$ and then each of these piles decrease in size by $$$1$$$ (and similar logic for type $$$2$$$ operation but with white stones).

Its never optimal to do both extra type $$$1$$$ and type $$$2$$$ operations because at most you can get $$$n - 2$$$ extra pairs and they "cancel" out.

We can simulate this in $$$O(n \cdot (w + b))$$$.

However, we can notice that we don't need to simulate it directly and it only depends on the number of positive elements in the side we are doing extra operations to subtract from and the total number of black and white stones.

We can sort the array to find out when the number of positive numbers changes and maintain the sums as we do more extra operations.

We can use this to speed up the simulation to $$$O(n \log n + w + b)$$$

To speed this up further we can consider every interval where the number of positive piles is the same. Call this $$$k$$$. At every step the sum of the array will change by $$$n - 2 \cdot k$$$ and the number of pairs increases by $$$k - 1$$$.

Because the pairs are taken mod $$$n - 2$$$ we can't just check the first and last values of the interval. However, it is enough to check the first and last $$$\frac{2n}{k}$$$ in order to find the global minimum (on this segment). This is fast enough because $$$\sum_{i=1}^n \frac{n}{i} \approx n \log n$$$

Since there are only $$$n$$$ different possible values of $$$k$$$ the total time complexity is $$$O(n \log n)$$$.

Code: https://pastebin.com/DYLEYN37

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

    very orz

    fun fact: during testing he was trying to think of a solution in this path but i interrupted him and asked him if he wanted to work together and then proceeded to force the invariant method down his throat