Bzoj 2809 can maintain subtree information with heap

Source: Internet
Author: User

For each node, select as many nodes as possible in its subtree, and the weights and values of the nodes are less than a fixed value.

Build a large root heap, each node merges from the Son node, and pops the maximum value up to and meets the requirements.

1 /**************************************************************2 problem:28093 user:idy0024 language:c++5 result:accepted6 time:1224 Ms7 memory:6664 KB8 ****************************************************************/9  Ten#include <cstdio> One#include <algorithm> A #defineMax (a) (a) > (b)? ( A):(B)) - #defineN 100010 - using namespacestd; the   -typedefLong Longdnt; -   - #defineSzof (ND) (ND)? ( nd)->sz:0) + structNode { -     intV, SZ; + dnt s; ANode *ls, *rs; atInlinevoidUpdate () { -s = v + (ls?ls->s:0) + (Rs?rs->s:0);  -SZ =1+ szof (LS) +Szof (RS); -         if(Szof (LS) <Szof (RS)) swap (LS,RS); -     } -}pool[n], *tail=pool, *Root[n]; in   - intN, M; to intHead[n], dest[n], next[n], Etot; + dnt lead[n], cost[n]; - intQu[n], BG, ed, master; the   * voidAdde (intUintv) { $etot++;Panax NotoginsengDest[etot] =v; -Next[etot] =Head[u]; theHead[u] =Etot; + } ANode *newnode (intv) { theNode *nd = + +tail; +Nd->s = Nd->v =v; -Nd->ls = Nd->rs =0; $Nd->sz =1; $     returnnd; - } -Node *smerge (node *na, node *nb) { the     if(!na &&!NB)return 0; -     if(!na)returnnb;Wuyi     if(!NB)returnna; the     if(Na->v > nb->v) { -Na->rs = Smerge (na->RS, NB); WuNa->update (); -         returnna; About}Else { $Nb->rs = Smerge (nb->rs, NA); -Nb->update (); -         returnnb; -     } A } + intMain () { thescanf"%d%d", &n, &m); -      for(intI=1, p; i<=n; i++ ) { $scanf"%d%lld%lld", &p, Cost+i, lead+i); the         if(P) Adde (P, i); the         Elsemaster =i; the     } theQu[bg=ed=1] =Master; -      while(bg<=ed) { in         intu=qu[bg++]; the          for(intT=head[u]; T t=Next[t]) { the             intv=Dest[t]; AboutQu[++ed] =v; the         } the     } thednt ans =0; +      for(inti=ed; i>=1; i-- ) { -         intu=Qu[i]; theRoot[u] =NewNode (Cost[u]);Bayi          for(intT=head[u]; T t=Next[t]) { the             intv=Dest[t]; theRoot[u] =Smerge (Root[u], root[v]); -         } -          while(Root[u]->s >m) theRoot[u] = Smerge (Root[u]->ls, root[u]->rs); theans = max (ans, lead[u]*root[u]->sz); the     } theprintf"%lld\n", ans); -}
View Code

Bzoj 2809 can maintain subtree information with heap

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.