[Bzoj 2588] Count on a tree

Source: Internet
Author: User

Transmission Door

lca+ tree (can persist line tree)

Take a point as the root, and each segment tree records the distribution of the weights on the axis of the node to the root of the tree (of course discretization),

The distribution of the number on the axis of the two-point u,v path can be expressed as TREE[U]+TREE[V]-TREE[LCA (u,v)]-tree[fa (u,v)] (you can draw yy for a moment),

Then you can query the tree you got.

Code:

#include <bits/stdc++.h>using namespacestd;#defineN ((1<<17)-1)intn,m,rv[n],v[n],dep[n],jp[n][ -],cnt,p[n];BOOLVis[n];vector<vector<int> >G;structsn{SN*son[2]; intVal;} Sn[n* -],*Root[n];voidBuild (Sn&x,intLintR) {    if(L==R)return; intM= (l+r) >>1; x.son[0]=&sn[++CNT]; x.son[1]=&sn[++CNT]; Build (*x.son[0],l,m); Build (*x.son[1],m+1, R);} InlineBOOLcmpintAintb) {returnrv[a]<rv[b];}voidPutin () {intI,x,y; scanf ("%d%d",&n,&m); G.resize (n+1);  for(i=1; i<=n;i++) scanf ("%d", &rv[i]), p[i-1]=i; Sort (p,p+n,cmp);  for(i=0; i<n;i++) v[p[i]]=i;  for(i=0; i<n-1; i++) {scanf ("%d%d",&x,&y);        G[x].push_back (y);    G[y].push_back (x); } Build (sn[0],0, N-1); root[0]=&sn[0];}voidRebuild (Sn&x,intLintRintk) {X.val++; if(L==R)return; intM= (l+r) >>1, S; if(k<=m) s=0, r=m; Elses=1, l=m+1; sn[++cnt]=*X.son[s]; X.son[s]=&sn[cnt]; Rebuild (*x.son[s],l,r,k);}voidDfsintXintFintd) {    inti; VIS[X]=1; DEP[X]=D; jp[x][0]=F;  for(i=0; jp[x][i];i++) Jp[x][i+1]=Jp[jp[x][i]][i]; ROOT[X]=&sn[++CNT]; *root[x]=*Root[f]; Rebuild (*ROOT[X],0, N-1, v[x]);  for(i=0; I<g[x].size (); i++)        if(!vis[g[x][i]]) DFS (g[x][i],x,d+1);}intSearch (Sn&a,sn&b,sn&c,sn&d,intLintRintk) {    if(L==R)returnl; intVal= (*a.son[0]). val+ (*b.son[0]). val-(*c.son[0]). val-(*d.son[0]). val; intM= (l+r) >>1, S; if(val>=k) s=0, r=m; Elses=1, k-=val,l=m+1; returnSearch (*a.son[s],*b.son[s],*c.son[s],*d.son[s],l,r,k);}intLCA (intUintv) {    inti; if(dep[u]!=Dep[v]) {        if(dep[u]<Dep[v]) swap (U,V);  for(i= -; i>=0; i--)            if(Dep[u]-(1<<i) >=dep[v]) u=Jp[u][i]; }    if(U==V)returnu;  for(i= -; i>=0; i--)        if(jp[u][i]!=Jp[v][i]) {u=Jp[u][i]; V=Jp[v][i]; }    returnjp[u][0];}voidAnswer () {inti,ans=0, U,v,k,lca;  for(i=0; i<m;i++) {scanf ("%d%d%d",&u,&v,&k); U^=ans; LCA=LCA (U,V); Ans=rv[p[search (*root[u],*root[v],*root[lca],*root[jp[lca][0]],0, N-1, K)]; printf ("%d", ans); if(i<m-1) printf ("\ n"); }}intMain () {Putin (); DFS (1,0,1); Answer ();}
View Code

Gmt
Detect languageAlbanianArabicAzerbaijani languageIrishEstonianBasque languageBelarusian languageBulgarianIcelandicPolishBosnianPersianBoolean language (Afrikaans)DanishGermanRussianFrenchFilipinoFinnishKhmer languageGeorgian languageGujaratiKazakhHaitian CreoleKoreanHausa languageDutchGalicianCatalanCzechKannada languageCroatianLatin languageLatvianLao languageLithuanianRomanian languageMalagasy languageMalteseMarathiMalayalamMalayFYRO MacedonianMaoriMongolianBengaliBurmese languageHmongZulu, South AfricaNepalese languageNorwegianPunjabiPortugueseChichewa languageJapaneseSwedishSerbian languageSesotho languageSinhala languageWorld languageSlovakSlovenianSwahiliCebu languageSomalia languageTajik languageTeluguTamilThaiTurkishWelshUrdu languageUkrainianUzbek languageHebrewGreekSpanishHungarianArmenianIgbo languageItalianYiddishHindiIndonesian SundaIndonesian languageIndonesian JavaneseEnglishYorubaVietnameseChinese SimplifiedChinese Traditional AlbanianArabicAzerbaijani languageIrishEstonianBasque languageBelarusian languageBulgarianIcelandicPolishBosnianPersianBoolean language (Afrikaans)DanishGermanRussianFrenchFilipinoFinnishKhmer languageGeorgian languageGujaratiKazakhHaitian CreoleKoreanHausa languageDutchGalicianCatalanCzechKannada languageCroatianLatin languageLatvianLao languageLithuanianRomanian languageMalagasy languageMalteseMarathiMalayalamMalayFYRO MacedonianMaoriMongolianBengaliBurmese languageHmongZulu, South AfricaNepalese languageNorwegianPunjabiPortugueseChichewa languageJapaneseSwedishSerbian languageSesotho languageSinhala languageWorld languageSlovakSlovenianSwahiliCebu languageSomalia languageTajik languageTeluguTamilThaiTurkishWelshUrdu languageUkrainianUzbek languageHebrewGreekSpanishHungarianArmenianIgbo languageItalianYiddishHindiIndonesian SundaIndonesian languageIndonesian JavaneseEnglishYorubaVietnameseChinese SimplifiedChinese Traditional
Language features limited to 100 character options: History: Help: Anti-feedback

[Bzoj 2588] Count on a tree

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.