Editorial: Programming Contest #1 (Chapter 'Ramayan')

Revision en15, by vok8, 2020-05-27 19:56:27

-

Laxman and Maths

Logic (Explanation)
Problem Setter's Code

-/-/-

Raavan and Subarray

Logic (Explanation)
Problem Setter's Code

-/-/-

Bharat and Possibilities

Logic (Explanation)
Problem Setter's Code

-/-/-

Hanuman and Tree

Logic (Explanation)

Let’s create the two-dimensional array dis[node][deg], which stores the ancestor of node at distance 2deg.

If we fill this dis array, we can answer every query in lg(n) time, using Binary Lifting.

dis[node][0] = parent(node), for every node (except root)

dis[root][0] = -1, for root node.

So, now for deg from 1 to lg(n) and for each node,

dis[node][deg] = dis[dis[node][deg-1]][deg-1].

So, now, for each query node d, convert d into its binary representation and for every set bit in this binary representation, we replace node to dis[node][set-bit position from right], till the value of node doesn't become -1 or till all the set-bits of d are iterated.

Time Complexity: `O((n+q)*

Tags #contest, #coding, #quarantine, #ramayan, #algorithms, #editorial, #codechef

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en17 English vok8 2020-05-27 20:22:36 14893 Reverted to en14
en16 English vok8 2020-05-27 20:02:07 5 Tiny change: 'sting)\n\n-\n\n[Laxma' -> 'sting)\n\n[Laxma'
en15 English vok8 2020-05-27 19:56:27 14898
en14 English vok8 2020-05-27 17:48:48 1 Tiny change: 'nd the **nth** term ' -> 'nd the **n'th** term '
en13 English vok8 2020-05-24 16:26:02 5 Tiny change: 'in `lg(n)`, using [B' -> 'in `lg(n)` time, using [B'
en12 English vok8 2020-05-24 12:22:39 0 Tiny change: 'iler>\n\n-/-/-\n\n[Raav' -> 'iler>\n\n---\n\n[Raav'
en11 English vok8 2020-05-24 11:01:31 8 Tiny change: ' distance `2`<sup>`deg`</sup>.\n\nIf we' -> ' distance **2<sup>deg</sup>**.\n\nIf we'
en10 English vok8 2020-05-24 10:59:46 11
en9 English vok8 2020-05-19 21:53:48 8 Tiny change: 'g)\n\nSo, the best way to create' -> 'g)\n\nSo, one of the best ways to create'
en8 English vok8 2020-05-19 18:55:52 383
en7 English vok8 2020-05-19 17:57:26 6 Tiny change: 'ity**: `O(lg(n))`, per te' -> 'ity**: `O(1)`, per te'
en6 English vok8 2020-05-19 17:55:41 191
en5 English vok8 2020-05-19 15:55:08 1381
en4 English vok8 2020-05-19 15:33:37 544
en3 English vok8 2020-05-19 15:03:38 6 Tiny change: 'm `1` to `n` and for ' -> 'm `1` to `log2(n)` and for '
en2 English vok8 2020-05-19 14:32:48 159
en1 English vok8 2020-05-19 14:04:38 19655 Initial revision (published)