Hdu_4918_query on the subtree (tree partition + Tree Array)

Source: Internet
Author: User

Title Link: hdu_4918_query on the subtree

Test instructions

Give a tree of n points, each point has a weight, there are two operations, one is to change the weight of a point to v, and the other is to query the distance point you do not exceed the value of the point of D and.

Exercises

Here you can go to the film Bird God Blog.

The simple thing is to create two tree arrays for each center of gravity of the tree, then modify each point in the bit of the center of gravity, and query the bit in each center of gravity, and then let go of the Answer.

The complexity of each operation is log2n, the details of this problem is more, the specific look at the Code.

1#include <bits/stdc++.h>2 #defineF (i,a,b) for (int I=a;i<=b;i++)3 #definePB Push_back4 using namespacestd;5 Const intn=1e5+7;6 7 intn,q,g[n],nxt[n*2],v[n*2],ed,w[n],vis[n],id[n];8 intpool[ +*n],c_ed,pool_ed,sz[n],mi,mx[n],root;9 Charop[2];Ten  one structnode a { -     intrt,subrt,dis; - node () {} theNodeint_rt,int_subrt,int_dis): rt (_rt), subrt (_subrt), dis (_dis) {} - }tmp; -Vector<node>vt[n]; -  + voidAdgintXintY) {v[++ed]=y,nxt[ed]=g[x],g[x]=ed;} - voidInit () {ed=c_ed=pool_ed=0; F (i,1, N) vt[i].clear (), vis[i]=g[i]=0;} +InlinevoidUpint&a,intB) {if(a<b) a=b;} a  at structBIT - { -     int*c,n; -     voidInitintTot) {n=tot,c=pool+pool_ed,pool_ed+=tot+1; F (i,0, N) c[i]=0;} -InlinevoidAddintXintC) { while(x<=n) c[x]+=c,x+=x&-x;} -InlineintAskintXintan=0) in     { -         if(x>n) x=n; to          while(x>0) an+=c[x],x-=x&-x; +         returnan ; -     } the}tr[n*2]; *  $ voidGet_rt (intUintFaintNum)Panax Notoginseng { -sz[u]=1, mx[u]=0; the      for(intI=g[u];i;i=nxt[i]) +         if(!vis[v[i]]&&v[i]!=Fa) a         { the Get_rt (v[i],u,num); +sz[u]+=sz[v[i]],up (mx[u],sz[v[i]); -         } $Up (mx[u],num-sz[u]); $     if(mx[u]<mi) Root=u,mi=mx[u]; - } -  the voidDelintUintFaintRtintsubrt,intdis=1)//place each point of the subtree in the corresponding center of gravity - {Wuyi VT[U].PB (node (rt,subrt,dis)); theTr[rt].add (dis+1, w[u]); -Tr[subrt].add (dis+1, w[u]); wu      for(intI=g[u];i;i=nxt[i]) -         if(v[i]!=fa&&!vis[v[i]]) aboutDel (v[i],u,rt,subrt,dis+1); $ } -  - voidInit_tree (intu=1,intnum=N) - { aMi=N,get_rt (u,u,num); +     intrt=root,rt_id=++c_ed; theTr[c_ed].init (sz[u]+2); -vis[rt]=1, VT[RT].PB (node (c_ed,0,0)); $Tr[c_ed].add (1, w[rt]); theGet_rt (rt,rt,num);//new calculation of sz size the      for(intI=g[rt];i;i=nxt[i]) the         if(!vis[v[i]]) the         { -Tr[++c_ed].init (sz[v[i]]+2); in del (v[i],v[i],rt_id,c_ed); the         } the      for(intI=g[rt];i;i=nxt[i]) about         if(!vis[v[i]]) the Init_tree (v[i],sz[v[i]); the } the  + intMain () - { the      while(~SCANF ("%d%d",&n,&q))Bayi     { the Init (); theF (i,1, N) scanf ("%d", w+i); -F (i,1, n-1) -         { the             intx, y; thescanf"%d%d",&x,&y); the ADG (x, y), ADG (y,x); the         } - Init_tree (); the          while(q--) the         { the             intu,v;94scanf"%s%d%d",op,&u,&v); the             if(op[0]=='!') the             { the                 intSize=vt[u].size (), d=v-w[u];98F (i,0, size-1) about                 { -tmp=vt[u][i];101Tr[tmp.rt].add (tmp.dis+1, d);//dis+1 Remove the bug that the distance is 0 timeout on bit102                     if(tmp.subrt) Tr[tmp.subrt].add (tmp.dis+1, d);103                 }104w[u]+=d; the}Else106             {107                 intd=v,ans=0, size=vt[u].size ();108F (i,0, size-1)109                 { thetmp=vt[u][i];111Ans+=tr[tmp.rt].ask (d-tmp.dis+1); the                     if(tmp.subrt) Ans-=tr[tmp.subrt].ask (d-tmp.dis+1);113                 } theprintf"%d\n", ans); the             } the         }117     }118     return 0;119}
View Code

Hdu_4918_query on the subtree (tree partition + Tree Array)

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.