Left-leaning tree is a should be used to maintain the interval, and then need to merge operations and the invention of the algorithm, in fact, the algorithm is not difficult, and the tree is a bit like, maintain a few values, and then recursive back to the time can be modified.
The problem is dry:
background in a ninja gang, some ninjas are selected to be dispatched to the customer and rewarded for their work. Title description in this gang, there is a ninja called Master. In addition to master, each ninja has and has only one parent. To keep it private and to enhance the leadership of the Ninja, all instructions related to their work are always sent by the superior to his direct subordinates, and not by other means. Now you need to recruit a bunch of ninjas and send them to customers. You need to pay a certain salary for each ninja you send, and make the total amount you pay not exceed your budget. In addition, in order to send instructions, you need to select a ninja as the manager, ask the manager to send instructions to all the sent ninja, when sending instructions, any ninja (whether or not dispatched) can be the message of the sender. Managers themselves can be dispatched, or they may not be dispatched. Of course, you don't need to pay a manager's salary if the manager is not being taken. Your goal is to make the most of your customers ' satisfaction on budget. This defines the customer's satisfaction as the total number of ninjas dispatched is multiplied by the manager's leadership level, and each ninja's leadership level is also certain. Write a program that gives the top bi of each ninja I, salary ci, leadership Li, and the total salary of the ninja to pay the total budget m, outputting the maximum customer satisfaction when the budget meets the above requirements. Input and output format input format: The first line contains two integers n and M, where n represents the number of ninjas, and M represents the total budget for the salary. The next n lines describe the Ninja's superiors, their salary, and their leadership. The line I contains three integers bi,ci,li, respectively, representing the superior, salary, and leadership of the Ninja of the first. Master Meets BI=0, and each ninja's boss's number must be smaller than his own number bi<i. Output format: Outputs a number that represents the maximum value of customer satisfaction in the budget. Input and Output sample input example #1: Copy5 40 3 31 3 52 2 21 2 42 3 1Output Example #1: Copy6Description1≤n≤ -, thethe number of ninjas;1≤m≤1, the, the, theTotal salary budget;0≤bi <The number of the superior of the ninja;1≤ci≤m Ninja's salary;1≤li≤1, the, the, theNinja's leadership level. For -% of data, n≤ the。
Code:
#include <iostream>#include<cstdio>#include<cmath>#include<ctime>#include<queue>#include<algorithm>#include<cstring>using namespacestd;#defineDuke (I,a,n) for (int i = a;i <= n;i++)#defineLV (i,a,n) for (int i = a;i >= n;i--)#defineClean (a) memset (A,0,sizeof (a))Const intINF =1<< -; typedefLong LongLl;typedefDoubledb;template<classT>voidRead (T &x) { CharC; BOOLOP =0; while(c = GetChar (), C <'0'|| C >'9') if(c = ='-') op =1; X= C-'0'; while(c = GetChar (), C >='0'&& C <='9') x= x *Ten+ C-'0'; if(OP) x =-x;} Template<classT>voidwrite (T x) {if(X <0) Putchar ('-'), x =-x; if(x >=Ten) Write (X/Ten); Putchar ('0'+ x%Ten);}intN,m,len =0, lst[100005];intc[100005],l[100004];structnode{intL,R,NXT;} a[100005];voidAddintXinty) {a[++LEN].L =x; A[LEN].R=y; A[LEN].NXT=Lst[x]; LST[X]=Len;} ll ans=0; ll sum[100005],dis[100005];introot[100005],ls[100005],rs[100005],siz[100005],v[100005];intMergeintXinty) { if(!x | |!y)returnx |y; if(V[x] <V[y]) swap (x, y); RS[X]=merge (Rs[x],y); if(Dis[ls[x]) <dis[rs[x]) swap (ls[x],rs[x]); DIS[X]= Dis[rs[x]] +1; SIZ[X]= Siz[ls[x]] + siz[rs[x]] +1; SUM[X]= Sum[ls[x]] + sum[rs[x]] +V[x]; returnx;}voidNewNodeintx) {Sum[x]= V[x] =C[x]; SIZ[X]=1; Root[x] =x;}intDelintx) { returnmerge (Ls[x],rs[x]);}voidDfsintx) {newnode (x); for(intK = Lst[x];k;k =a[k].nxt) { inty =A[K].R; DFS (y); ROOT[X]=merge (Root[x],root[y]); } while(Sum[root[x]] > M && siz[root[x]]) root[x] =del (root[x]); Ans= Max (ans, (LL) siz[root[x]] *(LL) l[x]);}intMain () {read (n); read (m); Duke (I,1, N) { intx; Read (x); Read (c[i]); Read (L[i]); if(x) Add (x,i); } DFS (1); printf ("%lld\n", ans); return 0;}
P1522 dispatched left-leaning tree