### PikMike's blog

By PikMike, history, 6 months ago, translation, ,

•
• +58
•

 » 6 months ago, # |   +28 What about using a persistent segment tree for F? We let version=depth and it should run in .
•  » » 6 months ago, # ^ |   0 I'm assuming that by version = depth, you mean that T[i] in the k-th version will contain the k-block minimum for the subtree rooted at 'i'. Persistent segment trees get their memory and time complexity based on the fact that each version differs from the previous one by 1 position ie., O(LogN) nodes in the tree. But, in this case, the minimum for k+1 can be different from the minimum for k at every node.Correct me if I got it wrong, if that is not what you meant.
•  » » » 6 months ago, # ^ | ← Rev. 3 →   +4 I was struggling with that for a while too, but since you can reindex the nodes in DFS order, keeping track of entry and exit index, before putting them in segment tree, it guarantees logn. The key thing is the segment tree is balanced even when the original tree isn't. Here's my submission: http://codeforces.com/contest/893/submission/32605016EDIT: My bad, I didn't read carefully. The k-th version is all nodes with distance <=k from the root (using placeholders +INF for all the other nodes)
 » 6 months ago, # |   0 how to solve 3rd question rumor
•  » » 6 months ago, # ^ | ← Rev. 2 →   0 The problem mainly can be modeled as a graph , where each group of connected vertices (connected component) represents some people which can share the rumor with each other so you can only tell one of them the rumor and he will spread it among all the group. so in order to minimize the cost you should select the cheapest one to tell him , and he will tell all the group , and you should do the same for all groups. Ans = summation of minimums in all groups.
•  » » 6 months ago, # ^ |   0 This is my solution to this problem i did it as described in the editorial, i took given members as nodes and friendship between a pair of members as edge and constructed an undirected graph and simply traversed each connected component using bfs and took the minimum value of member from each connected component and added it to answer
•  » » » 6 months ago, # ^ |   0 An integer isn't always large enough to contain the final solution, as it can be up to c*n, which is 10^14.
•  » » » » 6 months ago, # ^ | ← Rev. 3 →   0 So you have to take integer of 64bit to store final answer
 » 6 months ago, # | ← Rev. 2 →   0 in E — in O(1) (by precalcing factorials and inverse factorials)https://en.wikipedia.org/wiki/Stirling_numbers_of_the_second_kindin order to solve the problem you had to calculate s(n,k). How can you calculate this with O(1)?
•  » » 6 months ago, # ^ |   0 U can precalcualte factorial and inverse factorial:for(int i=0;i<=1e6;i++) { fact[i=fact[i-1]*i ifc[i]=ifc[i-1]*modularinverse(i) }
•  » » 6 months ago, # ^ |   0 I think he means you can query/access value in O(1), but I'm sure the time to precompute takes longer.
 » 6 months ago, # |   +6 Can someone please explain Div 2 F ? I am not able to understand it from the editorial ?
•  » » 6 months ago, # ^ |   0 I made it using something like merge-sort tree. First, you need to do a dfs(euler tour tree) to store the time you processed the vertex u, store the depth of u and the size of subtree. Create an array V[] that on each position V[time[u]] stores a pair containing the depth and the cost of vertex u(V[time[u]] = ). Now, Build it like a normal segment tree, but each node keep an array that stores all the elements of V in the range(L,R). You can see that only O(nlogn) memory is used to store them. Do some preprocessing on the nodes of the tree: for each position i of the array of each node, do array[i].cost = min(array[i].cost, array[i-1].cost). Now, you just need to do a query on the segment tree: find the interval [time[u],time[u]+subtree_size[u]], and do a binary search on the array stored on the node searching the last value of depth less or equal than K and return its cost. Since we made that preprocess before, this cost will be the cost of the minimum cost vertex of the k-blocked subtree of u. I hope you understand, my english isn't that good.
•  » » » 6 months ago, # ^ | ← Rev. 2 →   +3 Can you elaborate the pre -processing part? Also, can you explain the over-all logic behind your code?
•  » » » » 6 months ago, # ^ |   +4 The pre-processing: The answer for the k-blocked subtree of u is the minimum between the answer from 1-blocked to k-blocked subtree of u. So we need to get the minimum value c1 that has distance not more than k from u. Since the data structure is holding , in each node, an array containing pairs like sorted by depth, we just need to propagate the minimum answer found to bigger depths in that range. Thats why i pre-process every node, to make this prefix minimum operation. Now, how do i know i am querying the subtree of u? Using time[u] (from the first dfs), we know that for all v with time[v] <= time[u]+subtree_size[u], v is in the subtree of u, so when i call my query to get the answer from time[u] to time[u]+subtree_size[u], with depth K+depth[u], i'm getting the minimum answer for the subtree of u. When i find the range of subtree of u, i do binary search on the array, getting the last pair with depth < K+depth[u] (if depth[v] -depth[u] <= K, then v is on the k-blocked subtree of u). Since i made that prefix minimum operation in the array, the last pair satisfying the condition gives me the minimum answer for that range.
•  » » » » » 6 months ago, # ^ |   +3 Can we build a segment tree inside every node of the original tree, to get the minimum cost, in the given range?
•  » » » » » » 6 months ago, # ^ | ← Rev. 2 →   0 I believe would just be a 2D segment tree. But as the editorial says, it might be too slow since querying would be O(lg^2 n).
•  » » » » » » » 6 months ago, # ^ |   0 I solve using sqrt decompositon which answers queries in O(sqrt(N)), so I think, as editorial says, O(lg^2 n) per query is OK.
 » 6 months ago, # |   +5 Problem C can be solved using disjoint set too!
 » 6 months ago, # |   0 Can someone elaborate on solving Problem C. I understand that if we find the minimum of each group, it will give us the minimum gold in order to spread the rumor. However, how can we turn the list of pairings we are given as inputs into groups?
