{"trustable":true,"sections":[{"title":"","value":{"format":"HTML","content":" Andrew has just made a breakthrough in complexity theory: he thinks that he can prove \u003ci\u003eP\u003c/i\u003e\u003dNP if he can get a data structure which allows to perform the following operation quickly. Naturally, you should help him complete his brilliant research. Consider a rooted tree with integers written in the leaves. For each internal (non-leaf) node \u003ci\u003ev\u003c/i\u003e of the tree you must compute the minimum absolute difference between all pairs of numbers written in the leaves of the subtree rooted at \u003ci\u003ev\u003c/i\u003e. \u003cbr\u003e\u003cdiv align\u003d\"left\" style\u003d\"margin-top: 1.0em;\"\u003e\u003cb\u003eInput\u003c/b\u003e\u003c/div\u003eThe first line of the input file contains two integers \u003ci\u003en\u003c/i\u003e and \u003ci\u003em\u003c/i\u003e\u0026nbsp;— overall number of nodes in the tree and number of leaves in the tree respectively. \u003cimg src\u003d\"CDN_BASE_URL/eebc42c65c8fff3f14089f6caa6c19c0?v\u003d1714219792\" style\u003d\"vertical-align: text-bottom;top: -2.0px;max-width: 100.0%;max-height: 100.0%;\"\u003e. All nodes are numbered from 1 to \u003ci\u003en\u003c/i\u003e. Node number 1 is always the root of the tree. Each of the other nodes has a unique parent in the tree. Each of the next \u003ci\u003en\u003c/i\u003e - 1 lines of the input file contains one integer\u0026nbsp;— the number of the parent node for nodes 2, 3,..., \u003ci\u003en\u003c/i\u003e respectively. Each of the last \u003ci\u003em\u003c/i\u003e lines of the input file contains one integer ranging from \u003cimg src\u003d\"CDN_BASE_URL/ec3f60891c39686c685ccbfcaf44f8fb?v\u003d1714219792\" style\u003d\"vertical-align: text-bottom;top: -2.0px;max-width: 100.0%;max-height: 100.0%;\"\u003e to \u003cimg src\u003d\"CDN_BASE_URL/ebab0eb3473c761f3ef81d9018f5d9c3?v\u003d1714219792\" style\u003d\"vertical-align: text-bottom;top: -2.0px;max-width: 100.0%;max-height: 100.0%;\"\u003e — the value of the corresponding leaf. Leaves of the tree have numbers from \u003ci\u003en\u003c/i\u003e - \u003ci\u003em\u003c/i\u003e + 1 to \u003ci\u003en\u003c/i\u003e. \u003cbr\u003e\u003cdiv align\u003d\"left\" style\u003d\"margin-top: 1.0em;\"\u003e\u003cb\u003eOutput\u003c/b\u003e\u003c/div\u003eOutput one line with \u003ci\u003en\u003c/i\u003e - \u003ci\u003em\u003c/i\u003e integers: for each internal node of the tree output the minimum absolute difference between pairs of values written in the leaves of its subtree. If there is only one leaf in the subtree of some internal node, output number 2\u003csup\u003e31\u003c/sup\u003e - 1 for that node. Output the answers for the nodes in order from node number 1 to \u003ci\u003en\u003c/i\u003e - \u003ci\u003em\u003c/i\u003e. \u003cbr\u003e"}},{"title":"Sample 1","value":{"format":"HTML","content":"\u003ctable class\u003d\u0027vjudge_sample\u0027\u003e\n\u003cthead\u003e\n \u003ctr\u003e\n \u003cth\u003eInput\u003c/th\u003e\n \u003cth\u003eOutput\u003c/th\u003e\n \u003c/tr\u003e\n\u003c/thead\u003e\n\u003ctbody\u003e\n \u003ctr\u003e\n \u003ctd\u003e\u003cpre\u003e5 4\n1\n1\n1\n1\n1 \n4 \n7 \n9\n\u003c/pre\u003e\u003c/td\u003e\n \u003ctd\u003e\u003cpre\u003e2\n\u003c/pre\u003e\u003c/td\u003e\n \u003c/tr\u003e\n\u003c/tbody\u003e\n\u003c/table\u003e\n"}},{"title":"Sample 2","value":{"format":"HTML","content":"\u003ctable class\u003d\u0027vjudge_sample\u0027\u003e\n\u003cthead\u003e\n \u003ctr\u003e\n \u003cth\u003eInput\u003c/th\u003e\n \u003cth\u003eOutput\u003c/th\u003e\n \u003c/tr\u003e\n\u003c/thead\u003e\n\u003ctbody\u003e\n \u003ctr\u003e\n \u003ctd\u003e\u003cpre\u003e5 4\n1\n1\n1\n1\n1 \n4 \n7 \n10\n\u003c/pre\u003e\u003c/td\u003e\n \u003ctd\u003e\u003cpre\u003e3\n\u003c/pre\u003e\u003c/td\u003e\n \u003c/tr\u003e\n\u003c/tbody\u003e\n\u003c/table\u003e\n"}},{"title":"Sample 3","value":{"format":"HTML","content":"\u003ctable class\u003d\u0027vjudge_sample\u0027\u003e\n\u003cthead\u003e\n \u003ctr\u003e\n \u003cth\u003eInput\u003c/th\u003e\n \u003cth\u003eOutput\u003c/th\u003e\n \u003c/tr\u003e\n\u003c/thead\u003e\n\u003ctbody\u003e\n \u003ctr\u003e\n \u003ctd\u003e\u003cpre\u003e7 4\n1\n2\n1\n2\n3\n3\n2 \n10 \n7 \n15\n\u003c/pre\u003e\u003c/td\u003e\n \u003ctd\u003e\u003cpre\u003e3 3 8\n\u003c/pre\u003e\u003c/td\u003e\n \u003c/tr\u003e\n\u003c/tbody\u003e\n\u003c/table\u003e\n"}},{"title":"Sample 4","value":{"format":"HTML","content":"\u003ctable class\u003d\u0027vjudge_sample\u0027\u003e\n\u003cthead\u003e\n \u003ctr\u003e\n \u003cth\u003eInput\u003c/th\u003e\n \u003cth\u003eOutput\u003c/th\u003e\n \u003c/tr\u003e\n\u003c/thead\u003e\n\u003ctbody\u003e\n \u003ctr\u003e\n \u003ctd\u003e\u003cpre\u003e2 1\n1\n100\n\u003c/pre\u003e\u003c/td\u003e\n \u003ctd\u003e\u003cpre\u003e2147483647 \n\u003c/pre\u003e\u003c/td\u003e\n \u003c/tr\u003e\n\u003c/tbody\u003e\n\u003c/table\u003e\n"}}]}