Swun 1440-what is your salary

Source: Internet
Author: User

 

What is your salary?Time limit (Common/Java): 2000 MS/6000 MS running memory limit: 65536 Kbyte
Total submission: 9 pass the test: 3

Description

 

After graduating from the ACM team of Minda, they joined together to start an IT company. During the business period, the company's profit also occasionally rises and falls, and everyone has to lament the hardships of entrepreneurship.

Of course, as an acmer, the company's salary adjustment is very rigorous, according to the contribution of the team or individual, strict approval.

There are n employees in the company. Each employee is numbered by di (1 <= I <= N), and the company has a strict team classification system. Each subordinate has only one direct supervisor, and each superior may have multiple subordinates. There is only one president in the company, and the President is D1, that is, I = 1. All employees except the President have their own superiors.

Now, the company asks you to write a salary management system.

It requires the following features:

1. query the current DI salary.

2. a salary increase of RMB X for di (-10000000 <= x <= 10000000 ).

3. Raise the salary of each member of Di's team by RMB X. (Di's team said: Di and its affiliated groups)

Note: Each person's initial salary is 0.

 

Input

Multiple groups of test data.

The first row of each group of test data has an integer N, which indicates the number of employees. (0 <n <= 100000 ).

In the next n-1 line, enter two integers I and j (0 <I, j <= N) in each row, indicating that Di's supervisor is DJ.

Enter an integer m to indicate the number of operations. (0 <m <= 100000 ).

In the next m rows, each row represents an operation:

1. Enter query I to query the di salary.

2. Enter add I x to raise the salary of di by RMB X.

3. input all I x, indicating that the team lead by Di will be given a salary increase of x RMB for each person.

 

Output

For each query in the operation, output the corresponding answer.

Sample Input

4
2 1
3 2
4 2
4
All 1 3
Query 3
Add 3 3
Query 3

Sample output

3
6

 

Question address:

Http: // 218.194.91.48/acmhome/problemdetail. do? & Method = showdetail & id = 1440

 

 

The method for this question is DFS +
Tree array (or DFS + line segment tree ))...

 

Then, the DFS traversal process mainly aims to [tree intervals]. You can take a look at it:

 

 

After being converted to a range, it is:

 

Does it look like the tree on the top is bent to the left?

 

In fact, during DFS traversal, each node is accessed in the order of 1, 2, 5, 6, 7, 9, 10, 8, 11, 4, and 3.

 

Therefore, we only need to record the [start point location] Start of each node and the [farthest point location] end that can be extended. Then, this node and all its subnodes, that is, [start, end].
This interval.

 

For example, if the interval of Node 1 is [], the interval of Node 2 is [], the interval of node 9 is [], and the interval of section 11 is [].
.

 

 

After the tree is converted into a range, the rest is the Interval Update and single point query (you can refer to the topic tree array mining-1002 ).

 

Therefore, it is easy to solve the problem by using a very basic tree array or line segment tree.

 

#include<iostream>#include<cstdio>#include<algorithm>#include<cstring>using namespace std;int n,m;struct Edge{int to,next;}eg[100010];int Ecnt,Ef[100010],top;void add_Edge(){int a,b;scanf("%d%d",&a,&b);eg[Ecnt].to=a;eg[Ecnt].next=Ef[b];Ef[b]=Ecnt++;}int newID,Start[100010],End[100010];void dfs(int v){Start[v]=++newID;for(int k=Ef[v];k!=-1;k=eg[k].next){dfs(eg[k].to);}End[v]=newID;}__int64 tree[100010];int lowbit(int x){return x&-x;}__int64 query(int v){__int64 sum=0;while(v<=n){sum+=tree[v];v+=lowbit(v);}return sum;}void update(int v,int val){while(v>0){tree[v]+=val;v-=lowbit(v);}}void _init(){int i;Ecnt=0;newID=0;for(i=1;i<=n;i++){Ef[i]=-1;tree[i]=0;Start[i]=-1;}}int main(){int i,x;char op[10];while(~scanf("%d",&n)){_init();for(i=1;i<n;i++)  add_Edge();dfs(1);scanf("%d",&m);while(m--){scanf("%s",op);if(op[0]=='Q'){scanf("%d",&i);printf("%I64d\n",query(Start[i]));}else if(op[1]=='d'){scanf("%d%d",&i,&x);update(Start[i],x);update(Start[i]-1,-x);}else{scanf("%d%d",&i,&x);update(End[i],x);update(Start[i]-1,-x);}}}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.