### Error_Yuan's blog

By Error_Yuan, 4 months ago, 1869A - Make It Zero

Author: Error_Yuan

Hint 1
Hint 2
Solution
Implementation
Rate the problem

1869B - 2D Traveling

Author: programpiggy

Hint
Solution
Implementation
Rate the problem

1868A - Fill in the Matrix

Author: Error_Yuan

Hint 1
Hint 2
Solution
Implementation
Rate the problem

1868B1 - Candy Party (Easy Version)

Author: Error_Yuan

Hint 1
Hint 2
Hint 3
Solution
Implementation
Rate the problem

1868B2 - Candy Party (Hard Version)

Author: Error_Yuan

Hint 4
Hint 5
Hint 6
Solution
Implementation
Rate the problem

1868C - Travel Plan

Author: programpiggy

Hint 1
Hint 2
Hint 3
Hint 4
Hint 5
Solution

$\mathcal O(\sum(m\log n+\log^3n))$ solution by programpiggy:

Implementation

$\mathcal O(\sum \log^3n)$ solution by sszcdjr:

Implementation

$\mathcal O(\sum \log^2n)$ solution by sszcdjr:

Implementation

$\mathcal O(\sum m\log n)$ solution by bilibilitdasc:

Implementation
Rate the problem

1868D - Flower-like Pseudotree

Author: sszcdjr

Hint 1
Hint 2
Hint 3
Hint 4
Hint 5
Solution
Implementation
Rate the problem

1868E - Min-Sum-Max

Author: duck_pear
Huge thanks to Kubic for the development of this problem!

Hint 1
Hint 2
Hint 3
Hint 4
Fun Fact
Solution
Implementation (by Artyom123)
Rate the problem

1868F - LIS?

Author: Error_Yuan

Hint 1
Hint 2
Hint 3
Solution
Implementation
Bonus
Hint for Bonus

Bonus solution by duck_pear:

Implementation
Rate the problem Tutorial of Codeforces Round 896 (Div. 1) Tutorial of Codeforces Round 896 (Div. 2) Comments (123)
 » First Comment!!!
•  » » Okay Sorry!!! I was just Excited..
 » thanks for fast tutorial
 » wtf!! 4 months ago?
•  » » bug
•  » » They prepare the blog before
 » Enjoyed the contest thanks for the fast tutorial.
 » anyone knows edge case of test case 3 of problem b?? my sumbission :- https://codeforces.com/contest/1869/submission/222740219
