Performance Review
Employee performance reviews are a necessary evil in any company. In a performance review, employees give written feedback about each other on the work done recently. This feedback is passed up to their managers which then decide promotions based on the feedback received. Maria is in charge of the performance review system in the engineering division of a famous company. The division follows a typical structure. Each employee (except the engineering director) reports to one manager and every employee reports directly or indirectly to the director. Having the managers assessing the performance of their direct reports has not worked very well. After thorough research, Maria came up with a new performance review system. The main idea is to complement the existing corporate structure with a technical rank for each employee. An employee should give feedback only about subordinates with lower technical level. Hence, the performance review will work as follows. Employees prepare a summary of their work, estimate how much time it takes to review it, and then request their superiors with higher technical rank to review their work. Maria is very proud 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 you help her out?
Task
Given the corporate structure of the engineering division, determine how much time each employee will spend writing performance reviews.
Input
The rst line of input has one integer E, the number of employees, who are conveniently numbered between 1 and E. The next E lines describe all the employees, starting at employee 1 until employee E. Each line contains three space-separated integers mi ri ti, the manager, the technical rank and the expected time to perform the review of employee i. The engineering director has no manager, represented with mi = −1. The other employees have mi between 1 and E.
SWERC'2016 Universidade do Porto 13
Problem F Problem F
Constraints 1 ≤ E ≤ 100000 Number of employees 1 ≤ ri ≤ 100000 Technical rank of each employee 1 ≤ ti ≤ 100000 Expected time to perform each review
Output
The output contains E lines. Line i has the time employee i will spend writing reviews.
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
题意:给你一棵树 每个结点有r,t值 现在求所有结点i的子树中的r值小于i点的r值的所有结点的t值之和
题解:dfs序列+树状数组 结点按照r值排序 r值相同的深度小的排前面。依次处理结点 ,将子树的操作转换到区间上。树状数组中存的是结点序号(dfs遍历序号in[root])
1 #include<bits/stdc++.h> 2 using namespace std; 3 #define ll __int64 4 #pragma comment(linker, "/STACK:102400000,102400000") 5 ll n; 6 ll a,b,c; 7 ll pre[100005]; 8 ll in[100005]; 9 ll out[100005]; 10 ll nedge=0; 11 ll dfn=0; 12 ll dep[100005]; 13 map<ll,ll>mp; 14 struct node 15 { 16 ll pre; 17 ll to; 18 }N[4*100005]; 19 struct dian 20 { 21 ll w; 22 ll you; 23 ll id; 24 }M[100005],MM[100005]; 25 ll tree[100005]; 26 bool cmp(struct dian aaa,struct dian bbb) 27 { 28 if(aaa.you<bbb.you) 29 return true; 30 else 31 { 32 if(aaa.you==bbb.you) 33 return dep[in[aaa.id]]<dep[in[bbb.id]]; 34 } 35 return false; 36 } 37 void add(ll from,ll to) 38 { 39 nedge++; 40 N[nedge].to=to; 41 N[nedge].pre=pre[from]; 42 pre[from]=nedge; 43 } 44 void getdfs(ll root,int de) 45 { 46 in[root]=++dfn; 47 MM[dfn]=M[root]; 48 dep[dfn]=de; 49 mp[root]=1; 50 for(ll i=pre[root];i;i=N[i].pre){ 51 if(mp[N[i].to]) 52 continue; 53 getdfs(N[i].to,de+1); 54 } 55 out[root]=dfn; 56 } 57 ll lowbit(ll xx) 58 { 59 return xx&(-xx); 60 } 61 void add2 (ll x,ll y) 62 { 63 for(ll i=x;i<=n;i+=lowbit(i)) 64 tree[i]+=y; 65 } 66 ll getsum (ll x) 67 { 68 ll ans=0; 69 for(ll i=x;i>=1;i-=lowbit(i)) 70 ans+=tree[i]; 71 return ans; 72 } 73 int main() 74 { 75 scanf("%I64d",&n); 76 memset(tree,0,sizeof(tree)); 77 memset(pre,0,sizeof(pre)); 78 nedge=0; 79 ll ro=0; 80 for(ll i=1;i<=n;i++) 81 { 82 scanf("%I64d %I64d %I64d",&a,&b,&c); 83 M[i].w=c; 84 M[i].you=b; 85 M[i].id=i; 86 if(a==-1){ 87 ro=i; 88 continue; 89 } 90 add(a,i); 91 add(i,a); 92 } 93 getdfs(ro,1); 94 ll ans[100005]; 95 sort(MM+1,MM+1+n,cmp); 96 for(ll i=1;i<=n;i++) 97 { 98 ll l,r; 99 l=in[MM[i].id]; 100 r=out[MM[i].id]; 101 ans[MM[i].id]=getsum(r)-getsum(l); 102 add2(l,MM[i].w); 103 } 104 for(ll i=1;i<=n;i++) 105 printf("%I64d ",ans[i]); 106 return 0; 107 }