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

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

Hi, Codeforces!

1529A - Eshag Loves Big Arrays

problem idea and solution: AmShZ

editorial
official solution

1529B - Sifid and Strange Subsequences

problem idea and solution: Davoth

editorial
official solution

1528A - Parsa's Humongous Tree

problem idea and solution: shokouf, AmShZ

editorial
official solution

1528B - Kavi on Pairing Duty

problem idea and solution: alireza_kaviani

editorial
official solution

1528C - Trees of Tranquillity

problem idea and solution: AliShahali1382

editorial
official solution
better implementation of official solution: AaParsa
python solution: Tet
another O(n.log) solution with seg-tree: N.N_2004

1528D - It's a bird! No, it's a plane! No, it's AaParsa!

problem idea and solution: AmShZ, AliShahali1382

editorial
official solution
a different O(n^3) solution: Atreus

1528E - Mashtali and Hagh Trees

problem idea and solution: AliShahali1382

Thanks to Kininarimasu for the editorial.

editorial
official solution

1528F - AmShZ Farm

problem idea and solution: AmShZ, AliShahali1382

editorial
official solution
Разбор задач Codeforces Round 722 (Div. 1)
Разбор задач Codeforces Round 722 (Div. 2)
  • Проголосовать: нравится
  • +260
  • Проголосовать: не нравится

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

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

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

Woow !! Fast editorial Thanks :)

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

There might be an error in the n log n solution with segtree (div1. problem C).

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

I hope all the future rounds will be as good as this one

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

Nice contest with nice problems! Thanks

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

Genius problems. Nice job.

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

Bad tests in D? The $$$\log n$$$ part in my $$$\mathcal{O}(nm \log n)$$$ code triggers only $$$0.025 N^3$$$ times in large tests: 117257732

EDIT: dorijanlendvaj found a large input where it triggers $$$0.5 N^3$$$ times, that's $$$20$$$ times better than the original test data!

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

Via 1529B — Sifid and Strange Subsequences

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

Dfs Based Solution of C Parsa's Humongous Tree without DP based on the fact that during path traversal on a tree from root , A particular node is reached only once.

We return a pair {a,b} from every node.

A has the best solution from that node to all nodes below it when left_mini is assigned to that node.

B has the best solution from that node to all nodes below it when right_maxi is assigned to that node.

We store best possible values of the current node from all children node i.e. one with maxi on all possible calls and other with mini and then return it.

In the end when recursive calls end print the maximum of the pair of values returned from the root.

Solution

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

Where is the editorial on F?..

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

For Div2 D- "x≤n: In this case, due to the lemma mentioned above all the segments must have the same length, thus their length must be a divisor of n, in this case they can be paired in D(n) ways; where D(n) is the number of divisors of n.".

The equality should not be present I think. It should be only x<n. Correct me if I am wrong!

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

    If 1 is paired with n, each number from n+1 to 2n must belong in a segmemt with length n, where the length of a segment is defined as the number of elements inside it.

    The pairing is unique as well ...

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

I have done Question b in two way 1st submission : 117264950 2nd submission : 117264953 I can't find why my second submission failed I did the same thing but just in another manner, can anyone help me out please

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

    It took me a lot of time but I found out what's wrong with your code. Actually, you are failing edge case when seq is [-1] or [0] because in your second program you'll be having cnt = 1 and since there is only 1 item in the array it will not enter the while loop, and bool will evaluate to true and it will increase it by 1 resulting in ans = 2. Luckily your former code worked for these edge cases.

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