•  » » Instead of m1=INT_MAX, assign it with a bigger value like LLOMG_MAX>>2.
•  » » » what about mine wa2 B? https://codeforces.com/contest/1869/submission/222829174
•  » » » » If $a > k$ and $b < k$,You will calculate the distance between $a$ and $b$ mistakely.The code after changing can get AC.222837643
•  » » » » » Can you please help me, why my code is giving TLE ? I'm also solving the question in O(t*(n+k)) time complexity.My submission : https://codeforces.com/contest/1869/submission/222959644
•  » » » » » » You should turn ll dist(int a, int b, vector> hash) into ll dist(int a, int b, vector> &hash)
•  » » » » » » » How will passing by reference help in avoiding the tle?Please explain
•  » » » » » » » » If you don't add &,the code will copy a new vector.It's cost $O(n)$ time complexity,$n$ is the size of vector >。If you add that,only an Address is passed in.It cost $O(1)$ time complexity.
•  » » » » » » » » » I see, thanks.
•  » » » May I know why you are right shifting it by 2 and not directly taking LLONG_MAX?
•  » » » » Because if we take LLONG_MAX, m1 + m2 will give overflow.
•  » » » » » Thanks
•  » » use 1e18 instead of INT_MAX
•  » » I made the same mistake as you can see in my submissions. Actually the distance can be bigger than MAX_INT so you need to compare with a bigger value like 1e18.
•  » » 3 weeks ago, # ^ | ← Rev. 2 →   because you declared it as a int(integer)the max size of int is ~2000000000(1e9*2)but the max answer is when become the distance between 'A' and "Nearest major city" is 4*1e9 (major city is x=-1e9 ,y=-1e9 ) and ("A" is x=1e9 , y= 1e9 )So abs(Xmajor-Xa)+abs(Ymajor — Ya)=4*1e9and the same thing to distance between the nearest major city And "B" is 4*1e9 So the max answer is 4*1e9 + 4*1e9= 8*1e9 the max value u can store in Integer is 2*1e9 so u need Long Long .i hope the explain is clear sorry for bad english :D
•  » » thnx a lot guys
 » ORZ fast tutorial
 » Typo in B1, should be $\implies a_i •  » » Sorry, it is fixed now.  » Why I am getting pretest failed in Problem A ? Here is the code •  » » In problem Make It Zero •  » » try the case 3 1 1 1  •  » » 3 weeks ago, # ^ | ← Rev. 2 → when you do the last one, you are just replacing$a_{n}$with$a_{n}$. •  » » » I am replacing a[n] with XOR of a[n] and a[n] which is 0 as p and r can be equal(given in the question's constraint) •  » » » » l and r . Sorry for the typo •  » » » » » You are replacing a[n] with a[n]. When you do the xor for only one number, you don't get the xor of 2 numbers. To make it more clear, assume the operation was multiplication in stead of xor. When you want to find the product of the numbers from l to r, where l = r = n, your answer is a[n], not a[n] * a[n]. I hope this approach helped you visualise your mistake. •  » » » » The problem said you replace each$a_i$with$a_l\oplus a_{l+1}\oplus\cdots\oplus a_r$, for$l\le i\le r$.So for example, if$l=1$and$r=3$, you set each$a_i=a_1\oplus a_2\oplus a_3$. if$l=1$and$r=2$, you set each$a_i=a_1\oplus a_2$. if$l=1$and$r=1$, you set each$a_i=a_1$. Do you see it now? •  » » » » » Yesh got it now...btw awesome explanation  » Thanks For the Lightning Fast Editorial  » 3 weeks ago, # | ← Rev. 4 → Here is my solution using dijkstra's if anyone wants to have a look. Please check it here Great solution in the tutorial by the way, will get there one day. •  » » TLE. how is that great •  » » » I meant in the editorial •  » » » » oh ok •  » » » pura to pdh le bkl •  » » bro... •  » » » IKR, average day in a gray's life. •  » » Bro you literally coded Dijkstra for it. You have my respect.  » 3 weeks ago, # | ← Rev. 2 → Quick question, in 2D Traveling how are we making sure that there are 2 major cities in between source and destination?edit — got it, it doesn't matter!! •  » » In the shortest path, there are either 2 major cities between source and destination, or none. •  » » 3 weeks ago, # ^ | ← Rev. 2 → Well we don't have to verify that there are 2 cities.Think of it like this, both cities A and B have a closest major city, except if k=0 in which case the answer is just the direct flight from A to B.Using the fact that both A and B have a closest major city, we can calculate for both A and B which major city is closest to them, just by iterating over all major cities and calculating the distance and keeping track of the shortest distance.Now we know that we can go from any major city to any other major city for 0 cost, and even if both A and B are closest to the same city, our solution still works.Then we can simply say ans=min(AtoMAJOR+BtoMAJOR, AtoB).  » How to aproach MEX problems? i rarely got solve them •  » » Practice more problems ... 2.(Might be silly tip):Use Pen and Paper while you do rough work especially for math ones.  » got wa in b just because of taking max value for mins = 1e9 •  » » 3 weeks ago, # ^ | ← Rev. 2 → well yes, it should be 4e9  » 3 weeks ago, # | ← Rev. 2 → is there any alternative solution for div2 D1? i tried this — https://codeforces.com/contest/1869/submission/222801815  » Any simpler explanation for B2? I can't quite comprehend the way mentioned in the editorial lol  » In D1B, I wonder how many people proved (before submitting) that we can always choose a starting point, or at least noticed that it needed a non-trivial work to prove... I think this part is the hardest part of the problem so I feel like it is "the more you think, the more you lose points" problem. •  » » Yep, the conclusion is quite guessable, but it's really hard to prove. •  » » Same, I've spent 15 minutes after writing a solution trying to prove it, shouldn't have done that. •  » » » Could you outline the proof? •  » » Ah funny, I just commented a similar thing below. I noticed but gave up on proving. I tried to write a solution that doesn't assume that, but I think it's really hard/tedious because of the$a[i]$which are already equal to the mean.I feel like this type of thing, where some proof detail is quite hard, happens somewhat often. It kind of destroys me psychologically in contest, because I don't believe in my solution anymore.But other people can reliably umm... not have this problem I guess. I would like to know how lol. •  » » » For me it is a bit instinctual. Ideally, I'd like to prove it in my mind or on paper before submitting, but I think sometimes that is a waste of time. If I have convinced myself beyond reasonable doubt then I simply submit. Like in this problem, I didn't feel that I needed to prove it, it just felt very natural to me that it was possible.I think this really depends on if you are a very mathematical person or not. I'm somewhere in between but I've had students on either end of that spectrum and their method works for them, but is a bit too extreme for my taste.  » 1B1"Let's prove that we can always choose a starting point properly for any cycle, so that the condition (1) is satisfied."I feel like this was much harder than the rest of the problem. I gave up and proof by AC'd.  » So fast! Thanks!  » My solution for Div1 B1/B2 was (I think?) a bit simpler than the editorials. Basically, similar to the editorials we can get two numbers a and b where we must give a and receive b. This can be transformed into, if we gained b, we send a. This is kind of like a graph where there are n edges, each representing a transfer that needs to happen. Then, to check if this graph is valid, it's quite easy. All you have to do is check if the graph can be turned into a bunch of cycles that do not reuse edges. It's very similar to finding an Eulerian circuit, and the restriction for that (for directed graphs) is that the indegree and outdegree must be the same. Something important is that in the case of the number equalling the average, the edge is a self-edge but can be placed anywhere, and as long as you visit a node, it works. Then, for B2, note that if the distance is a power of two then you can change two operations into one. The change to the degree array is +2 -1 or -2 +1 so you can loop downwards and apply changes when needed if possible.  » 3 weeks ago, # | ← Rev. 2 → in problem A div2, when n is odd , if I perform [n,n] once instead of performing [n-1,n] twice, it should give the correct answer right? as a[n] ^ a[n] = 0 . •  » » Well no. Operation for [n, n] will do nothing. Think of it this way, operation[1, 2] is a ^ a, operation[1,3] is a ^ a ^ a then operation[1,1] will be a, that's it, no more xor taken from there. It is range, so range(1, 3) contains 3 elements, range(1,1) contains only 1 element. •  » » » I got your opinion, but by following their definiton, try to just use their formula you will get a[n]^a[n], it costed me frustration and two wrong answers :) •  » » » 3 weeks ago, # ^ | ← Rev. 2 → but the definiton said l can be equal to r. for an array 1,2,3,4,5,6,7 if i perform 7 ^ 7 , it should be 0 right? hence performing an operation n n (n ^ n) operation should replace the last value with 0 right? •  » » » » yea weird :) •  » » n,n just means the xor of the subarray (n,n) and you are replacing an with an only not by an^an; •  » » Nope, because you don't XOR a[n] twice. The XOR in the range [n, n] would just be a[n], leaving the array unchanged. •  » » » why? check the definition that they gave: Let s=al⊕al+1⊕…⊕ar right? and they said l<=r suppose l==r s=al xor ar no?? I actually got two wrong answers because of this! please prove me wrong :) •  » » » » I see your point, but I think that's just standard mathematical notation: for example, I think you agree if I say that the sum of the numbers from 1 to n is n(n+1)/2, right? But according to your argument, for n = 1 it should be 1+1 = 2. •  » » » » » if we put n=1 in the formula blindly we will get it equal to 1 , not the same with their definition •  » » » » » » exactly. •  » » » » » » Yeah, but the formula is not what I focused on. I just wanted to point out that when we talk about the sum of the numbers from 1 to n we typically denote it as$1 + ... + n$. This notation means "1" and not "1+1" when$n=1$. •  » » » exactly.. at the end it will be 0^a[n] i.e. a[n] •  » » bruh... a[n] ^ a[n] =0 is correct... but if n is odd you will end up with 0^a[n] (i.e. a[i])..which could be a non zero number •  » » Fill in the missing expression in the sequence!!! 95% fail!!!$\underline{\qquad}$,$1\oplus2$,$1\oplus2\oplus3$,$1\oplus2\oplus3\oplus4$,$\ldots$Hint: The answer is$\textbf{not}1\oplus1$!  » 3 weeks ago, # | ← Rev. 4 → " Thus, if there exists a possible solution, the number of candies each person receives and gives away is determined for ai≠s . If an equation has no solution, then obviously the answer is "No". "In Div1 B1, how can we prove/find out, that the equation has no solution??UPD : Understood  » Video Editorial for problem A,B,C,D1  » Why does only taking good interval give the min amount of operations on F?  » It appears that there are some typos in the editorial for B2. I believe these are the correct formulas:For$x \leq cntDS_i$:$cntS_i := cntS_i + xcntS_{i+1} := cntS_{i+1} + cntDS_i - xcntT_i := cntT_i + cntDS_i - x$For$x \leq cntDT_i$:$cntT_i := cntT_i + xcntS_{i-1} := cntS_{i-1} + cntDT_i - xcntT_i := cntT_i + cntDT_i - x$•  » » I'm still confused about why the last 2 lines haven't kept a symmetrical relationship with the first fomula... just like:$cntT_{i+1} := cntT_{i+1} + cntDT_i - xcntS_i := cntS_i + cntDT_i - x$can anyone explain this for me? :)  » Can anyone explain why does the solution to problem A (Div. 2) works? •  » » For this problem I first considered if each of the$a_i$are either$1$or$0$, since it helps to think about the most extreme cases first sometimes.Then notice that$1\oplus 1\oplus\cdots\oplus 1 = 0$if the number of$1$'s is even, otherwise it's equal to$1$.Also notice that$0\oplus 0\oplus\cdots\oplus 0 = 0$for any number of$0$'s.So clearly, if$r-l+1$is even, then performing the operation on$a_l,a_{l+1},\dots,a_r$will either make them all equal to$0$, or$1$, and then performing the operation again must always make$a_l=a_{l+1}=\cdots=a_r=0$.But then the same must be true for any values of$a_i$, not only if they are in$\{0,1\}$, since the same will happen for all of the bits of the numbers.So if$n$is even, then we can just do the operation twice on the entire subarray.If$n$is odd, we perform the operation twice on the first$n-1$elements, and then twice on the last$n-1$elements to ensure that all the numbers in the array are$0$.  » 3 weeks ago, # | ← Rev. 2 → For 1869A — Make It Zero, why couldn't you do something like cout< •  » » That doesn’t do anything. It replaces the last element just by the last element itself. We need to replace it by 0. •  » » » but your doing XOR to the same value, so doesn't that make it 0. Like 8 ^ 8 = 0 •  » » » Actually, my bad, I see why. I thought that doing cout<  » I can't understand Div2D2 solution. Can someone explain plz?  » thanks for fast tutorial.  » bad cones\  » For this example in Q D1/B1 1 3 6 4 8We are getting YES , can anyone tell me method of distribution of candies •  » » Test case -> 1 N -> 3 [6,4,8] •  » » Last person gives 4 candy to the first one; First person gives 4 candy to the second one; Second person gives 2 candies to the third one.First person: 6-4+4=6 Second person: 4-2+4=6 Third person: 8-4+2=6 •  » » » ohh okay got it.  » 3 weeks ago, # | ← Rev. 2 → In div1A / div2C Test case 25 is : 2 4 According to the solution the resultant matrix is [[0 1 2 3], [1 2 3 0]]v[i] : 2 0 0 1s = mex(v[i]) = 3But correct answer for this test case is 4 How is this possible or am i missing something??? •  » » Bro you are correct by ur proof. The mex of 2 4 is 3. But ur code is printing 4.  » In problem Div1 C what is$f_{i,j}$(one end being$i$— what is it?) and what does this mean: The only difference is that only one$f_{i,j}$is not$0$under this circumstance and we can solve it in$O(log^2n)$?  » 3 weeks ago, # | ← Rev. 4 → My approach to Div1B1/Div2D1 and Div1B2/Div2D2, with only 1D vectors and 1D maps and no graphs (though one can argue there is an implicit graph being considered):Net Gain: The total sum never changes, so to make all values equal, each value must become equal to the average. If the average is not an integer, answer is NO. After calculating the average, we can easily determine, for each value, what number needs to be added/subtracted to reach the average. Each value has a required "net gain" to reach the average. For values above the average, the net gain is negative (since they actually need a net loss). Validity of Net Gain: In order to reach the average, the net gain must be expressible as the difference between two powers of 2. For example, a net gain of 12 can be achieved by receiving 16 candies and giving 4 candies. But a net gain of 5 cannot be achieved. Checking whether a net gain value is valid, as well as determining what the respective powers of 2 are, is easy when considering the numbers in binary. Assuming$x > y$, then$2^x - 2^y = 2^y (2^{x - y} - 1)$will have$(x - y)$1s in a row, followed by$y$lagging 0s. Utilizing the __builtin_clz and __builtin_ctz functions can help in quickly determining whether a net gain value is valid or not, as well as what the corresponding powers of 2 are (but there are many other ways to do this as well). Note that we would also have to deal with scenarios for$x = y$(net gain of 0) and$x < y$(net gain is negative, but we can inspect the binary representation of the absolute/negated value instead). Fulfilling all Net Gains (Easy Version): In the Easy version, if a net gain value is valid, i.e., expressible as$2^x - 2^y$, then the only way to achieve this net gain is to receive$2^x$candies and give$2^y$candies. To check whether this is possible for all values, I used a simple map mp, where for a positive integer p, an entry mp[k] = p indicates there are p values that want to give k candies, while an entry mp[k] = -p indicates that there are p values that want to receive k candies. For each value where net gain is non-zero, I determine$2^x$and$2^y$, increment$mp[2^y]$and decrement$mp[2^x]$. If all net gains can be fulfilled, then all map entries should be 0 at the end, since the number of people who want to give$k$candies must match the number of people who want to receive$k$candies, for all$k$.Correctness: Having all map entries be 0 is a necessary condition for Yes, but is it sufficient? We don't have to worry about values with net gain 0, because we can simply have them pass a candy around to each other. Even if there is only one value with net gain 0, this person can be placed as a relay between someone who needs to give some candies to another person. The hard part was on checking whether there will always be someone who has enough candies to start the chain. Apparently this is always satisfied (according to the editorial), and while I don't have a proof either, I intuitively observed that the person with the largest number of candies in each cycle should have enough to start the chain.Easy Submission: 222779990 (it has some artifacts from when I was trying to check whether it's always possible to start the chain, like sorting the values in reverse-order, and setting a flag when somebody has enough to do so, but this doesn't account for multiple independent chains)Hard Version: If a valid net gain,$2^x - 2^y$cannot be be written as$\pm 2^z$for integer$z$, then the only way to achieve this net gain is through receiving$2^x$candies and giving$2^y$candies, which we have already dealt with in the Easy version. But if$2^x - 2^y = \pm 2^z$, then there are actually two options. Without loss of generality, if$2^x - 2^y = 2^z$, then we can either (A) receive$2^x$and give$2^y$, or (B) simply receive$2^z$and give nothing. Note that, in this case, we must have$x = z + 1$and$y = z$. How do we decide which of the two options to choose? My approach was to first deal with the values that have only one option (like Easy Version) and stores the values with two options in a separate vector to handle later. The map mp may have some non-zero entries at this point. For the remaining net gains (which are powers of 2, or their negations), I sorted them from highest absolute value to lowest absolute value. Let's say the largest value has a net gain of$2^z$. I consult the map to see if anybody else wanted to give$2^{z + 1}$candies. If yes, then choose option A, i.e., receive the$2^{z + 1}$and give$2^z$. If there is no such person, then choose option B, i.e., receive$2^z$only and give nothing. The case of net gain$-2^z$is handled similarly. In any case, we update the map and move on to the next value. Correctness: The sorted descending order is crucial. When we process the value with net gain$2^z$, if there really is some value that wants to give$2^{z + 1}$candies, then the only valid recipients are those with net gain$2^z$, since the remaining values afterwards would have smaller powers of 2, and cannot receive$2^{z + 1}$candies. So option A is always valid in such a scenario.But what about the scenario where we process a value with net gain$2^z$and nobody wants to give$2^{z + 1}$candies? Sometimes, option A might be valid here, i.e., receive$2^{z + 1}$and give$2^z$, but this requires that there is a$-2^z$value later on who will also choose option A, i.e., give$2^{z + 1}$candies, but such a value would also have to receive$2^z$candies, so these two people can simply swap with each other only. This is equivalent to both people choosing option B. Therefore, choosing option B is always guaranteed to work in this scenario (whereas option A would fail if there is no$-2^z$later). Hard Submission: 222851644 (still has the artifacts from Easy) •  » » Thanks a lot.  » 3 weeks ago, # | ← Rev. 2 → No matter what I do, I can't find the bug in my problem 1C. This is the first time I am completely unable to understand where my mistake is, even though my code can't pass the sample. I'm confused because the first three examples passed and there were no issues with overflow or out of bound or anything when I submitted it to Codeforces. Please, can someone help me take a look? I've been working on it all day. 222871376 •  » » Found the bug , I'm so silly.  » Hi could someone please explain how to find the values of x and y in Div2B1?  » May anyone explain the bit operations in the editorial of Problem D1 in div.2? I can't quite understand it •  » » No problems now I've figured it out  » Thanks for the great contest!  » I think B1 is an amazing problem.  » I posted a video editorial of problem C from Div. 2, I hope you enjoy it and find it interesting.  » D2 is great  » thanks for clear editorial ^^;Nice E  » Can someone tell me why my code is giving TLE ? I'm also solving the question in O(t*(n+k)) time complexity.My submission : https://codeforces.com/contest/1869/submission/222959644  » Problem AIf your n is even :Say you have:n=6[a,b,c,d,e,f]a^b^c^d^e^f =X[X,X,X,X,X,X]Now if you do the operation one more time on the entire array,X^X^X^X^X^X= 0[0,0,0,0,0,0]So the no of operations used are 2. (1 to n) (1 to n)If your n is Odd:Say n is 5[a,b,c,d,e]a^b^c^d^e=U[U,U,U,U,U]So U^U^U^U^U=U [U,U,U,U,U]SO WHAT WE DO IS : IF YOUR N IS ODD WE XOR 1 to N-1 and again N-1 to N[a,b,c,d,e]So a^b^c^d=U[U,U,U,U,e]And now again from 1 to n-1 U^U^U^U=0SO ARRAY BECOMES [0,0,0,0,e]NOW from n-1 to n0^e=e[0,0,0,e,e]Again n-1 to n e^e=0SO NOW IT BECOMES [0,0,0,0,0]So if n is odd then output 4 (1 to n-1) (1 to n-1) (n-1 to n) (n-1 to n)  » In A why we are taking 2 cases for even and odd when we can simply apply XOR on the whole array twice and get the ans?  » 3 weeks ago, # | ← Rev. 2 → In Problem D1, can someone please explain why the following input has output 'No'1316 10 10From what I understand, a1 can give 4 candies to a2 and then a2 can give 2 candies to a3 which should make the arrray12 12 12 •  » » nvm, I got it. I misread the statement where it says that it is mandatory to receive candy for each person.  » 2 weeks ago, # | ← Rev. 3 → can someone help why its MLE 18 Div2C? https://codeforces.com/contest/1869/submission/223144068upd:solved i'm changed writing in vector when m>n solution: https://codeforces.com/contest/1869/submission/223145308 anyway i dont get why first solution MLE  » 2 weeks ago, # | ← Rev. 2 → 💚  » dX ain't so happy 'bout problem D&F... felt it's actually okay:)  » 2 weeks ago, # | ← Rev. 2 → B2's tutorial needs a fix. Should be$a_i + 2^{x_i}\$ » Why i have to take even numbers of element to do xor operation. Why cant i do 1 to n xor operation two times in every case.(in https://codeforces.com/contest/1869/problem/A)
 » 老子真草了，题出这么难干什么啊？
 » 写题解不写完整是吧，证明留给读者，我真艹了你*了
 » 这D2的代码和题解真的弄的是一道题？后面的公式全是错的，真的，不想写可以不写的