Hiho 1232 Beijing online race F Couple Trees

Source: Internet
Author: User

Give two markings from the 1...N tree, ensure that the marking of small points must be on top. Each time you ask for x points on a tree, and y points on the B tree go up at the same time, the nearest meeting point and the X, Y distance to this point.

I wanted to do it with a doubling of LCA, but it was a slag .... Then I saw that the Chairman Tree wrote a

Dumb Horse:

1#include <iostream>2#include <cstdio>3#include <algorithm>4#include <cstring>5#include <cmath>6#include <vector>7 #defineINF 10000000078 #defineMAXN 1000209 #defineMAXM 4200Ten  One using namespacestd; A  - intFA[MAXN],GA[MAXN],LL[MAXN],RR[MAXN],DEP[MAXN],GAP[MAXN]; - intn,m,cnt; the  - structnode - { -Node *LC, *RC; +     intkey, CoV; - node () { +key=0; Acov=0; atLc=rc=NULL; -     } - }; -  - structChairmantree - { inNode *tree,*ROT[MAXN]; -     inttot; toNode *new_node () {return&tree[++tot];} +Node *build (intLintR) -     { theNode *now=New_node (); *now->cov=now->key=0; $         if(L==R)returnNow ;Panax Notoginseng         intMid= (l+r) >>1; -Now->lc=build (L,mid); theNow->rc=build (mid+1, R); +         returnNow ; A     } the  +     voidDown (node *p) -     { $         if(p->CoV) $         { -Node *nl=New_node (); -Node *nr=New_node (); the*nl=*p->LC; -*nr=*p->RC;WuyiNl->key=max (p->lc->key,p->CoV); theNl->cov=max (p->lc->cov,p->CoV); -Nr->key=max (p->rc->key,p->CoV); WuNr->cov=max (p->rc->cov,p->CoV); -P->lc=NL; AboutP->rc=nr; $p->cov=0; -         } -     } -  ANode *change (node *p,intLintRintllintRrintx) +     { theNode *now=New_node (); -         if(Ll<=l && r<=RR) $         { the*now=*p; theNow->key=max (now->key,x); theNow->cov=max (now->cov,x); the             returnNow ; -         } in Down (p); the*now=*p; the         intMid= (l+r) >>1; About         if(Ll<=mid) Now->lc=change (p->LC, L, Mid, LL, RR, X); the         if(Rr>mid) Now->rc=change (P-&GT;RC, mid+1, R, LL, RR, X); the         returnNow ; the     } +  -     intQuery (node *now,intLintRintx) the     {Bayi         if(L==R)returnNow->key; the Down (now); the         intMid= (l+r) >>1; -         if(X<=mid)returnQuery (now->LC, L, Mid, X); -         Else returnQuery (NOW-&GT;RC, mid+1, R, x); the     } the  the     voidWrite (node *now,intLintR) the     { -         if(l==R) the         { theCout<<now->key; the             return ;94         } the Down (now); the         intMid= (l+r) >>1; theWrite (now->lc,l,mid);98Write (now->rc,mid+1, R); About     } - 101     voidInitintN)102     {103Tree=Newnode[n* the];104tot=0; the     }106     voidClear ()107     {108         Delete[] tree;109     } the }s;111  thevector<int>E[MAXN];113  the voidDfsintx) the { thell[x]=++CNT;117     intn=e[x].size ();118      for(intI=0; i<n;i++)119     { -         intk=E[x][i];121 Dfs (k);122     }123rr[x]=CNT;124 } the 126 intMain ()127 { -     //freopen ("f.in", "R", stdin);129      while(SCANF ("%d%d", &n,&m)! =EOF) the     {131 s.init (n); thefa[1]=ga[1]=0;133dep[1]=gap[1]=1;134          for(intI=1; i<=n;i++) e[i].clear ();135          for(intI=2; i<=n;i++) scanf ("%d", &fa[i]), E[fa[i]].push_back (i), dep[i]=dep[fa[i]]+1;136          for(intI=2; i<=n;i++) scanf ("%d", &ga[i]), gap[i]=gap[ga[i]]+1;137Cnt=0;138Dfs1);139          $s.rot[0]=s.build (1, n);141         142          for(intI=1; i<=n;i++)143S.rot[i]=s.change (S.rot[ga[i]),1, n,ll[i],rr[i],i);144         intx,y,ans=0;145          for(intI=1; i<=m;i++)146         {147scanf"%d%d",&x,&y);148x= (X+ans)%n+1, y= (Y+ans)%n+1;149Ans=s.query (S.rot[y],1, n,ll[x]); Maxprintf"%d%d%d\n", ans,dep[x]-dep[ans]+1, gap[y]-gap[ans]+1);151         } the s.clear ();153     }154     return 0;155}
Couple Tree

Hiho 1232 Beijing online race F Couple Trees

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.