SWERC'2016 F 树形DP+树状数组


Performance Review

Employee performance reviews are anecessary evil in any company. In a performance review, employees give writtenfeedback about each other on the work done recently. This feedback is passed upto their managers which then decide promotions based on the feedback received.

Maria is in charge of the performancereview system in the engineering division of a famous company. The divisionfollows a typical structure. Each employee (except the engineering director)reports to one manager and every employee reports directly or indirectly to thedirector.

Having the managers assessing theperformance of their direct reports has not worked very well. After thoroughresearch, Maria came up with a new performance review system. The

mainidea is to complement the existing corporate structure with a technical rankfor each employee. An employee should give feedback only about subordinateswith lower technical level.

Hence, the performance review will work asfollows. Employees prepare a summary of their work, estimate how much time ittakes to review it, and then request their superiors with higher technical rankto review their work.

Maria is veryproud of this new system, but she is unsure if it will be feasible in practice.She wonders how much time each employee will waste writing reviews. Can youhelp her out?

Task

Given thecorporate structure of the engineering division, determine how much time eachemployee will spend writing performance reviews.

Input

The rst line of input has one integer E, the numberof employees, who are conveniently numbered between 1 and E. The next E lines describe allthe employees, starting at employee 1until employee E. Each linecontains three space-separated integers mi ri ti, the manager, the technical rank and the expected time to performthe review of employee i. The engineeringdirector has no manager, represented with mi = −1. The other employeeshave mi between1 and E.

F     F

Constraints

1≤ E ≤ 100000       Number of employees

1≤ ri ≤ 100000       Technical rank ofeach employee

1 ≤ ti ≤ 100000       Expected time toperform each review

Output

The outputcontains E lines. Line i has the time employee i will spend writingreviews.

Sample Input

5

4 4 80

1 1 40

-1 10 60

3   5 50

4   8 70

Sample Output

40

0

240

120

0


题意:给你每个结点的父亲  -1代表自己就是根  每个结点的rank和sum  问每个点的儿子rank比他小的结点sum和是多少


题解:树形dp  每次搜一个新结点时记录一个当前树状数组对他的贡献  把他的儿子搜完之后用当前贡献减去前面记录的贡献就是答案


#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
#include<deque>
using namespace std;
typedef long long ll;
ll n,cnt,head[100005],ans[100005],vis[100005],C[100005];
struct node{
	ll m,r,t;
}e[100005];
struct tree{
	ll to,nex;
}edge[200005];
void add(ll u,ll v){
	edge[cnt].to=v;
	edge[cnt].nex=head[u];
	head[u]=cnt++;
}
void change(ll pos,ll x){
	while(pos<=100000){
		C[pos]+=x;
		pos+=(pos&-pos);
	}
}
ll sum(ll n){
	ll ss=0;
	while(n>0){
		ss+=C[n];
		n-=(n&-n);
	}
	return ss;
}
void dfs(ll t,ll s){
	ll i;
	for(i=head[t];~i;i=edge[i].nex){
		ll v=edge[i].to;
		if(vis[v])continue;
		vis[v]=1;
		dfs(v,sum(e[v].r-1));
	}
	ans[t]+=sum(e[t].r-1)-s;
	if(t==0)return ;
	change(e[t].r,e[t].t);
}
int main(){
	scanf("%lld",&n);
	ll i,j;
	memset(head,-1,sizeof(head));
	for(i=1;i<=n;i++){
		scanf("%lld%lld%lld",&e[i].m,&e[i].r,&e[i].t);
		if(e[i].m!=-1){
			add(e[i].m,i);
			add(i,e[i].m);
		}
		else{
			add(0,i);
			add(i,0);
		}
	}
	vis[0]=1;
	dfs(0,0);
	for(i=1;i<=n;i++)printf("%lld\n",ans[i]);
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值