i am sorry for every one get wrong answer in problem B test 15 i put it in hacks :( can you give contriubution for it

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

Can anyone explain the DP applied in Kavi on the Pairing Duty problem in simple words?

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

In Div 1 Problem D:

"To handle the "waiting" concept, we can add n fake edges, i-th of which is from the i-th vertex to the (i+1 mod n) -th vertex with weight equal to one."

What is a vertex here? Does this sentence mean the cities? But waiting for 1 second doesn't move you to the next city, so that can't be. Or can it? Is the assumption, that "if we can reach city X from some other city Y, then we could've waited for 1s in Y and then we would've reached X+1"? That seems also to be the explanation for "It can be proved that doing dijkstra in the new graph is sufficient if we guarantee that the first used edge is not fake."

Well, It seems I answered my own question here while writing it down. Debugging-Rubber-Ducky for the win. But since I really was confused by this and I don't feel that this step is trivial, I'm gonna leave this comment here.

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

Can someone explain how to find Stirling numbers of the 2nd kind in O(k log k) (for F)?

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

    From wikipedia page \begin{gather*} S(n, k) = \frac{1}{k!}\sum_{i=0}^k (-1)^i \binom{k}{i} (k-i)^n \end{gather*}

    You can rewrite it as \begin{gather*} S(n, k) = \sum_{i=0}^k \underbrace{\left((-1)^i\cdot \frac{1}{i!}\right)}_{a_i} \cdot \underbrace{\left(\frac{1}{(k-i)!}\cdot(k-i)^n\right)}_{b_{k-i}} \end{gather*}

    So you can calculate arrays $$$a$$$ and $$$b$$$, and stirling numbers are just convolution of these arrays:

    \begin{gather*} S(n, k) = \sum_{i=0}^k a_i \cdot b_{k-i} \end{gather*}

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

I am getting TLE for Div 2 C Mysolution. I have done almost the same as Editorial (using memorization of recursion). Please anyone help me with debugging. Thank you

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

Since E editorial is not out yet, here is how I solved. Let $$$dp_i$$$ be the answer for all trees such that there exists a root and all edges are directed in the same direction from root and the root has at most $$$2$$$ children. We transition

$$$dp_i$$$ = $$$dp_{i-1} + dp_{i-1}*pdp_{i-2} + dp_{i-1}*(dp_{i-1}+1)/2$$$ where $$$pdp_i = \sum_{j=0}^i dp_j$$$

Then let $$$dp2_i$$$ be the same as $$$dp_i$$$ except the tree must have $$$2$$$ children. So $$$dp2_i = dp_i - dp_{i-1}$$$.

The answer for these cases is

$$$2*(dp_n + dp_{n-1}*pdp_{n-2}*(pdp_{n-2}+1)/2 + pdp_{n-2}*dp_{n-1}*(dp_{n-1}+1)/2 + dp_{n-1}*(dp_{n-1}+1)*(dp_{n-1}+2)/6) - 1$$$

This is because $$$dp_n$$$ holds the answer for at most $$$2$$$ children and the other section accounts for the rest. We multiply by $$$2$$$ to account for both edges directions, and subtract $$$1$$$ because a single path is isomorphic.

This obviously doesn't handle all cases, but all other cases can be found in the following form. Let $$$t_{up,k}$$$ be a tree where the root has $$$2$$$ children and the edges are directed up and the longest path is $$$k$$$, and let $$$t_{down,k}$$$ be a tree where the root has $$$2$$$ children and the edges are directed down and the longest path is $$$k$$$. Then all other cases are $$$t_{up,k}$$$ which exists on some path of length $$$l$$$ to and connects to $$$t_{down, n-k-l}$$$

We can count every other case as $$$\sum_{i=0}^{n-1} (dp_i-1)*dp2_{n-1-i}$$$

This works because we pretend the path is always length $$$1$$$, then if we do $$$dp_i*dp2_{n-1-i}$$$ we handle all cases except for when the $$$t_{up, k}$$$ is empty, and that only happens one time.

Here is my submission: 117272002

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

In problem B: my solution passed the pretests but giving time limit exceeded in test 4. It runs in O(nlogn) time l. Here is my submission Can someone help me finding what's wrong in my code?

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

can anybody please tell in B why its optimal to always choose negative elements

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

    the time complexity of min_element() is O(n), you are doing it (n-1) times in the worst case. you should think of propagating from the start only.

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

    because their maximum element is negative and absolute of all differences is greater than or equals to zero that is greater than negative maximum element.

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

    Suppose we choose more than 1 positive numbers a, b..., |a — b| < max(a, b), that means we can't choose more 1 positive numbers. Suppose all the numbers we choose is non-positive numbers, |ai — aj| >= max(ai, aj) is always true. So we just firstly choose all the non-positive numbers from the array, then calculate the minimum distance between these chosen numbers, we call it mn. And then check the original array if there is a positive number that is no greater than mn.

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

Problem B Div-2It's easy to prove that a strange subsequence can't contain more than one positive element.

Can someone please prove this ?

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

    Assume that it contains two positive elements — say 2 and 5 . Max is 5. Difference is 3 . Similarly, it is easy to see that difference of two positive numbers will always be less than the greater element of those two numbers. Hence taking more than one positive number is not possible

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

    For two positive elements, we have the property that their absolute difference will be strictly smaller than the larger element. So, you can clearly see that this violates the strange condition

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

can anyone find error on my code for Div2 C failing on test case 2. I can't find any error ,i checked it more than 10 times ,any help will be really appreciated

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

In div1 C / div2 E , Can someone explain the part 3 , i.e. "Among the vertices inside it, find the maximum number of vertices such that none of them is the ancestor of the other in Keshi's tree." . How are we doing it .

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

    When you give a dfs order of a tree, there will be no pair satisfies

    $$$in[u] \lt in[v], out[u] \lt out[v]$$$

    and u is a ancestor of v if and only if $$$in[u] \lt in[v] \leq out[v] \leq out[u]$$$. so,

    if neither of u or v is the ancestor of the other, then $$$[in[u], out[u]), [in[v], out[v])$$$ will be disjoint interval. and std::set is enough to maintain it.

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

Bye Bye Specialist!!
Hello Pupil!!

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

In Div 2 C, I think the condition should be $$$a_u \geq a_v$$$ for the first case and similarly $$$a_u \leq a_v$$$ for the second case. AliShahali1382

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

Very good round. ! Thanks for this round ! Graph and DP is always fun to solve in a contest. !

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

In Div-1 C [Editorial]: What are efficient ways to compute below?

Among the vertices inside it, find the maximum number of vertices such that none of them is the ancestor of the other in Keshi's tree.

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

This one of the best contest I have ever given on codeforces.

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

Can anyone explan in 2D,when $$$x\le n$$$,why each length must be a divisor of n?

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

Did anyone try solving Div2-D by solving the complementary problem??

The complementary problem was to find the total number of bad pairings, where a pairing is considered to be bad if, for at least two segments $$$A$$$ and $$$B$$$ among those, the following condition holds:
$$$1)$$$ $$$A$$$ and $$$B$$$ don't lie completely inside each other.
$$$2)$$$ $$$A$$$ and $$$B$$$ don't have the same length.

