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

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

2002A — Distanced Coloring

idea & solution: xcyle

Hint 1
Hint 2
Tutorial
Solution

2002B — Removals Game

idea & solution: xcyle

Hint
Tutorial
Solution

2002C — Black Circles

idea: Flamire, solution: le0n

Hint
Tutorial
Solution

2002D1 — DFS Checker (Easy Version) and 2002D2 — DFS Checker (Hard Version)

idea & solution: xcyle

Hint
Tutorial
Solution (Check 1)
Solution (Check 2, LipArcanjo)

2002E — Cosmic Rays

idea: le0n, solution: Flamire

Hint 1
Hint 2
Tutorial
Solution
Solution (priority_queue)

2002F1 — Court Blue (Easy Version)

idea: Flamire, solution: le0n

Hint 1
Hint 2
Tutorial
Solution

2002F2 — Court Blue (Hard Version)

idea: le0n, solution: xcyle

Hint
Hint (alternate version)
Tutorial
Solution
Solution (dfs)

2002G — Lattice Optimizing

idea & solution: xcyle

We apologize for unintended solutions passing, and intended solutions failing with large constants. Brute force runs very fast on $$$n=18$$$, which forced us to increase constraints.

Hint 1
Hint 2
Tutorial
Solution
Solution (trie, LipArcanjo)

2002H — Counting 101

idea: le0n, xcyle, solution: le0n, xcyle

Hint 1
Hint 2
Hint 3
Tutorial
Solution (orzdevinwang)
  • Проголосовать: нравится
  • +125
  • Проголосовать: не нравится

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

Woah, I solved A,B,C all through guessing and became purple!

»
3 месяца назад, # |
  Проголосовать: нравится -15 Проголосовать: не нравится

Why this solution of mine for C is giving WA on test5? 275844339

  • »
    »
    3 месяца назад, # ^ |
      Проголосовать: нравится +6 Проголосовать: не нравится
    1. Check for integer overflow

    2. sqrt can cause precision loss, i recommend you eliminate it completely

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

      how will we check then?

      My approach : If after distance(d) time, if our destination points becomes part of any of the circle then the answer is NO otherwise YES.

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

        you can just compare the squared distance, you end up squaring it again after taking the square root anyways

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

lol, you can solve D1 in $$$O(N*Q)$$$ with pragmas: 275797240

Also you can use segment tree too for E: 275831791

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

Can someone try hacking my solution to E? I solve for each value separately and use RMQ to keep track of merging blocks, but I believe my code is O(N^2 log N) if one value appears lots of times, the first and last occurrence of that value have high a_i, and all intermediate occurrences have very low a_i. 275860315

»
3 месяца назад, # |
  Проголосовать: нравится -29 Проголосовать: не нравится

Only solve A, B.

I am too weak. T^T

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