•  » » 6 months ago, # ^ |   +1 Build an undirected graph. If there is a pair (u, v) then there is an edge from node u to node v and from node v to node u. Two nodes are connected if there is a path between them.
•  » » 6 months ago, # ^ |   0 You can build a graph from the list of pairs of friends. Then, every connected component will be a group. Run a dfs for each group and keep the minimum value from it. Sum this value to the ans after finishing a dfs on one group. Another solution(mentioned by the guy above) is to use disjoint set union. For each pair (u,v) , merge the group of u with the group of v, storing the minimum value for that group. Now just iterate over all disjoint groups adding to the ans the stored value for each group.
•  » » » 6 months ago, # ^ |   0 Thanks, your explanation was very clear. I'm still having trouble with the "Every component will be a connected group" part. How do you represent these groups? For example if 1 is friends with 2 3 4, then my adjacency lists look like: 1 : [2, 3, 4] 2 : [1] 3 : [1] 4 : [1] But if I DFS starting with every node, I will find the minimum, but I will be quadruple counting this one group. Sorry if this seems silly, I am pretty new to graph problems.
•  » » » » 6 months ago, # ^ | ← Rev. 2 →   +5 You could use array of boolean to flag which node/vertex already visited. So it looks like this:
 » 6 months ago, # |   0 Can someone explain me please why C(y+cnti-1 ; cnti) is the corect formula? I can.t undertand from where we have this relation
•  » » 6 months ago, # ^ |   0 I can't understand it too
•  » » » 6 months ago, # ^ |   0 Thank you :D
•  » » » 6 months ago, # ^ |   0 Thank you!
 » 6 months ago, # |   0 in problem D a0<0 how it is possible because starting amount of account=0.
 » 6 months ago, # |   +3 Good Contest
 » 6 months ago, # |   0 Can someone explain me "Segment Tree structure" solution for "F" ?
•  » » 6 months ago, # ^ |   0 Hi, i explained my solution using segment tree here (on this same post)
•  » » » 6 months ago, # ^ |   0 thanks a lot, my inattention(
 » 6 months ago, # | ← Rev. 3 →   0 Can someone explain to me why my solution for D is wrong? https://paste.ubuntu.com/26042298/For each ai = 0 I supppose that I deposited the maximum amount of money that I am allowed to deposit. Then, if my money at bank is greater than d, I substract only the necessary from the last deposit; if after this update, my last deposit cant make my money at bank non-negative, print -1.
