arthur_9548's blog

By arthur_9548, history, 16 months ago, In English

Hey everyone! I hope everyone enjoyed the problems of VI UnBalloon Contest Mirror. This editorial contains the description of the solutions and their implementation. Feel free to discuss them in the comments!

Problem A

Solution
Code

Problem B

Solution
Code

Problem C

Solution
Code

Problem D

Solution
Code

Problem E

Solution
Code

Problem F

Solution
Code

Problem G

Solution
Code

Problem H

Solution
Code

Problem I

Solution
Code

Problem J

Solution
Code

Problem K

Solution
Code

Problem L

Solution
Code

Problem M

Solution
Code

Problem N

Solution
Code
  • Vote: I like it
  • +29
  • Vote: I do not like it

»
15 months ago, hide # |
 
Vote: I like it +13 Vote: I do not like it

The problem N can be solved in O(n + q) using a prefix sum. If an interval l, r is a permutation of m elements (m = r — l + 1), its sum will be m(m+1)/2, no other set of m numbers apart from the permutation holds the its sum is m(m+1)/2.

  • »
    »
    15 months ago, hide # ^ |
     
    Vote: I like it +5 Vote: I do not like it

    That is a very nice solution as well! Very elegant and simple.

  • »
    »
    12 months ago, hide # ^ |
     
    Vote: I like it 0 Vote: I do not like it

    There's a different nice randomized solution in $$$O(n + q)$$$ as well that works even if the original array wasn't a permutation, shared in this blog.

    TLDR: Gives a random integer to each number and precompute the XOR of these integers for each permutation. Now for each query, answer if range XOR equals the expected XOR of the permutation.

    Submission: 334965237

»
15 months ago, hide # |
 
Vote: I like it +8 Vote: I do not like it

I have another solution for problem C. The problem essentially asks for 3D range product queries modulo $$$m$$$. Since m is not prime, we can't divide directly and can't use prefix products. Or can we?

$$$m$$$ can be factored as $$$p_1^{e_1} p_2^{e_2} ... p_n^{e_n}$$$ where $$$n \lt 7$$$ as the power of the first seven primes exceeds $$$10^5$$$. The trick is that any number $$$x$$$ can be written as $$$k \cdot p_1^{b_1}p_2^{b_2} ... p_n^ {b_n}$$$, where $$$gcd(k, m) = 1$$$ — we store the powers of primes in $$$m$$$ separately. This representation easily supports multiplication and division under mod $$$m$$$. Since $$$gcd(k, m) = 1$$$, we can always find $$$\frac{1}{k} \mod m$$$ and for prime powers multiplication/division is just addition/subtraction of exponents.

This almost works, but we can have 0 entries in the matrices. Fortunately, this can be handled easily by maintaining the number of zeroes in the number, or representing $$$x$$$ as $$$k \cdot p_1^{b_1}p_2^{b_2} ... p_n^ {b_n} 0 ^ z$$$ using the convention $$$0^0 = 1$$$.

One way to implement this is to use an array<int, 8> for each entry of the prefix product. $$$m$$$ is pretty small here, so we can store the normalized representation for each modulo.

It's important to note we don't need to worry about roots, the exponents $$$b_i$$$ will never be negative here.

To improve queries, we can precompute powers of each prime factor of $$$m$$$. The maximum exponent is roughly $$$H \cdot W \cdot T_{max} \cdot \log(m)$$$.

»
15 months ago, hide # |
 
Vote: I like it +8 Vote: I do not like it

Problem N can also be solved in $$$O(n+q)$$$ using the editorial's solution without sorting.

There is at most one good range for each size, so we can keep an array where the $$$i$$$-th entry is the good range of size $$$i$$$. To answer the query, we just check if the range is the correct one for that size.