I was able to find exactly two segments $$$A$$$ and $$$B$$$, which will follow the above condition(if I am not wrong in computing that xD), but could not reduce it further. Is it even possible this way or is there any other approach to solve it??

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

I referred the editorial for Div2(C)/Div1(A)... But my solution failed after multiple attempts. Can someone please help me out? My solution 117289608

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

"To be continued ..." in Div. 1 C's editorial?

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

dpforces

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

Can someone describe the data strcuture used for Div1 C? It just displays "to be continued".

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

In div2 D, lets say n=4(8points) And lets say we pair 1 with 4 What are the rest of the pairs? Can anyone list them?

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

I really loved the problems!

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

Sorry to correct one possible little mistakes about the editorial of problem 1528A. In case 3(p=q),to maximize the beauty of the tree, au must be rv or uv.For example, if au=3,lv=0,rv=7, p=q=4, av=4,5,9,5,1,2,0,-1, the original beauty is 21.If change au into rv, the beauty will be 35.(In a word,I mean " The beauty of the tree doesn't depend on av" is not very rigorous.) However,thanks for your editorials again! They have helped me a lot!

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

Can someone tell, how to solve Div2C/Div1A if we need to minimise the sum instead of maximising?

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

how would we solve the div2 problem A if we were asked to find the max no of operations instead of max no of elements to be deleted?

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

    The algorithm I provided deletes $$$1$$$ element at a time, thus does the maximum number of operations. It's worth noting that the answer to both of the problems is the same.

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

Getting TLE on test case 4 of Div2 C. It follows the editorial explanation. Any help would be appreciated. Here's my submission.

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

    Add cin.tie(0) , ios::sync_with_stdio(0); to your main function and it should be fine.

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

      Thanks a lot. It works now. But why does it not work within the time constraints? Most of the n <= 1e5 cases work fine without fast I/O in O(nlogn) sorting time. Why does this solution, which is supposed to be O(n), fail to run in the required time?

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

        It's not as simple as that, the sum of $$$n$$$ over all test cases can be as large as $$$2 \cdot 10^5$$$, there can be approximately $$$2 \cdot 10^5 \cdot (5 + 9 + 9)$$$ characters in the input which is quite a lot.

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

          I'm sorry I did not understand, could you elaborate?

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

            I said that you might need to read more than 4600000 characters from the input (4.5e6). cin itself is slow so you can't read that many characters in time. So you have to add that magical line to your code to make it work faster.

            This is because by default, before you call the function cin, the call cout << flush is done. Adding cin.tie(0) unties cin from cout and stops this.

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

