Amazon Online Quiz Topic the recent common ancestor problem of Amazon (variant) Quadtree

Source: Internet
Author: User

The title means finding a minimum common ancestor (Lcp,least Common Father) that corresponds to the above two points on the tree shown in the link above, and then returns to their father node in order of comparison size. Specific code: Getfather (a) function is the code to find the father

#include <cstdio>#include<cstring>#include<iostream>#include<algorithm>using namespacestd; Const intMaxDepth = +; Long Longthree[maxdepth], sum[maxdepth]; voidinit () {three[0] =1; sum[0] =0;  for(intI=1; i<maxdepth;i++) {Three[i]= three[i-1] *3 ; Sum[i]= sum[i-1] +Three[i]; }        for(inti =0; i < maxDepth; i + +) {cout<< Three[i] <<"\ t"; } cout<<Endl;  for(inti =0; i < maxDepth; i + +) {cout<< Sum[i] <<"\ t"; } cout<<Endl; //cout << "int_max\t" << Int_max <<endl;}  intGetfather (Long Longa) {if(A <=3)        return 0; inti;  for(i=0; sum[i]<a;i++)        ; I-- ; intTMP = (2+a-sum[i])/3; intFather = Sum[i]-tmp +1; returnfather;}intLCP (intAintb) { while(A! =b) {if(A > B) a =Getfather (a); Elseb =Getfather (b); }    returnA;}intMain () {intA, B;    Init ();  while(SCANF ("%d%d", &a,&b)! =EOF) {        //cnt = 0;         intAns =LCP (A, b); printf ("%d\n", ans); }    return 0;}

Where three array saves 3 of the index
Sum Arry saves as many as 1, because index is starting from 0.

As you can see, sum[20] > Int_max, so as long as 21 elements in sum can cover all 0 to Int_max

At the same time, because THREE[20] and sum[20] > Int_max, so a long long to save.

Three array:1       3       9        -      Bayi      243     729     2187    6561    19683   59049   177147  531441  1594323 4782969 1434890743046721        129140163       387420489       1162261467      3486784401sum array:0       3        A       the       -     363     1092    3279    9840    29523   88572   265719  797160  2391483 7174452 2152335964570080        193710243       581130732       1743392199      5230176600Int_max214748364

Finally, the parent node of the current node is calculated alternately, knowing that the parent node is the same.

The calculation of Getfather is also relatively simple, compared to the sum array, find Sum[i] > node, and then I--, know that the node is on the row of the previous row.

The offerset= (node-sum[i]+2)/3 is then calculated as the offset from sum[i], so father= sum[i]-offerset + 1;

Example: To find the 17 father, find the < < 39, so 17 is in the next line of sum[2]= 12.

offset = (17+2-12)/3 = 2, which means 17 father distance sum[2] two distances. is actually a,

So father = Sum[2]-offset + 1 = 12-2 + 1 = 11.

Amazon Online Quiz Topic the recent common ancestor problem of Amazon (variant) Quadtree

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.