### dreamplay's blog

By dreamplay, history, 2 weeks ago, ,

Hey there.

I have the following problem, not from any ongoing or past contest. Can you help me, by suggesting what optimal methods you have to solve it.

Node has three properties: left, right, height

N Insert node queries. Each query is (l, r, h).

Now M queries ask following: L, R, H
For all nodes(l, r, h), that satisfy L <= l && r <= R,
return min( abs( H — h) )

Can you guys suggest some approaches that you have in mind?

•
• +3
•

By dreamplay, history, 7 months ago, ,

I recently started learning basics of Java and got stuck on this doubt. How to write Java code for the below given C++ code. Note that making a class and passing an object or returning some object looks like a hack to me. So looking for other possible answers.

``````// c++ code
void f(int & x, int & y) {
x++;y++;
}
f(x, y); // function call somewhere
``````

Thanks

•
• -3
•

By dreamplay, history, 13 months ago, ,

Editorial will be completed soon!!

## Problem A

Code

This is an adhoc problem. We just need to consider all the possible cases of joining. Note that we can join 2 rectangles only along their common side lengths, if any.

For n = 1, it is trivial, check if it is a square.

For n = 2, only possible way is to join the 2 rectangles.

For n = 3, we have two cases. Either join them all linearly, all 3 in a row kind of fashion. Another way is to join the 2 rectangles to form a greater rectangle and then join third one, perpendicularly.

For all of the above cases, if it is possible to form a square, the side length of square is the largest length / breadth from rectangles.

## Problem B

Code

First of all, g(0) = 0.

Since g(g(x)) = -x then using this condition, g(g(g(0))) = -g(0) if we expand 2 outer g's and g(g(g(0))) = g(0) if we expand 2 inner g's. Thus, if g(0) = -g(0) then g(0) = 0.

Now, let g(a) = b then g(b) = g(g(a)) = -a, similarly g(-a) = g(g(b)) = -b and g(-b) = g(g(-a)) = a. Thus we get {a, b, -a, -b}, that is we need to divide the given integers into such groups.

Therefore, if r != -l, then answer is zero, as we need both positive and negative of a number.

Also, if r is odd, then answer is zero, as we need to divide into groups with exactly 2 positive and their negative integers. By using combinatorics, we can get the answer as follows: ** yet to write further**

## Problem C

Code

To find all triangles, we need to find number of ways to select 3 lines such that no 2 of them have same slope.

We can do this counting, by sorting all the lines by their slope. Now let the slope of 3 lines that we select be m1, m2 and m3, such that m1 < m2 < m3.

So we iterate on the 2nd line, that has slope m2 and for each such line, add the possible ways for selecting m1(<m2) and m3(>m2) ways.

## Problem D

Code

Since C(n, i + 1) = C(n, i) * (n — i) / (i + 1).
We can iterate on i from 1 to k, and keep some table to store the prime factorisation of each C(n, i).

Then we can know the maximum power of each prime that occured and thus their product will give us the required LCM.
Another solution for this problem can be to use a mathematical identity
Let X be the required answer, X = lcm { C(n, 0), C(n, 1), ...C(n, k)} % mod
then (n + 1) * X = lcm(n+1, n, n — 1, ..., n + 1 — k),
for proof of above identity see this
So now all we need is to find the lcm of (n + 1 — k, n + 1 — k + 1, ...n-1, n, n + 1)
We can do this by iterating over all the primes and then iterating on powers from 1 till the smallest power that is not divisible is found in this range.

## Problem E

Code

A suboptimal solution first.

Let us first look at the algorithm for finding Minimum Spanning tree.

We may observe that if the ordering of edges, when sorted by weights, does not change, then the minimum spanning tree remains to be unchanged i.e. the edges in MST remain to be same, even if we increase/decrease the edge weights.

So, if we considered all the pairs of edges, found the time when their edge weights become equal, then one of the edge weight becomes larger than the other one, forever, as they are linear functions of time.

Of course, in some cases, if they are identical, then always same.

Now, sort these O(m^2) times of intersections, we can say that minimum spanning tree between any of the two adjacent times, remains unchanged.
Thus we can find the MSTs for each of these times and answer the queries.
This will certainly TLE, O(m^3 log n).
The optimal solution uses binary search and attains following complexity.
precalculation:(O(log 10^8 * m log(m)))
each query:O(m log(m))
** optimal solution updated **
The sum of edge cost of every possible spaning tree is a linear function for t. So the MST function is a concave function. For solving this problem, we precalculate the highest position of the MST function by ternary search in range [-108, 108]. The time complexity is O(log(108) × m × log(m)). After finishing this step, we get the maximum MST value V_{max} where t is in range [\$-10^8\$, 108] and let such t as tmax.

