Bzoj2809:dispatching (persistent segment Tree + tree differential)

Source: Internet
Author: User
Tags time limit

2809: [apio2012]dispatching

Time Limit:10 Sec memorylimit:128 MB

Description

In a ninja gang, some ninjas are selected to be dispatched to the customer and rewarded for their work. In this gang, a ninja is 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, if the manager is not being removed, there is no need to pay the manager's salary. 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 l I, and the salary budget M paid to the ninja, outputting the maximum customer satisfaction when the budget meets the above requirements.

The number of 1≤n≤100,000 ninjas;

1≤m≤1,000,000,000 salary total budget;

0≤bi < I ninja's superior number;

1≤ci≤m Ninja's salary;

1≤li≤1,000,000,000 Ninja's leadership level.

Input

Reads data from standard input.

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 whole Bi, C I, L I, respectively, represent the superior, salary and leadership of the first ninja. Master satisfies b i = 0, and the number of each ninja's boss must be smaller than its own number Bi < I.

Output

Outputs a number that represents the maximum value of customer satisfaction in the budget.

Sample Input

5 4

0 3 3

1 3 5

2 2 2

1 2 4

2 3 1

Sample Output

6

HINT
If we choose a ninja numbered 1 as a manager and dispatch a third and fourth Ninja, the sum of the salaries is 4, not exceeding the total budget of 4. Since 2 ninjas were dispatched and the manager's leadership was 3,
User satisfaction is 2, is the maximum number of user satisfaction can be obtained.

Topic Analysis: I began to think of a treap+ heuristic merging approach, time complexity O (n*log^2 (n)), and then I found myself simply a silly lack. We can learn from the Noip2016day1t2 method, using the difference in the tree to do. We record the segment tree state of Dfs when we first entered the point, and then record the segment tree status at exit, and make binary lookups based on the two segment trees. The problem then becomes a durable line tree with time complexity O (N*log (n)).

Code:

#include <iostream> #include <string> #include <cstring> #include <cmath> #include <cstdio

> #include <cstdlib> #include <stdio.h> #include <algorithm> using namespace std;
const int maxn=100100;
const int MAXL=20;

typedef long Long LL;
	struct data {int obj;
Data *next;
} E[MAXN];
Data *HEAD[MAXN];

int ecur=-1;
	struct Tnode {LL sum;
	int cnt;
Tnode *lson,*rson;
} tree[(MAXN&LT;&LT;2) +MAXN*MAXL];
Tnode *ROOT[MAXN];

int tcur=-1;
int C[MAXN];
int TEMP[MAXN];
int ID[MAXN];

int L[MAXN];
int time=0;
LL ans=0;

int n,m;
	void Add (int x,int y) {ecur++;
	E[ecur].obj=y; E[ecur].
	NEXT=HEAD[X];
Head[x]=e+ecur;

} bool Comp (int x,int y) {return c[x]<c[y];}
	Tnode *new_node () {tcur++;
	tree[tcur].sum=tree[tcur].cnt=0;
	Tree[tcur].lson=tree[tcur].rson=tree;
return tree+tcur;
		} void Update (Tnode *root,int l,int r,int X,int v) {if (l==x && x==r) {root->sum+= (long long) v;
		root->cnt++;
	Return } int mid= (L+R) >>1;
	Tnode *y=new_node ();
		if (x<=mid) {*y=* (Root->lson);
		root->lson=y;
	Update (Y,L,MID,X,V);
		} else {*y=* (Root->rson);
		root->rson=y;
	Update (Y,MID+1,R,X,V);
	} root->sum=root->lson->sum+root->rson->sum;
root->cnt=root->lson->cnt+root->rson->cnt;
		} int Query (tnode *x,tnode *y,int l,int r,ll v) {if (l==r) {LL now_sum=y->sum-x->sum;
		LL now_cnt=y->cnt-x->cnt;
		if (now_sum<=v) return now_cnt;
	else return 0;
	} LL left_sum=y->lson->sum-x->lson->sum;
	LL left_cnt=y->lson->cnt-x->lson->cnt;
	int mid= (L+R) >>1;
	if (left_sum<=v) return left_cnt+query (x->rson,y->rson,mid+1,r,v-left_sum);
else return Query (X-&GT;LSON,Y-&GT;LSON,L,MID,V);
	} void Dfs (int node) {time++;
	Root[time]=new_node ();
	*ROOT[TIME]=*ROOT[TIME-1];
	Update (Root[time],1,n,id[node],c[node]);
	int x=time-1;
	For (data *p=head[node]; p; p=p->next) Dfs (p->obj); int Num=query (root[x],root[tIme],1,n, (Long Long) m);
	LL nans= (Long Long) l[node]* (long Long) num;
Ans=max (Ans,nans);
	} int main () {freopen ("c.in", "R", stdin);
	
	Freopen ("C.out", "w", stdout);
	scanf ("%d%d", &n,&m);
	for (int i=1; i<=n; i++) head[i]=null;
		for (int i=1; i<=n; i++) {int b;
		scanf ("%d%d%d", &b,&c[i],&l[i]);
		Temp[i]=i;
	if (b) Add (b,i);
	} sort (Temp+1,temp+n+1,comp);
	
	for (int i=1; i<=n; i++) id[temp[i]]=i;
	Root[0]=new_node ();
	
	DFS (1);
	cout<<ans<<endl;
return 0; }

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.