•  » » 6 months ago, # ^ | ← Rev. 2 →   0 Accepted!I was missing something important!You can check my solution here: 32652462
•  » » » 6 months ago, # ^ |   0 Hi, what should be the output for 4 10 -10 -10 -10 0Your solution gives output as 1. I think it should be 2 with final solution as 10 -10 10 0. Am I missing something here ?
•  » » » » 6 months ago, # ^ |   0 In the given input they will check our account balance only last day.To make sure the money we have in the last day is non-negative, she should go to the bank in the last day and deposit some money between 30-40. Because she has -30 and the limit is 10. Answer is 1.
•  » » » » » 6 months ago, # ^ |   0 Okay got it, actually I thought that the amount of money that can be deposited also cannot be greater than the limit. Thanks !!
 » 6 months ago, # |   0 why does the code showing tap doesnot appear now in the tutorial ??? it hsould be there atleast for the hard problems along with specific comments at the right place .
 » 6 months ago, # |   0 Can someone provide a proof for editorial for E?
 » 6 months ago, # |   0 893F can be transformed into a 2D RMQ without assignments where there's only one element in each row, so I think 1D Sparse Table + 1D persistent segment tree/balanced tree can be used, which could also run in O(n log^2 n + m log n)... (Note: let dat[i][j] be the segment tree for elements from the i-th row to the (i+2^j-1)-th row, then the difference between dat[i][j] and dat[i+1][j] is only about two elements) Yet I'm not certain whether this solution will get Memory limit Exceeded. [sorry for my poor English]
 » 6 months ago, # |   0 Can someone explain the example input of F, why the output is "2 5"? I think I misunderstood the problem somehow...
 » 6 months ago, # |   +10 In Problem C — Rumor, the editorial and comments mention describe a solution where one takes the sum of minimum gold for all connected components. In my solution 32587668, I sorted the characters by the amount of gold they need to spread the rumor. Then it is sufficient to process each character in order and run a DFS if not visited and sum up the gold required. There's no need to take the minimum anymore.
 » 6 months ago, # |   0 In problem E for every prime divisor i of n , we are calculating the number of ways of putting 'cnti' objects into 'y' boxes.Or in other word the number of non-negative integral solution of equation x1+x2+x3+....+xY = cnti. And this should be equal to (cnti+y-1)C(y-1) .What is wrong in this logic? And if it is correct then how that formula came which you wrote over there?
•  » » 6 months ago, # ^ |   0 nCr = nCn-r
•  » » » 6 months ago, # ^ |   0 Thanks bro
 » 6 months ago, # |   0 For f we can also use fractional cascading wich gives us a solution with (n+q)log
 » 6 months ago, # |   0 I have a solution for F that involves a 2D data structure (It's a Segment Tree with Treaps in each node). It runs in O((N+M)*Log^2(N)). Here's the submission: 32671104
 » 6 months ago, # | ← Rev. 2 →   0 hey codeforces !! can someone please explain the logic of the beautiful divisor question given above i do this ques by my way first genrating all the distinct divisors in o(sqrt(n)) time putting one by one in set and then from the right most element of set which is the largest (I calculated its binary in top-down fashion ) and i get accepted verdict. But this solution sucks me off any help will be appreciated ! I want to do this in O(1) time as explained above :)
 » 6 months ago, # |   0 Hello codeforces community .. can someone tell me that the rumor problem can be solved without using the dfs / bfs i am just trying to maintain the neighbours of each vertex in an vector of vectors but get stuck any help will be appreciated
•  » » 6 months ago, # ^ |   0 You can do it without dfs/bfs. Check the union find algorithm or you can check my solution
•  » » » 6 months ago, # ^ |   0 hey u have also uses the iterative approach or the recursive one with dfs
 » 6 months ago, # |   0 Can someone post their approach for the problem D (Credit Card) in Div 2?
 » 6 months ago, # | ← Rev. 2 →   0 I suddenly come up with a interesting idea, problem F can be solved with KD-tree(2-dimension) ...and I tried just now, it was proven to be feasible and it's space complexity is nearly to O(n)^_^ 32738460
 » 6 months ago, # |   0 Actually, tests in F are really weak. I got AC with O(n) for minimum query (with default athor solution which is supposed to use sparse tables to process a query with O(log(n)) by going down from x with 2^k steps and taking minimum on segment of precalculated values each time).
 » 6 months ago, # | ← Rev. 2 →   0 In E, can someone explain this ? And why do we multiply answer every time when we encounter new prime?
•  » » 6 months ago, # ^ | ← Rev. 2 →   0 Imagine each element in the array as a box, so we have y boxes with us. Now suppose we have 2 as a divisor and our number x is divisible m times by 2. So now we have m 2's which we can put in any of the boxes. So how do you arrange m (balls) into y (boxes) ? See here for the formula: https://en.wikipedia.org/wiki/Stars_and_bars_(combinatorics)
•  » » » 6 months ago, # ^ |   0 Thank you!
 » 6 months ago, # | ← Rev. 3 →   0 Can someone tell me why my submission gets MLE in problem F . I think it uses Nlogn memory which is within the limits. What am I missing here ? PS. I made a typo in binary search in the get() function as typing (a<
 » 6 months ago, # |   0 I am not able to understand problem D. Can anyone explain it?
 » 6 months ago, # |   0 In Problem E: My code works till 7th test case and then shows long long int overflow in diagonistics --"signed integer overflow: 58062342303970488 * 835 " says the error message,but in my code it must not happen because pdt=((pdt%MOD)*(NCR(val+y-1,y-1)%MOD))%MOD;I have already taken mod before multiplication!My submission : 32953649
•  » » 6 months ago, # ^ |   +1 It refers to the code inside your NCR function.