In problem D2 check 1, can someone explain how the merge step works? ("merge the subtree of u into a large node with size siz_u")
I don't understand why it is sufficient to just maintain "bad" children, instead of maintaining information for the entire subtree.

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

    Let's note the following $$$pos_x$$$ : position of $$$x$$$ in $$$p$$$

    $$$sub_x$$$ subtree size of x

    For D1 consider some node $$$x$$$ , we call node $$$x$$$ valid if the positions of its children in the permutation are $$${pos_x +1 , pos_x + t + 1}$$$ where t is the subtree size of one of the children.

    A dfs order is valid iff all nodes are valid and $$$p_1 = 1$$$

    So we can maintain a set for bad nodes $$$bad$$$ When we swap two values $$$p_x , p_y$$$ it only affects $$$p_x , p_y$$$ and their parents (because $$$pos_{p_x}$$$ becomes $$$y$$$ and vice versa)

    So we can check easily in D1

    For D2 you should maintain a set {$$$pos_{child} , child $$$} for each node $$$x$$$ . Node $$$x$$$ is valid iff (*) for each two adjacent positions of children $$$c_i$$$ , $$$c_{i+1}$$$ in the set $$$pos_{c_{i+1}} - pos_{c_i} = sub_{c_i}$$$ and $$$pos_{c_1} - pos_x = 1$$$ Thus we can maintain also a set of bad children (who don't satisfy (*)) in each node

    And when we update we only remove and insert new values in the set

    Finally if the set of bad children of a node is empty than erase this node from $$$bad$$$ otherwise insert it.

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

    I suppose, this part is just about proving that subtree dfs log places next to the parent node dfs log. So there's no merging part when talking about solution.

    Actually, I don't understand how to keep tracking "bad" nodes with sets on D2. I would be glad if someone would explain this part. Can't understand author's code ideas.

    P.S.: Sorry, don't refresh the page too long , thaks for explaining!

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

the image in c's tutorial is not visible

»
3 месяца назад, # |
Rev. 3   Проголосовать: нравится +1 Проголосовать: не нравится

Why this solution of mine for D1 is giving TLE on test9 and it's O(n*q)? My Solution

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

I am really dumb .

But Why CD = AD in tutorial of C . I don't understand that

could anyone explain that pls.

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

    I think that CD = AD represent the point where we intersect with a circle since our starting point A has the same distance to D as does the circle's center C.

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

    understand it in a way that the speed of expansion of circles is same as walking speed of the guy and thus the circles radius at any instant would be same as the distance covered by the guy till that instant which implies AD=CD

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

Alternative (possibly wrong?) solution for D2:

  1. Firstly, check that for each $$$i$$$ from $$$2$$$ to $$$n$$$ $$$p_{i - 1}$$$ is the parent of $$$p_i$$$ except the case when $$$p_{i - 1}$$$ is a leaf.

  2. Also check that for each $$$i$$$ the position of $$$i$$$ in $$$p$$$ is greater than the position of its parent.

  3. Check that the multiset $$$S = \{ LCA(p_1, p_2), LCA(p_2, p_3), \cdots, LCA(p_{n - 1}, p_n) \} $$$ matches with such multiset of any valid DFS order (intuition: virtual trees). It can be easily checked by maintaining the multiset hash.

Here is my implementation: 275842859. Feel free to uphack it.

UPD. It seems that the second condition is unnecessary: 275927827. Now my solution looks similar to the Check 2 in the editorial as songhongyi pointed below (thanks for it). I think the first condition is a "weaker" version of Check 2 and the third condition makes my solution work somehow by making the first one "stronger". I still don't know how to prove it though.

UPD 2. Actually the second condition is necessary but the first one isn't. Now it seems less similar to Check 2.

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

    This solution is very similar to check2. I suspect it's correct and should be provable in a similar way.

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

    The check2 is actually equivalent to $$$p_1=1$$$ and $$$\operatorname{LCA}(p_i,p_{i+1})=fa(p_{i+1})$$$ forall $$$1\le i\le n$$$. Your third condition is actually $$$ \{ {\operatorname{LCA}(p_i,p_{i+1})} \} =\{fa(p_{i+1})\}$$$, which is obviously weaker than check2. But I'm sure it is equivalent to check2 with your second condition.

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

Could anyone write out the proof by induction in B? I'm not sure how to prove it...

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

    I didn't really understand the subarray thing in the editorial. The way I see it is:

    Suppose Bob can mimic Alice's move when there are $$$n$$$ elements. There are only two ways for this to happen (let $$$A = a_1 \dots a_n$$$, $$$B = b_1 \dots b_n$$$):

    • We have $$$a_1 = b_1$$$ and $$$a_n = b_n$$$. If Alice takes $$$a_1$$$ Bob can take the same element, i.e., $$$b_1$$$. If Alice takes $$$a_n$$$ Bob can take $$$b_n$$$.
    • We have $$$a_1 = b_n$$$ and $$$a_n = b_1$$$. If Alice takes $$$a_1$$$ Bob can take the same element, i.e., $$$b_n$$$. If Alice takes $$$a_n$$$ Bob can take $$$b_1$$$.

    If any of the above cases happen, nothing has changed in the game and we keep going, now with $$$n-1$$$ elements.

    If at any moment none of the cases above match, it means Alice has at least one endpoint element which Bob does not (let it be $$$x$$$). Bob cannot pick $$$x$$$ right after Alice, so he takes any one of his endpoint elements (let it be $$$y \neq x$$$). Now Alice can easily win by picking all her left elements except $$$y$$$, because at the end of the game, Alice will have $$$y$$$ left but Bob already deleted it, so the last elements cannot be equal.

    If the game keeps going with one of the two cases described initially, Bob can always mimic Alice's move, so Alice cannot win. These two cases can only happen when the whole arrays $$$A$$$ and $$$B$$$ are equal or one is the reverse of the other.

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

    I wish they had elaborated why the interval condition was "intuitive to see".

    Here is my proof. If there is a pair of neighbors in array A that are not neighbors in B then Alice wins. She just needs to remove the elements until only those two elements remain. Then after Bob's move his remaining two elements would not be the same as the two Alice's elements, and on the following move Alice can leave an element that Bob does not have. So, for Bob to win every pair of neighbors in A need to be neighbors in B. It is easy to show that it can only happen if B is equal to A or its reverse, depending on the order in B of the first two elements of A.

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

What is $$$fa$$$ mentioned in Problem-D Check 2?

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

My Insights for A,B,C

A
B
C
Fun Fact
  • »
    »
    3 месяца назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

    How did you come up with the conclusion of A? I turn to solve B/C instead.

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

      I tried to come up with something related about multiplication of numbers

      You can see the cases like this

      Cases Observation

      I think the observing the thing described in the spoiler is enough to come up with

      $$$\displaystyle \min(n,k) \times \min(m,k)$$$
»
3 месяца назад, # |
Rev. 4   Проголосовать: нравится 0 Проголосовать: не нравится

My idea for D1 was that it looks like a segment tree: Store in each node if that subtree makes sense or not, and the maximum index in that subtree, and of course the index of the node in the permutation. Then on update query, it takes at most O(log n) updates like a segment tree, by moving up and calling combine function on the 2 children of the current node until reaching root. Then the root has the answer, by just checking if it makes sense. Combine function: to get maximum index just max the maximum index of both children and your index, to see if it makes sense one of the children's index must be 1 more than the node's index, and then the other's index must be 1 more than the maximum index of the first child so it forms a dfs order that makes sense, and of course both subtrees must make sense (AND them together). Time complexity: O(n + q log n)

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

In D check 1 the editorial specifies this condition:

For every $$$u$$$, all of its children $$$v$$$ satisfy $$$[pos_v, pos_v+siz_v-1] \in [pos_u, pos_u+siz_u-1]$$$

In the code solution, a different, easier check is made:

int chk(int x) {
    return son[x].empty() ? 1 : (q[x] < *son[x].begin() && *--son[x].end() + siz[p[*--son[x].end()]] <= q[x] + siz[x]);
}

Here son[x] includes ordered indices of $$$x$$$'s children in the permutation. Therefore, it is only checking the editorial condition for the furthest child in the permutation, and that the closest child in the permutation has an index not less than the one $$$x$$$ itself has. Can someone explain how these conditions are equivalent to the editorial? In fact I tried to check the editorial condition directly in my code but got TLE probably due to constant factor.

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

    editorial should have cared to explain that.
    Nevertheless, here we go.
    Lets prove it for any node $$$u$$$.
    Assume that condition is satisfied for all immediate children $$$v$$$ of $$$u$$$.
    it means, for any immediate child $$$v$$$ of $$$u$$$, the range $$$[posv,posv+sizv−1]$$$ will contain whole subtree of $$$v$$$, and nothing else.
    So, the point is, for any two immediate child $$$v1$$$ and $$$v2$$$, their range is non intersecting. ($$$[posv1,posv1+sizv1−1]$$$ & $$$[posv2,posv2+sizv2−1]$$$). If the ranges are non intersecting, then we can just check the first and last range, and if they are contained within $$$[posu,posu+sizu−1]$$$, then all other are forced to contain within it.

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

      Please correct me if I'm wrong, but I think you're only proving one side of the equivalence, namely editorial check implies code check. In fact we would be more interested in the other implication ( code check implies editorial check ), since the code check at first glance looks like a weaker condition. That is, for a single node it doesn't make much sense for the checks to be equivalent, but rather the fact that this weaker check works for all nodes at the same time might be what actually makes it equivalent.

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

        May be, I should have also cared to explain more.,
        I didnt prove editorial check implies code check, that is noobest thing i can comment.

        what I have proved above?
        How I have proved that?
        editorial check
        code check
        • »
          »
          »
          »
          »
          3 месяца назад, # ^ |
            Проголосовать: нравится +3 Проголосовать: не нравится

          Ok I guess I didn't understand the induction right. I got it now. Thank you!

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

How is F2 harder than F1? I don't see any difference, only tighter time limits maybe. Can someone explain why F2 is considered harder than F1?

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

Quoting editorial for D2:

Then, for each pair of adjacent elements $$$p_i,p_{i+1}$$$, $$$fa(p_{i+1})$$$ must be an ancestor of $$$p_i$$$

Can you tell me what does the notion $$$fa(p_{i+1})$$$ mean? I don't think I have seen it being declared anywhere in the tutorial. Thank you in advance.

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

Can anybody explain the proof for C? I get the part where CD = AD and the CD > CB-DB part, but everything else kinda just falls apart... I've forgotten everything about proofs from geometry class

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

    I can't understand this proof either, but if you look at the conclusion directly, it will be obvious that if the distance from the center of the circle to the target is less than or equal to the distance from the starting point to the target, it is obvious that the boundary of the circle will be touched before (and when you reach this point).

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

Solved ABCD1 and became specialist.

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

what does this line mean in editorial of $$$H$$$?

Let b_i be the number of operations that has the element equal to v after block y_i as its center

I couldn't understand the rest of the editorial because of that

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

WOW!You are very good! But could you give me some exegesis in the H code?

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

why was E placed after D?

»
3 месяца назад, # |
Rev. 3   Проголосовать: нравится +1 Проголосовать: не нравится

for the following test case for D2,

1
7 2
1 1 7 2 3 3 
1 3 7 4 6 2 5
3 5
5 3

check 1 outputs NO NO but check 2 (and other ACs) output NO YES and i think it should be NO YES. the way siz is calculated in check 1 makes siz[1] = 6 and siz[3] = 3 but shouldn't they be siz[1] = 7 and siz[3] = 4?

Edit: nvm the constraint ai < i is not satisfied for this tree so it's a wrong tc

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

Solution 3 in problem F2 is interesting. Despite $$$L = 50$$$ is already hacked, higher $$$L$$$ should still yield an AC (with the cost of praying that one's code wouldn't TLE).

A loose upperbound by me, with $$$L = 125$$$: 276065570

I wonder how far could the uphack raise up "cheeseable" lowerbound value for $$$L$$$.

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

Could anyone explain how we are supposed to preprocess GCD as mentioned in F1 solution?

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

    I didn't preprocess GCD, my solution got passed

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

      My F1 also passed using the normal one. But for F2, I had to use custom binary gcd function (which I copied from one of the CF blogs). That's why I asked if it was possible to preprocess the gcd and then find them in constant time. It will be a huge optimization, nearly 20 times for the given constraints.

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

For problem D, the following check is also sufficient:

  • for every $$$i$$$ : $$$lca(p[i-1], p[i]) = parent(p[i])$$$
  • »
    »
    3 месяца назад, # ^ |
      Проголосовать: нравится +12 Проголосовать: не нравится

    It's just check2 written in another form.

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

    Hey, can you explain me how can I use segment tree to solve the problem D2. The authors solution mentions that we can impose some check on each node in the permutation.

    Specifically, For every u, all of its children v satisfies

    $$$[pos_v, pos_v + siz_v-1] \subseteq [pos_u, pos_u + siz_u-1]$$$

    And, we can maintain this check by keeping track of the number of u which violates this condition, and check for each u using sets.

    First of all, how can we do this using sets, and how can we do this using segment tree. I tried thinking a lot, but I could only think of maintaining a segment tree using dfs entry time. So each segment node in the segment tree contains a set/vector containing entry times for each tree node in this segment. And for swapping part, I could do it like replacing the entry time for swapped nodes in the set of their respective segments.

    Then how can I perform verifying this check for each node using this segment tree? This is where I'm getting stuck. Can you help me out, Please.

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

      Editorial should have mentioned that, they compare only smallest and largest $$$posv$$$ to see if they are within $$$[posu,posu+sizu−1]$$$, rather than all its childs. see: https://codeforces.me/blog/entry/132569?#comment-1182635

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

        Yeah, I get it that checking the first and last child range is equivalent to checking all of children of u. Because the set stores the position of each node in sorted order.

        And we can easily perform swapping by first deleting the position of a node from its parent's children set and then add the swapped node position to it, and again the set would maintain all the positions in sorted order. The number of good nodes are decreased for now, as we need to check again after swap if they become good nodes.

        Now after swap, we only need to check for the (parent(a), a) and (a, children(a)) for all children of a. Similarly for (parent(b), b) and (b, children(b)) for all the children of b, for the condition of a good node. Because we want to see if after swap, the node is still contained in its parent range, and the node still contains all of its children within its range. If yes, then the number of good nodes increases by one each time for all these 4 pairs, otherwise not. And after each query, we check if all n nodes are good nodes or not. And we print the answer accordingly.

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

Can someone explain why this solution for D is timing out? 276371875

I am trying to implement Um_nik's idea from his submission: 275817851

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

    Interesting, submitting your solution in GCC 7-32 yielded TLE while GCC 13-64 yielded RTE.

    Huge red flag of undefined behaviors right here, though I can't yet tell where it was.

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

Alternative (maybe easier) solution for D :

let s[x] = {{j1,u1}, {j2,u2},... } be the set of indices and node id of the children of the node x, pos[x] the position of node x in P, and siz[x] the size of the subtree of node x.

It suffices to check that for all i from 1 to n : pos[x] = min(s[x]) + 1 , and for all i from 1, s[x].size() — 1 : Ji+1 — Ji == siz[Ui] meaning the positions of the children of x when sorted should have the difference of siz[u] where u is the node with smaller pos.

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

Ask a question to the time complexity of G.

this formula $$$2^{2B}+2^{2-B}$$$

but you use some calculus you know B=1/3 it's minimum. not B=2/3

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

for f1, if you let p be the largest prime <= n, then x<=p or y<=p is possible.