Can anybody please help me what is wrong with this code it is showing TLE test set 4 of question 3 Please help Thank you in advance

Here is my code: https://mirror.codeforces.com/contest/1528/submission/117327246

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

First time to solve C without fst though I used O(n(logn)^2) algorithm.

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

Can someone explain to me for 1528A — Parsa's Humongous Tree why every vertex needs to be either lu or ru. I'm finding it difficult to understand. Thanks

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

what does it mean by ans += a[i] != mn; in the 2nd for loop?

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

Can someone tell me why this code times out? (https://mirror.codeforces.com/contest/1529/submission/117339337)

The problem is 1528A — Parsa's Humongous Tree. Thanks!

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

In problem Div1C:

"Now to check whether any vertex u in the subtree of vertex v exists in S,..."

You don't need this check at all to AC. This is because of the condition that $$$p[i] \lt i$$$ for all $$$i$$$ in both trees. Assume that $$$x$$$ is a parent of $$$y$$$ in any tree, then that ensures that $$$x \lt y$$$. You can see that it can't be true that $$$x$$$ is a parent of $$$y$$$ in Soroush's tree (and is therefore in the stack of nodes beforehand when we start DFS at $$$y$$$), and $$$y$$$ is a parent of $$$x$$$ in the other tree, since that would require $$$y \lt x$$$ and $$$x \lt y$$$.

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

In 1528A — Parsa's Humongous Tree, i used a bit different approach in dp which seems quite similar to me with the one provided in editorial . But it is not passing the pretests and i can't figure out what is wrong with my approach. I am new to dp. Can someone help me out?
Code using my own approach: my approach Code using editorial's approach: editorial approach

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

In problem C official solution, because of the way the trees are given in the input (each vertex in both trees has higher label than its ancestors), there's actually no need to check the case where $$$res = -2$$$. For any pair of vertices $$$u, v$$$, if $$$u$$$ is ancestor of $$$v$$$ in the first tree $$$(u \lt v)$$$, $$$v$$$ can't be ancestor of $$$u$$$ in the second tree as it would imply $$$u \gt v$$$. So, you always insert the current vertex, you just need to check if there's any ancestor of it and if there is, just remove it :)

Here's the submission 117369367 with the corresponding assert.

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

In the editorial solution of 1528A, the proof claims we can get a better result by changing av to lv or rv because the contribution from its children increase in this process. But at the same time it can reduce the contribution from its parent right? How do we say that the net result of changing av is an increase in beauty?

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

https://mirror.codeforces.com/contest/1529/submission/117382997 This is my solution for problem C. It is showing TLE. Can anyone debug my solution,Pease.

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

    There's nothing wrong with your code. The input is quite large so you have to add cin.tie(0) , ios::sync_with_stdio(0); before reading input to do it faster.

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

Well, the editorial of the 1528C - Trees of Tranquillity has some mistakes.

In the sixth to last line, $$$p\geq st_v0$$$ should be changed to $$$p\geq st_v$$$.

"is less than $$$st_v$$$" should be changed to "is less than $$$ft_v$$$".

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

Q2 could've been solved using binary search as well which would reduce the time complexity. https://del.dog/binarysearchsolution.txt here is my solution using binary seach.

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

Here is a stupid solution for Div.1 C:

The problem is as such: choose a maximum subset of a path in Soroush's tree such that if $$$u$$$ and $$$v$$$ are in that subset neither $$$u$$$ nor $$$v$$$ is an ancestor of the other in Keshi's tree.

The problem can be solved as such: dfs on Soroush's tree, take node $$$u$$$ if none of its subtree in Keshi's tree is taken. If $$$u$$$ has ancestor $$$p$$$ in Keshi's tree that was already taken, greedily take $$$u$$$ in place of $$$v$$$, this works because a lower node is an ancestor of fewer nodes, in another word: picking $$$u$$$ instead of $$$p$$$ offers the chance to pick $$$v$$$ where $$$v$$$ is a child of $$$p$$$ and a sibling of $$$u$$$.

To solve this we'll need two kinds of queries: 1. on a path from root to $$$u$$$ find if a node is already taken, 2. in the subtree of $$$u$$$ find if a node is already taken. Both queries can be done by using a an approach similar to here.

Similar to the editorial we store $$$st$$$ and $$$ft$$$ for each node, notice that for the range $$$[0, st_u]$$$ a node exists exactly once if it is on the path from root to $$$u$$$ and may exist twice otherwise, thus we just need to focus on nodes that occurred a single time. To add a node we update the segment tree at $$$st_u$$$ and $$$ft_u$$$ to be $$$u$$$, to remove a node we set them to $$$0$$$.

Since at most one ancestor is taken at a time, we can implement an xor segment tree on the dfs order and query the range $$$[0, st_u]$$$; undesirable nodes cancel themselves out, all other nodes will be $$$0$$$ except for (potentially) one node: $$$p$$$ which we will then remove and take $$$u$$$ instead. To solve 2, I just implemented an or segment tree (max would work as well) to find if a node exists in subtree.

my solution, not pretty

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

For problem F, how do you go from the sum with binomial coefficients to the sum with Stirling numbers? Also, should the sum be from CNT=1 to n?

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

    I got it.

    We can interpret $$$\sum_{r=1}^n \binom{n}{r} \cdot n^{n-r} \cdot r^k$$$ as the number of functional graphs whose vertex set is $$$V = A\cup B\cup Z$$$ such that

    1. $$$|A|=n$$$, $$$|B|=k$$$, and $$$Z=\{z\}$$$ with $$$f(z)=z$$$,

    2. $$$f(V)\subset A\cup Z$$$ (no element of $$$B$$$ has any incoming edges),

    3. Let $$$C=\{a\in A:f(a)=z\}$$$ be the elements in $$$A$$$ that map to $$$z$$$. Then, $$$|C|=r$$$ and $$$f(A\setminus C)\subset A$$$ (the elements in $$$A$$$ that don't map to $$$z$$$ all map to some element in $$$A$$$),

    4. $$$f(B)\subset C$$$.

    This interpretation is correct because there are $$$\binom{n}{r}$$$ ways to choose the subset $$$C$$$, then $$$n^{n-r}$$$ ways to choose how $$$A\setminus C$$$ maps to $$$A$$$, and $$$r^k$$$ ways to choose how $$$B$$$ maps to $$$C$$$.

    We can count these functional graphs in another way. First, let $$$i=|f(B)|$$$.

    1. Choose how to map $$$B$$$ to $$$f(B)$$$ by first partitioning $$$B$$$ into $$$i$$$ sets. There are $$$\begin{Bmatrix}k\\i\end{Bmatrix}$$$ ways to partition $$$B$$$ into $$$i$$$ sets and $$$i!$$$ ways to order these sets.

    2. There are $$$\binom{n}{i}$$$ ways to choose a subset of $$$A$$$ to be $$$f(B)$$$.

    3. There are $$$(n+1)^{n-i}$$$ ways to choose how $$$A\setminus f(B)$$$ maps to $$$A\cup Z$$$.

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

Can anyone explain why pairing is unique for x <= n in Div2 D/ Div1 B.

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

There's an error in tutorial of Div1B.

$$$dp[i]$$$ is not equal to $$$\sum_{0}^{n-1} dp_i+D(n)$$$, but equals to $$$\sum_{0}^{n-1} dp_i+D(n)-1$$$ instead.

That's because length must be a divisor of $$$N$$$ and less than $$$N$$$, otherwise $$$x \gt N$$$.

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

Can somebody explain magical number 63 in div.1-D solution? From the first glance it implies that answers can't exceed 63...

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

B. Sifid and Strange Subsequences

Considering The Test Case

14 9276 -5771 -783 -6209 2041 -9286 -8065 3975 -2961 2689 -1680 9924 1483 3067

The -ve numbers are -9286,-8065,-6209,-5771,-2961,-1680,-783

And the minimum +ve number is 1483

I am confused about how can just adjacent checking ensure that the difference of pairs in the subarray satisfy the condition of strange sequence. For example abs(-9286-(-8065)) is not > 1483 similarly it holds true for -1680 and -783. Can someone please explain me why is this intuition incorrect .

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

    Consider this task: Given a non-decreasing array $$$a$$$, you must find a pair of elements $$$a_i$$$ and $$$a_j$$$ such that $$$i \lt j$$$ and $$$|a_i - a_j|$$$ is minimum. Obviously, there exists an answer such that $$$j-i = 1$$$, because otherwise we could have chosen $$$a_{i+1}$$$ without getting a worse result.

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

Deleted