Tutorial is loading...

Tutorial is loading...

Tutorial is loading...

Tutorial is loading...

Tutorial is loading...

Tutorial is loading...

# | User | Rating |
---|---|---|

1 | Benq | 3796 |

2 | tourist | 3722 |

3 | Radewoosh | 3719 |

4 | ecnerwala | 3578 |

5 | ksun48 | 3462 |

6 | Um_nik | 3456 |

7 | maroonrk | 3445 |

8 | jiangly | 3431 |

9 | Petr | 3370 |

10 | scott_wu | 3350 |

# | User | Contrib. |
---|---|---|

1 | 1-gon | 207 |

2 | awoo | 186 |

3 | rng_58 | 184 |

4 | Errichto | 182 |

5 | SecondThread | 177 |

5 | Radewoosh | 177 |

7 | -is-this-fft- | 176 |

7 | maroonrk | 176 |

9 | Um_nik | 173 |

10 | antontrygubO_o | 170 |

Tutorial is loading...

Tutorial is loading...

Tutorial is loading...

Tutorial is loading...

Tutorial is loading...

Tutorial is loading...

↑

↓

Codeforces (c) Copyright 2010-2021 Mike Mirzayanov

The only programming contests Web 2.0 platform

Server time: Jun/15/2021 22:21:12 (i1).

Desktop version, switch to mobile version.

Supported by

User lists

Name |
---|

What about using a persistent segment tree for F? We let version=depth and it should run in .

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.

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)

how to solve 3rd question rumor

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.

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

An integer isn't always large enough to contain the final solution, as it can be up to c*n, which is 10^14.

So you have to take integer of 64bit to store final answer

in E — in O(1) (by precalcing factorials and inverse factorials)

https://en.wikipedia.org/wiki/Stirling_numbers_of_the_second_kind

in order to solve the problem you had to calculate s(n,k). How can you calculate this with O(1)?

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) }

I think he means you can query/access value in O(1), but I'm sure the time to precompute takes longer.

Can someone please explain Div 2 F ? I am not able to understand it from the editorial ?

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]] = <depth[u], C[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.

Can you elaborate the pre -processing part? Also, can you explain the over-all logic behind your code?

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 <depth, cost> 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 <depth, cost> 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.

Can we build a segment tree inside every node of the original tree, to get the minimum cost, in the given range?

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).

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.

Problem C can be solved using disjoint set too!

it is saying TLE .I have used disjoint set .55217078

You are missing the path compression. Your parent function has $$$O(n)$$$ complexity.

Got it Thanx.

I used disjoint set..but can't understand why I am getting WA..can u please explain? https://codeforces.com/contest/893/submission/88613755 here is my code

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?

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.

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.

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: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.

You could use array of boolean to flag which node/vertex already visited. So it looks like this:

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

I can't understand it too

I believe this article is about this combinatorics problem:

https://en.m.wikipedia.org/wiki/Stars_and_bars_(combinatorics)

Thank you :D

Thank you!

in problem D a0<0 how it is possible because starting amount of account=0.

Good Contest

Can someone explain me "Segment Tree structure" solution for "F" ?

Hi, i explained my solution using segment tree here (on this same post)

thanks a lot, my inattention(

Can someone explain to me why my solution for D is wrong? https://paste.ubuntu.com/26042298/

For each

ai = 0I supppose that I deposited the maximum amount of money that I am allowed to deposit. Then, if my money at bank is greater thand, 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.Accepted!

I was missing something important!

You can check my solution here: 32652462

Hi, what should be the output for 4 10 -10 -10 -10 0

Your solution gives output as 1. I think it should be 2 with final solution as 10 -10 10 0. Am I missing something here ?

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.

Okay got it, actually I thought that the amount of money that can be deposited also cannot be greater than the limit. Thanks !!

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 .

Can someone provide a proof for editorial for E?

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]

Can someone explain the example input of F, why the output is "2 5"? I think I misunderstood the problem somehow...

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.

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?

nCr = nCn-r

Thanks bro

For f we can also use fractional cascading wich gives us a solution with (n+q)log

It's been a while since this contest, but could you explain this a bit more?

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

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 :)

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

You can do it without dfs/bfs. Check the union find algorithm or you can check my solution

hey u have also uses the iterative approach or the recursive one with dfs

Can someone post their approach for the problem D (Credit Card) in Div 2?

Maybe I am too late to answer this, if not, it can be helpful . The first idea we get is , maintain a sum, till ith number, if corresponding query is check , and sum is less than zero, increment out result by 1. But that's not true. Example 7 7 -1 0 -1 -1 -1 0 -1 we may answer it 2 , by the greedy approach explained , but answer should be 1.

So how we actually solve this ? Well, instead of trying to add the smallest possible number, we should try adding the largest possible, such that , the total don't exceed the given limit at any index . So , while we are at an index having 0(check) We find the maximum value we can add to current index, and hence all elements after this, such that limit is maintained. We need to answer range max queries , on prefix sum array You can use, sparse table or any other data structure for this.

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

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).

In E, can someone explain this ? And why do we multiply answer every time when we encounter new prime?

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)

Thank you!

I got the first part(stars and bars one), but why do we multiply answer every time when we encounter new prime?

upd: i got it now.

can you explain it to me why we are multiplying?

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<<step) instead of (1<<step) but i have no idea why did that cause MLE.

I am not able to understand problem D. Can anyone explain it?

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

It refers to the code inside your NCR function.

In problem C I am doing it using DFS and summing up all minimums, I really don't know why it gives me TLE in test case 31. Can anyone help me please? Here is my solution: https://codeforces.com/contest/893/submission/57108433

my code is not working (rumor) can someone help me?

## include<bits/stdc++.h>

using namespace std;

vector v[10000009];

long long w,y,n,ans,MIN,l[10000009],k,i; bool a[10000009],ok; void bfs(long long o) { if(a[o]==false) { a[o]=true; if(l[o]<MIN) MIN=l[o]; if(v[o].size()-1!=0) for(k=1;k<v[o].size();k++) { if(a[v[o][k]]==false) bfs(v[o][k]); } } } int main() { long long m; cin>>n>>m; for(i=1;i<=n;i++){ v[i].push_back(1); } for( i=1;i<=n;i++) { cin>>l[i]; }

cout<<ans; }

Problem E can be solved using pure Segment Tree with complexity O((m+n)log(n)). First, run Eulerian ordering (using DFS) on this root tree. After that we got an array 2n elements where 2 adjacent elements have depth difference <= 1. We'll build segment tree base on this array. Each node [l, r] we store array of length r-l+1. Each i-th element of this array is minimum value which has depth <= i. Sorry for poor explanation. Sound like crazy but I got accepted submission. Pls check it out if you curious. 74651014

Can anyone please tell why i am getting Runtime Error in

Problem C. I used DFS to find CC. Submission Link Cheers!can pls anyone tell me whats wrong with my code for rumors?

D. Credit Card question is not clear at all. Problem setters should make questions understandable !

Alternate solution for F : Build a merge sort tree on the euler tour of the given rooted tree, with list sorted by height, giving prefix minimum of assigned id's. To speed up query in merge sort tree from Log^2(N) to Log(N), use fractional cascading. Solution works easily in less than 2 seconds.