Hdu_5788_level Up (tree-like array + chairman tree)

Source: Internet
Author: User

Topic Link: hdu_5788_level up

Test instructions

There is a tree, n nodes, each node has a capability value a[i],mid[i],mid The median of the sub-tree of the I node (including itself), and now lets you change one of the nodes to a value of 1e5, asking all of the mid's and the maximum number of questions.

Exercises

We can know that if you change one of the a[i], if a[i] is smaller than the mid of his father's node, then the median of his father's time will be shifted back one

For example, 1 2 3 4 5, the 3rd point is the 2nd point of the Father, if changed the second point of a value, then it becomes 1 3 4 5 1e5, the second point of the father's mid has become 4, compared to a backward move.

So we can use the Chair tree to support querying the median, and then preprocess the values of mid and mid+1 for each node.

Finally, a tree-like array +dfs is used to maintain a maximum difference.

1#include <bits/stdc++.h>2 #defineF (I,A,B) for (int i=a;i<=b;i++)3 using namespacestd;4typedefLong Longll;5 6 Const intn=1e5+7, p=1e5;7 intA[n],n,g[n],v[n],nxt[n],ed,x,tot,root[n],dfn[n],dfs_idx,sz[n],mid[n],val[n];8 ll Sum[n],ans,mx;9 TenInlinevoidAdgintXintY) {v[++ed]=y,nxt[ed]=g[x],g[x]=Ed;} One voidInit () {ed=tot=dfs_idx=0, ans=mx=0; memset (SUM,0,sizeof(sum)); Memset (G,0,sizeof(g));} AInlinevoidUpmax (ll &a,ll b) {if(a<b) a=b;} - //------------------A tree-like array -InlinevoidAddintXintc) { while(x<=p) sum[x]+=c,x+=x&-x;} theinline LL Ask (intx) {ll an=0; while(x) an+=sum[x],x-=x&-x;returnan ;} - //------------------Chairman Tree - structnode{intL,r,sum;} t[n* -]; -  +InlinevoidUpdateint&x,intYintPosintL=1,intR=P) - { +t[++tot]=t[y],t[tot].sum++,x=tot; A     if(L==R)return; at     intM=l+r>>1; -     if(pos<=m) update (T[X].L,T[Y].L,POS,L,M); -     ElseUpdate (t[x].r,t[y].r,pos,m+1, R); - } -  -InlineintQueryintXintYintKintL=1,intR=P) in { -     if(L==R)returnl; to     intM=l+r>>1, sum=t[t[x].l].sum-t[t[y].l].sum; +     if(k<=sum)returnquery (t[x].l,t[y].l,k,l,m); -     Else returnQuery (t[x].r,t[y].r,k-sum,m+1, R); the } *  $ voidPredfs (intu=1)Panax Notoginseng { -sz[u]=1, dfn[u]=++Dfs_idx; theUpdate (root[dfn[u]],root[dfn[u]-1],a[u]); +      for(intI=g[u];i;i=nxt[i]) Predfs (V[i]), sz[u]+=Sz[v[i]]; A     if(!g[u]) mid[u]=a[u],val[u]=p-a[u],ans+=A[u]; the     Else +     { -         ints=sz[u]+1>>1; $Mid[u]=query (root[dfs_idx],root[dfn[u]-1],s); $Val[u]=query (root[dfs_idx],root[dfn[u]-1],s+1)-Mid[u]; -ans+=Mid[u]; -     } the } - Wuyi voidDfsintu=1) the { - Add (Mid[u],val[u]); WuUpmax (Mx,ask (P)-ask (a[u]-1)); -      for(intI=g[u];i;i=Nxt[i]) DFS (v[i]); AboutAdd (mid[u],-Val[u]); $ } -  - intMain () - { A      while(~SCANF ("%d",&N)) +     { the init (); -F (I,1, N) scanf ("%d", A +i); $F (I,2, N) scanf ("%d",&x), ADG (x,i); the Predfs (), DFS (); theprintf"%lld\n", ans+MX); the     } the     return 0; -}
View Code

Hdu_5788_level Up (tree-like array + chairman 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.