# CLOVER - Editorial

Practice

Medium-Hard

## PREREQUISITES:

Depth First Search,Segment Trees

## EXPLANATION:

Firstly, here we use the thing that if we dfs a tree and we keep a stack such that

we push a element to the stack when we arrive at it and pop the element from the

stack when we depart from it, therefore when we depart from node x the

elements on the stack are the elements which lie on the path from root to x

Here we use the same thing, we maintain an array (arr) of the maximum value in

the nodes and each index of this array works like the stack discussed above (i.e.

Suppose a node of value v is arrived we push the arrival time of this node to

arr[v] and next, suppose a node of value v is departed we pop the arrival time of

this node from arr[v]).

Now, all we need is the closest vertex with value greater than some value (x). For

that we have an array (arr2) of the maximum value in the nodes such that arr2[i]

stores the top of the stack at position i in “arr” array described above.

Now,we simply need the maximum value in the range (x,maximum

value in the nodes) which is actually the arrival time of the vertex closest to the

given vertex and has a value greater than the given value,

For this we apply segment tree to find the maximum value in a range and also we

apply point updates in the segment tree every time we arrive or depart a vertex

in the dfs run.

Since we can store the answer for each vertex beforehand in the dfs run we can

simply answer the queries in O(1) by maintaining an array such that the array[i]

stores the answer for the vertex i and update this array during the dfs run.

Total Complexity : O(Nlog(Max value)+Q)

## SETTER’S SOLUTION:

Can be found here

My code is being showed as a wrong answer even though it is getting executed successfully with the sample input . Please tell me where am I making the mistake . Thank you .

https://www.codechef.com/viewsolution/11780190

//
{"ft_lang_label":"__label__en","ft_lang_prob":0.8419289,"math_prob":0.96810937,"size":1771,"snap":"2021-31-2021-39","text_gpt3_token_len":428,"char_repetition_ratio":0.13695529,"word_repetition_ratio":0.045454547,"special_character_ratio":0.22416714,"punctuation_ratio":0.06738544,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9939779,"pos_list":[0,1,2],"im_url_duplicate_count":[null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-09-19T17:40:10Z\",\"WARC-Record-ID\":\"<urn:uuid:8d74f30b-e9b4-4be6-9bac-e4b711605df2>\",\"Content-Length\":\"16005\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:e409e16a-04d7-4ba9-9e29-0b6831a3a571>\",\"WARC-Concurrent-To\":\"<urn:uuid:270eadeb-762d-4741-8851-de161a451549>\",\"WARC-IP-Address\":\"54.166.204.247\",\"WARC-Target-URI\":\"https://discusstest.codechef.com/t/clover-editorial/13306\",\"WARC-Payload-Digest\":\"sha1:MH3MZEZSGCMEKOWOUOQMWOPRLUKUJU3G\",\"WARC-Block-Digest\":\"sha1:C2BQO4SE5XCDOVI53ISPMCQV4ST7D4IH\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-39/CC-MAIN-2021-39_segments_1631780056892.13_warc_CC-MAIN-20210919160038-20210919190038-00023.warc.gz\"}"}