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

Автор giorgosgiapis, 8 лет назад, По-английски

Second round of COCI 2017/2018 season will take place this Saturday at 14:00 UTC.
You can find the full schedule for this season here and register for the contest here.

Let's discuss the problems here after the contest.

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

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

Contest starts in less than 15 minutes!

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

Very interesting problems)
How to solve E (Usmjeri) and F (Garaža)?

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

    Root the tree. Let path a - b be p1 = a, p2, p3, ..., pk, ..., pt = b where pk is their lca. If k = 1 or k = t then all edges (pi, pi + 1), (1 ≤ i < t) must be either all directed down the root or all up the root. Suppose now this is not the case, then the edges (pi, pi + 1), (1 ≤ i < k) must have the same direction (down or up the root) and also the edges (pi, pi + 1), (k ≤ i < n) must have the same direction while the edges (pk - 1, pk) and (pk, pk + 1) must have different directions so now you obtained a simple 2-sat problem for which you can find if there is a solution in O(n) and the number of solutions is 2number of conex components in the 2 - sat graph.

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

    hint for F (it can be solved with different data structures after that):

    Imagine we change the value of position i. Let's find the leftmost position j such that gcd(j, i) = ai (here gcd(l, r) is the GCD of the range al, al + 1, ..., ar). Now we know that the gcd(j - 1, r) will decrease. Well let's find the leftmost position j2, such that gcd(j2, i) = gcd(j - 1, i).

    If we do this until gcd(jk - 1, i) = 1 how many such ranges can we have in worst case?

    Well .

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

      I already knew that fact. But I have no idea what to do next.

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

        Well if we had no updates we could solve the problem like that:

        For every position i find rightmost position ri such that gcd(i, ri) > 1.

        Now the answer for a query [L;R] is ( (sum of r values in [L;R]) — (sum of r values in [L;R] greater than R) + (count of r values in [L;R] greater than R) * RR * (R + 1) / 2 + (L * (L - 1) / 2)).

        Well this can be done with a merge sort tree in .

        Now how to handle updates?

        Well we go through the O(log(109)) ranges from the previous comment. Now with binary search in segment tree we find for each of the ranges the corresponding r value. You can see that this r value will be the same for all of the positions in a given range. Well now we simply set the values in the range to r.

        The data structure we will use will be merge sort tree with treap in every node, because using it the set operation of an interval can be done easily.

        The complexity will be .

        • »
          »
          »
          »
          »
          8 лет назад, скрыть # ^ |
          Rev. 4  
          Проголосовать: нравится 0 Проголосовать: не нравится
          You can see that this r value will be the same for all of the positions in a given range.

          How to understand this?

          UPD Understood. Thanks. But I guess treap is too heavy and, probably, a simpler solution exists.

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

            Well time limit is 4 sec so this should fit in TL without a problem.

            Another possibility is to use SQRT decomposition (if you don't like treaps :D) and get a solution which also should pass if implemented well.

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

              I think your solution is too complex. With one more observation, you can simplify your solution a lot.

              Suppose, f(i) = maximum index j, such that gcd[i, j] > 1. Note that, f(1) <= f(2) <= ...

              So for a query, [L, R], you can binary search and find, largest i (L <= i <= R) such that, f(i) <= R. Then rest is just application of simple segment tree.

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

      How can you calculate j1, j2, ...? I cannot find a suitable time complexity to solve this. I only know a O(log(109) * log(N)2) solution per update by using a segment tree of GCD and binary search. However, it seems that such a solution is not fast enough to pass the testcases.

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

        OPS I explained how to find the closest position to the right, not to the left, but the idea to find the one to the left is basically the same.

        You do the binary search inside the segment tree:

        Let's get the nodes in the segment tree which the query [i;N] will cover. They will obviously be at most . Now we begin from the leftmost such interval and go through these nodes.

        If the interval of the current node is [L;R] we can easily find gcd(i, R) in O(1) using the gcd values of the previous intervals.

        We find the first interval, such that gcd(i, R) = 1. This will be done in time.

        Well now we know that the position which we are searching for (current j) is in a given interval [L;R] (we also know the number of this node in the segment tree).

        Well we just start going down from our current node:

        If the GCD of the current gcd and the gcd of the left child is not 1, we go to the right child with the new gcd. Else we go to the left child with the same gcd.

        Using this in the end we will be at the first position x with gcd(i, x) = 1. The complexity will be .

        So overall complexity for finding the closest position to the right of a given position will be .

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

    In Problem E I found solution with HLD. But i haven't coded HLD before. So i can't.

    My Solution

    Am i wrong ? Sorry for my bad English :D

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

Yeah, nice problems

How to solve C? I solved D with meet-in-the-middle but not C :P

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

How to solve D? I guess it is solved using meet-in-the middle but couldn't figure out the solution.

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

Approximately when the results will be posted?