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

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

Hi!

I didn't find any blog post about the round. So, let's discuss it. I'm a little bit upset, because I didn't solve C. Actually, on last minutes I wrote the solution which should pass if replace linear searches with binary.

Am I right that now Google doesn't publish tests?

P.S. And eatmore is right. The standings are completely unusable (

Теги gcj, 2019, 1b
  • Проголосовать: нравится
  • +226
  • Проголосовать: не нравится

»
6 лет назад, # |
  Проголосовать: нравится -7 Проголосовать: не нравится

My submission button went grey from blue when I just clicked on it in the last minute of the contest.

Totally pissed off. I think the server got overloaded. Or is there any reasonable explanation for this?

»
6 лет назад, # |
  Проголосовать: нравится +8 Проголосовать: не нравится

How to solve C?

I used something which looks like Segment tree beats and it passed.

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

    can you please explain your solution?

    • »
      »
      »
      6 лет назад, # ^ |
      Rev. 2   Проголосовать: нравится +8 Проголосовать: не нравится

      I will try, but for me it is hard to explain:

      let's solve the problem when the right end of the segment is equal to i.

      Then answer will be sum of these values.

      What happens with maximum when we end new element to the end of current array:

      [5,2,1] and we add 4 -> [5,4,4,4] maximum changes on some segments.

      [5,4,3] and we add 1 -> [5,4,3,1].

      Next what we want it calculates the number of positions in the arrays A,B:

      when values differ at most K.

      So what, we need:

      change value on segment in one of the arrays

      calculate the total number of positions, where values differ at most k.

      First, it is easy to do with the segment tree

      second, it is harder, but we can do still segment tree, but where we should stop.

      Either value in subtree didn't fully intersect or intersect fully. (Like in segment tree beats, if you can to stop, just stop here). Otherwise we need go deeper and recalculate.

      Other it is just written segment tree with lazy propagation.

      Code

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

    I solved it with divide and conquer.

    • »
      »
      »
      6 лет назад, # ^ |
        Проголосовать: нравится +5 Проголосовать: не нравится

      Please share the idea.

      • »
        »
        »
        »
        6 лет назад, # ^ |
          Проголосовать: нравится +12 Проголосовать: не нравится

        We divide [s,e] segment into two segments [s,m] and [m+1,e], where m = (s + e) / 2. We iterate first segment and fix left side of the semgent. Then we must solve 4 cases.

        1. When max_A of the segment is in the left side and max_B of the segment is in the left.
        2. max_A -> left , max_B -> right
        3. max_A -> right , max_B -> left
        4. max_A -> right , max_B -> right

        After that we only need binary searchs.

        It works O(N*logN*logN).

        Code

        • »
          »
          »
          »
          »
          6 лет назад, # ^ |
          Rev. 2   Проголосовать: нравится 0 Проголосовать: не нравится

          i dont understand the idea 2 question

          why do we need to determine the position of max_A,max_B?

          why do you use binary search ? i see the sample input, the input is unsorted(example: 0 8 0 8 0)

          thank you

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

            For example 1st case. If our segment is [s,e] , we want to count the number of [l,r] segments , such that l>=s && l<=m && r>m && r<=e. If we fixed l and said that max_A of [l,r] in the left side from [l,m] ,then we can use binary search for finding the bound for r. We do the same for max_B.

  • »
    »
    6 лет назад, # ^ |
      Проголосовать: нравится +18 Проголосовать: не нравится

    I used sqrt-decomposition with time complexity $$$O(N^{1.5} \log N)$$$.

    First, for a fixed $$$R$$$, we define $$$v_L = \min(A_L, A_{L+1}, A_{L+2},..., A_R) - \min(B_L, B_{L+1}, B_{L+2},..., B_R)$$$. When $$$R$$$ increases by $$$1$$$, some value of $$$v$$$ changes, but the overall change is mostly composed with few range-add. Actually, the overall number of range-adds is $$$O(N)$$$.

    So, we can transform into the following problem:
    You are given $$$N$$$, and $$$Q$$$ queries. The array $$$c_1, c_2, ..., c_N$$$ is initially all-zero, and there are two types of queries.
    Type 1: You are given $$$l, r, x$$$. Increase $$$c_l, c_{l+1}, ..., c_r$$$ by $$$x$$$.
    Type 2: You are given $$$a, b, r$$$. Calculate the number of $$$i \ (i \leq r)$$$ which is $$$a \leq c_i \leq b$$$.
    This query can be answered in $$$O(\sqrt{N} \log N)$$$ when we do sqrt-decomposition of array.
    In this problem, $$$a = -K$$$ and $$$b=K$$$. And also, as stated above, $$$Q = O(N)$$$. Hence, the overall time complexity is $$$O(N^{1.5} \log N)$$$.

  • »
    »
    6 лет назад, # ^ |
      Проголосовать: нравится +3 Проголосовать: не нравится

    My solution counts the number of intervals $$$[L, R]$$$ s.t. $$$max(a[L, R]) < max(b[L, R]) - K$$$. For each $$$i$$$ let $$$[u, v]$$$ be the interval in which $$$b[i]$$$ is maximal. We'll take the smaller interval between $$$[u, i]$$$ and $$$[i, v]$$$. Assuming it's $$$[u, i]$$$. Let's iterate $$$j$$$ over $$$[u, i]$$$ and find the largest $$$k$$$ s.t. $$$max(a[j, k]) < b[i] - K$$$. Subtracting $$$k - i + 1$$$ from the answer.

»
6 лет назад, # |
  Проголосовать: нравится +23 Проголосовать: не нравится

My clock on my phone is 3 minutes ahead, when I entered to a contest at 18:28 the countdown timer wasn't shown, probably they take time from device on client side?

»
6 лет назад, # |
  Проголосовать: нравится +8 Проголосовать: не нравится

How to solve B ? (2nd subtask)

  • »
    »
    6 лет назад, # ^ |
      Проголосовать: нравится +3 Проголосовать: не нравится

    Find R1 and R2 using some value which gives strictly increasing value (I use 50 as 2^50*R1 > 2^25*100(as 100 can be the max value of any R)). So, we get R1 and R2.

    Now, use some value which give increasing sequence for R3 and R4 as well (I use 168). After that we have two eqn and two variables. So, just solve for R5 and R6!

  • »
    »
    6 лет назад, # ^ |
      Проголосовать: нравится +53 Проголосовать: не нравится

    Here's my solution.

    First, write down a table of $$$[n/i]$$$ for various $$$n$$$ and $$$i$$$ from $$$1$$$ to $$$6$$$. I picked two lines:

      n   n/1 n/2 n/3 n/4 n/5 n/6
     55:   55  27  18  13  11   9
    185:  185  92  61  46  37  30
    

    Now, when we ask for day 185, we get the value $$$v_{185} = (r_1 \cdot 2^{185} + r_2 \cdot 2^{192} + r_3 \cdot 2^{61} + r_4 \cdot 2^{46} + r_5 \cdot 2^{37} + r_6 \cdot 2^{30}) \bmod 2^{63}$$$. Everything after power $$$63$$$ is lost. Note that $$$r_i$$$ are from $$$0$$$ to $$$100$$$, so when we have at least $$$7$$$ powers of two between consecutive values, we can just restore them directly. Thus

    r6 = (v185 >> 30) & 127;
    r5 = (v185 >> 37) & 127;
    r4 = (v185 >> 46) & 127;
    

    The second question will be about $$$v_{55} = (r_1 \cdot 2^{55} + r_2 \cdot 2^{27} + r_3 \cdot 2^{18} + r_4 \cdot 2^{13} + r_5 \cdot 2^{11} + r_6 \cdot 2^{9}) \bmod 2^{63}$$$. Here, note that $$$r_4$$$ influences the bits of $$$r_3$$$, $$$r_5$$$ can influence $$$r_4$$$, and $$$r_6$$$ can influence $$$r_5$$$. But as we know them already, calculate $$$v_{55}' = v_{55} - r_4 \cdot 2^{13} - r_5 \cdot 2^{11} - r_6 \cdot 2^{9}$$$. Now the restoration is straightforward:

    r3 = (v55p >> 18) & 127;
    r2 = (v55p >> 27) & 127;
    r1 = (v55p >> 55) & 127;
    
  • »
    »
    6 лет назад, # ^ |
      Проголосовать: нравится +13 Проголосовать: не нравится

    Number of rings after day $$$d$$$ is $$$\sum_{i=1..6}R_i2^{x_i}$$$ where $$$x_i=\lfloor{d/i}\rfloor$$$. If $$$2^{x_5}\cdot maxR < 2^{x_6}$$$ then no numbers overlap in the binary representation of the sum and we can read all $$$R$$$s that fit in $$$2^{63}$$$. So at $$$d=210$$$ (lowest solution of above inequality) we can read $$$R_4$$$, $$$R_5$$$, $$$R_6$$$ directly. Once we know them, we ask the same condition $$$2^{x_2}\cdot maxR < 2^{x_3}$$$ for the next $$$R$$$s and the solution $$$d=42$$$ gives $$$R_1$$$, $$$R_2$$$ and $$$R_3$$$ directly after subtracting the known sum of $$$R_42^{x_4}+R_52^{x_5}+R_62^{x_6}$$$.

  • »
    »
    6 лет назад, # ^ |
      Проголосовать: нравится +36 Проголосовать: не нравится

    i'm old man. 54 years old.

    when i read it i thought wtf how second problem can be so hard. i looked at the scoreboard and my place was 220. that's why my idea was the following: print 220 and 54.

    When you print 220, you get $$$2^{55} \cdot r_4 + 2^{44} \cdot r_5 + 2^{36} \cdot r_6$$$. $$$r_i <= 100$$$, so you need maximum $$$7$$$ bits to store such a number. That's why representations of $$$r_4$$$, $$$r_5$$$ and $$$r_6$$$ are non-intersecting.

    Printing 54 use the idea above for $$$r_1,r_2,r_3$$$, knowing $$$r_4,r_5,r_6$$$ and subtracting them from input given.

»
6 лет назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

Two lines missed to pass B :(

I've asked 56th day and 104th. r1 and r2 can be deduced from Ans_56, $$$r_1 = ans \div 2^{56}, r2 = (ans - 2^{56}r_1) \div 2^{28} $$$

After that just iterate all possible r3, r4, r5, r6. This gets TL, but passes if you check if current r3 is even possible (i.e. if we set all r4,r5,r6 to either zeros or 100. Ans_56 should be between those two numbers).

  • »
    »
    6 лет назад, # ^ |
      Проголосовать: нравится +3 Проголосовать: не нравится

    There's an easier solution to B. Take the first query to be 56, you can get r1 and r2 as you said. Take the next query to be 170. Since 2^170 and 2^85 are greater than 2^63, they'll get wiped out after mod. Using the above technique like for 56, you can find r3 and r4. Now for r5 and r6 you can simply use the 2 above queries and solve the system of 2 linear equations using Cramer's rule.

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

    If I understand your approach correctly, multiple pairs of values for r3,4... can satisfy the equation

    • »
      »
      »
      6 лет назад, # ^ |
        Проголосовать: нравится +5 Проголосовать: не нравится

      Bruteforce over all 10^8 options shows that solution is unique (you can ignore r1, r2).

      That what was rather unfortunate as I expected plain 10^8 to pass TL, but apparently it didn't.

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

        How do you differentiate between $$$r_5=0, r_6=4$$$ and $$$r_5=1, r_6=0$$$? Both lead to the same value on day 56 or not? ($$$r_5 * 2^{11} + r_6 * 2^{9}$$$)

»
6 лет назад, # |
Rev. 4   Проголосовать: нравится 0 Проголосовать: не нравится

Can anyone help me with my submission for B1 ?

I've simply asked $$$63 \cdot i$$$ for $$$i$$$ in $$$6..2$$$ and tried to deduce numbers one by one because after $$$63 \cdot (6 - i)$$$ all numbers except for last $$$i$$$ are nullified because of $$$2^{63}$$$ modulo.

For some reason one of numbers ended up being above 100 (debug assertions showed that) but I can't really understand how could this be given tests are correct and there is only one solution.

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

    You should read $$$w$$$ after $$$T$$$ and judge's response after writing your answer

    • »
      »
      »
      6 лет назад, # ^ |
      Rev. 2   Проголосовать: нравится 0 Проголосовать: не нравится

      Noticed it right after writing this comment, but this didn't really help unfortunately, this still ends up in a wrong answer =(

      Oh... Right. Also read W. Yeah thank you.

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

    You forgot to input w :/

»
6 лет назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

How to solve C?

»
6 лет назад, # |
  Проголосовать: нравится +8 Проголосовать: не нравится

How to solve A for Q=10^5 ?

  • »
    »
    6 лет назад, # ^ |
    Rev. 2   Проголосовать: нравится 0 Проголосовать: не нравится

    Compress the coordinates. There will be up to P + 2 different coordinates for each axis. Then you can select the best one among all intersection in the compressed grid.
    Code

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

    The problem is independent for each coordinate.

»
6 лет назад, # |
Rev. 2   Проголосовать: нравится 0 Проголосовать: не нравится

Can anyone help me with my submission for A?

I sorted the north,south,east and west arrays. Then, for each value in north, I applied binary search for value greater than that in south and updated the result. I did the same for east and west.

I can't understand what's wrong.

»
6 лет назад, # |
  Проголосовать: нравится +10 Проголосовать: не нравится

I solved C using sets only.

Include elements from greatest to smallest and try to solve how many segments you can use such that each element inserted (in order) is the smallest. You can maintain 4 or 6 sets with elements that are within difference k, elements that have more than k difference and elements in general. The idea is that there'll be limits to the right/left where the first element greater but within the wanted difference from the other array, to the right/left where elements from the same array were included and to the right/left where elements from the other array have difference greater than k (and were included/are greater). With these limits you can use inclusion-exclusion Code

  • »
    »
    6 лет назад, # ^ |
      Проголосовать: нравится +20 Проголосовать: не нравится

    One set, no binsearch, no inclusion-exclusion, code.

    We subtract numbers of bad intervals by passing through all possible skill values. Now it works for $$$O(\max(c_i,d_i)\cdot\log{n})$$$, but can be easily reduced to $$$O(n\cdot\log{n})$$$ by compressing the skill values (but why should one do it).

    • »
      »
      »
      6 лет назад, # ^ |
      Rev. 2   Проголосовать: нравится +18 Проголосовать: не нравится

      For each $$$i$$$, I counted the number of segments that have the leftmost maximum of $$$C$$$-values at index $$$i$$$. Using binsearch, we find the following:

      • rightmost index $$$l_c < i$$$, s.t. $$$C[l_c] \geq C[i]$$$ (or $$$-1$$$)
      • rightmost index $$$l_b \leq i$$$, s.t. $$$D[l_b] > C[l_b] + K$$$
      • rightmost index $$$l_d \leq i$$$, s.t. $$$D[l_d] \geq C[l_c] - K$$$

      Similarly on the other side we get $$$r_c$$$, $$$r_b$$$, $$$r_d$$$. Now, a good interval with leftmost maximum at $$$i$$$ satisfies the following conditions:

      • $$$\max(l_c, l_b) < l \leq i $$$,
      • $$$i \leq r < \min(r_c, r_b)$$$, and
      • either of $$$l \leq l_d$$$, $$$r_d \leq r$$$

      Thus, the answer can be found in $$$\mathcal{O}(n \log n)$$$ with a constant range max query, binary search and few ifs and multiplications.

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

      Can you explain this approach more?

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

        We calculate the number of bad segments instead of good ones. Then we consider two cases: where $$$\max\limits_{l\leq i\leq r}c_i > \max\limits_{l\leq i\leq r}d_i$$$ (and hence $$$> \max{d_i} + k$$$) and the contrary. Let's say that $$$\max{c_i} > \max{d_i}$$$ (the other case is similar). We try each possible value of $$$\max{d_i}$$$ (and this is where we can reduce the complexity but we don't) from $$$0$$$ to $$$10^5$$$.

        For each $$$x$$$ we look at the set of indices $$$i$$$ where both $$$c_i\leq x$$$ and $$$d_i\leq x - k - 1$$$ and at the set of indices where $$$c_i< x$$$ and $$$d_i \leq x - k - 1$$$. The number of bad segments where C used exactly sword with skill $$$x$$$ and D was much worse equals the number of subsegments of the first set minus the number of subsegments of the second set. One can maintain this set as well as the number of subsegments in a structure with one set of subsegments.

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

          Thanks, I was able to use this approach with DSU in python to get a O(N alpha(N)) soln.

»
6 лет назад, # |
  Проголосовать: нравится +24 Проголосовать: не нравится

If anyone wants to filter standings by country here you go

kudos to this person. I just parsed new data and copied his template.

»
6 лет назад, # |
Rev. 2   Проголосовать: нравится 0 Проголосовать: не нравится

For C, I was thinking along the following lines.

For each C[i], find subarray where it will be the maximum element. Let that range be L[i], R[i] such that for any L[i]<= l <= i and i<=r<= R[i] , C[i] is maximum in [l,r]

For D, maintain a segment tree for range maximum queries.

Now, for each 'i' in C[i], having found L[i] and R[i], we restrict these values based on feasible values for D. For that we can do binary search to find out the nearest indices to 'i' where Max of subarray of D ending at 'i' becomes greater than or equal to c[i]-k and becomes greater than c[i]+k to have a range for l. Similarly compute range for r with binary searches on subarrays starting at 'i'.

Finally add to answer sizeof(possible_l_values)*sizeof(possible_r_values). Do this for each 'i'.

Time complexity : O(N*logN*logN)

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

    Ofcourse a sparse table on array D would reduce the extra logN for range max queries to get this down to O(N*logN)

»
6 лет назад, # |
  Проголосовать: нравится +35 Проголосовать: не нравится

Problem C and $$$O(N^2)$$$: https://ideone.com/uJ2Sel

»
6 лет назад, # |
Rev. 2   Проголосовать: нравится 0 Проголосовать: не нравится

For some reason I thought my A large submission would pass with a $$$O(Q)$$$ solution, but got TLE. Found out that if I compressed some loops and changed to Python 2, it got accepted after the contest was over :(

Also figured out the solution for B large 2 minutes before the end, gotta work on my time limits and fast coding for next round.

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

Slightly different solution to C to those I've seen. As usual, we want to find, for each i, the biggest interval over which $$$c_i$$$ is the (left-most) maximum in $$$c$$$, and for which all elements in $$$d$$$ are less than $$$c_i - K$$$. However, I don't use a range-max query structure.

Let's find just the left end of the interval. Sweep left to right, maintaining a Pareto front for each fighter (i.e. elements which are >= than any subsequently seen elements). After adding $$$c_i$$$, we can immediately read off the next larger value to the left, which gives us half the answer; and we can binary search the Pareto front of $$$d$$$ to find the rightmost element >= $$$c_i-K$$$, which is the other bound on the interval, and we then take the larger of the two bounds.

To get the right ends, we can just run the sweep in the opposite direction. A little care is needed to correctly deal with ties correctly.

The binary search makes this $$$O(n \log n)$$$. I haven't tried to implement it yet, but I suspect the running time could be reduced to O(n) by combining $$$c$$$ and $$$d+K$$$ into one Pareto front.

  • »
    »
    6 лет назад, # ^ |
      Проголосовать: нравится +3 Проголосовать: не нравится

    I've implemented my O(n) idea and it works — although again, care needs to be taken to handle ties correctly.

»
6 лет назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

Problem A made me realize how helpful it is, that CF problems usually have pictures for explaination. Took me 30 minutes until I figured why the first test case failed

»
6 лет назад, # |
Rev. 2   Проголосовать: нравится 0 Проголосовать: не нравится

I solve C by maintaining a segment tree that store indices of the leftmost maximum element instead of the maximum element's value so that I can avoid ties (i.e I will take into consideration the leftmost value in case of ties). Here is link to code. But I am not able to figure out how I can solve using maintaining segment tree of maximum values in case of ties. Any help?