Let the MST value of t = x is mst(x).

For each query, if Ti1 ≤ tmax ≤ Ti2, the answer is Vmax. If Ti2 ≤ tmax, the answer if mst(T_{i2}). Otherwise, the answer is mst(T_{i1}).

•
• +54
•

By dreamplay, history, 13 months ago, ,

Hi everyone!

I would like to invite you to the Weekly Training Farm 25 !! The problemsetter is me (dreamplay) and the tester and quality controller is dreamoon. Big thanks to drazil for his help in preparation.

It will be a 2 hour — 5 problems contest, with ACM-ICPC style scoring, will be held on codeforces, 1.5 hours after CF round #402.

Contest time: 17:00 UTC + 5.5
Contest link: Join the group (as participant) first and find Weekly Training Farm 25.

Editorial will be provided right after the contest.
Here are the standings from a recent Weekly Training Farm contest.

Good luck and hope you enjoy the problem solving!

UPD: 30 min to the start !!

UPD: contest is over. Hopefully, each of you liked atleast one problem.

Sadly, last problem went unsolved. Happy upsolving.
Editorial will be posted soon.
Congratulations to Winners.
1. zscoder
2. eddy1021
3. cchao
UPD: Here's the Editorial.

•
• +68
•

By dreamplay, history, 16 months ago, ,

Given a polygon of n points in counter-clockwise order and then some queries, result of each query being either INSIDE(point is inside or on the boundary) or outside (point outside the boundary)
This is the code I refer to.
Input:
3
0 0
2 0
2 2

1 1

Output:
Outside

Expected Output:
Inside ( point is on boundary )
Am I missing something, or is there an error while handling the boundary case? Similar error is also commented in russian on the link.

•
• +8
•

By dreamplay, history, 20 months ago, ,

Given n points (x,y) on a euclidean plane. A radius R.
Input: Q queries of form (a,b)
Output: For each query, points within radius R

Suggest a solution to this?

Expected complexity: Better than brute force, asymptotically.

•
• +16
•

By dreamplay, history, 2 years ago, ,

This is a reminder to the ACM ICPC Practise contest for the Amritapuri,India Site Preliminary Round.

The preliminary round will be held on 11th of October whereas Practise Contest will be held Tonight i.e. 6th October from 9pm to 2am, 7th October (IST/GMT+5:30)

Although the timings clash with Codeforces Round #324, here`s the Practise contest Link

Greetings.Thank you.

•
• +14
•

By dreamplay, history, 2 years ago, ,

A very warm Happy Birthday to our Greatest Gennady Korotkevich aka tourist.

I'd like to call upon all of you especially Petr on this special day, to say just 2-3 lines about him apart from wishing him.

Here's Mine: Tourist, is an inspiration to me and motivates me to work hard, not because of his unreachable ratings, but because of the humility that he has even after being the best and the fact that he still works hard to get better.

•
• +78
•

By dreamplay, 3 years ago, ,

It would be great if someone could explain how to exactly solve this problem .

I know about BIT , had even tried of using it during the contest but I don`t know how , even after reading the editorial. Please explain in a better way . Thanks .

Also it`d be great if you could suggest similar problems .

•
• +1
•

By dreamplay, 3 years ago, ,

I need to perform following queries What data structure can i use or modify to.

1) Insert/delete an element(comparable) , say integer , in some order such that i.e. iteration from smallest to largest element at any time , takes O(number of elements) .

2) number of elements greater than ei is given in O(logn) .

...I thought of adding an attribute at each node of r-b tree which stores number of elemts in subtree rooted at that element but this seems incorrect too .

•
• +7
•

By dreamplay, 3 years ago, ,

I have often encountered problems(in gym) which reduce to finding C(n,r) % p , where p is not necessarily prime . Also n and r are both large . Essentialy they demand for the use of Lucas and Chinese Remainder Theorem.

For constraints let`s take this problem .

This link does help a lot but I am still confused , especially in the part which asks to use C.R.Theorem . How to apply CRT?

Also what if p is not a square free number ?

Along with explaination , a link to code would be extremely helpful.

•
• +8
•

By dreamplay, 3 years ago, ,

http://www.spoj.com/SPOJ/problems/ABCPATH/

Consider the above problem which can possibly have a maximum of 2500 letters.

It is a dp on graphs problem,solvable by dfs.