A little girl loves problems on trees very much. Here's one of them.
A tree is an undirected connected graph, not containing cycles. The degree of node x in the tree is the number of nodes y of the tree, such that each of them is connected with node x by some edge of the tree.
Let's consider a tree that consists of n nodes. We'll consider the tree's nodes indexed from 1 to n. The cosidered tree has the following property: each node except for node number 1 has the degree of at most 2.
Initially, each node of the tree contains number 0. Your task is to quickly process the requests of two types:
- Request of form: 0 v x d. In reply to the request you should add x to all numbers that are written in the nodes that are located at the distance of at most d from node v. The distance between two nodes is the number of edges on the shortest path between them.
- Request of form: 1 v. In reply to the request you should print the current number that is written in node v.
The first line contains integers n (2 ≤ n ≤ 105) and q (1 ≤ q ≤ 105) — the number of tree nodes and the number of requests, correspondingly.
Each of the next n - 1 lines contains two integers ui and vi (1 ≤ ui, vi ≤ n, ui ≠ vi), that show that there is an edge between nodes ui andvi. Each edge's description occurs in the input exactly once. It is guaranteed that the given graph is a tree that has the property that is described in the statement.
Next q lines describe the requests.
- The request to add has the following format: 0 v x d (1 ≤ v ≤ n, 1 ≤ x ≤ 104, 1 ≤ d < n).
- The request to print the node value has the following format: 1 v (1 ≤ v ≤ n).
The numbers in the lines are separated by single spaces.
For each request to print the node value print an integer — the reply to the request.
3 6
1 2
1 3
0 3 1 2
0 2 3 1
0 1 5 2
1 1
1 2
1 3
9
9
6
题意:
一棵树有n个节点,但是除了根节点1外,其他节点都的出度和入度加起来就只有2(就是棵树是由几条链的第一个节点粘在一起的),现在有两种操作(1)"0 v x d",在距离v节点距离d以内的所有节点的权值都加上1,(2)"1 v",查询节点v上的权值。
思路:
我们可以对根建树和根的每个子节点都动态建一个树状数组,当要更新一个点距离+d和-d之间所有点时,先判断是否会经过根,分两种情况判断一下便可以了。
#include<bits/stdc++.h>
using namespace std;
const int maxn=100100;
vector<vector<int> >tree;
int dep[maxn],Belong[maxn],maxv=0;
vector<int>G[maxn];
void dfs(int u,int pre,int fa){
Belong[u]=fa,dep[u]=dep[pre]+1;
tree[fa].push_back(0);
for(int i=0;i<G[u].size();i++){
int v=G[u][i];
if(v==pre)
continue;
dfs(v,u,fa);
}
}
void update(int st,int ed,int x,int id){
for(int i=st;i<tree[id].size();i+=(i&-i))
tree[id][i]+=x;
for(int i=ed+1;i<tree[id].size();i+=(i&-i))
tree[id][i]-=x;
}
int query(int x,int id){
int ret=0;
while(x>0){
ret+=tree[id][x];
x-=(x&-x);
}
return ret;
}
int main(){
int n,m,u,v,x,d,op;
scanf("%d%d",&n,&m);
for(int i=1;i<n;i++){
scanf("%d%d",&u,&v);
G[u].push_back(v);
G[v].push_back(u);
}
dep[1]=1;
int num=G[1].size();
tree.resize(num+5);
for(int i=0;i<G[1].size();i++){
u=G[1][i];
dfs(u,1,i+1);
tree[i+1].push_back(0);
}
tree[0].resize(maxn+5,0);
for(int i=1;i<=m;i++){
scanf("%d",&op);
if(op==0){
scanf("%d%d%d",&v,&x,&d);
if(v==1)
update(1,d+1,x,0);
else{
if(d<dep[v]-1){
int id=Belong[v];
update(dep[v]-d-1,dep[v]+d-1,x,id);
}
else{
int id=Belong[v];
update(1,d-dep[v]+2,x,0);
update(dep[v]-1,dep[v]+d-1,x,id);
if(d-dep[v]+2==dep[v]-1)
continue;
if(d-dep[v]+2>dep[v]-1)
update(dep[v]-1,d-dep[v]+1,-x,id);
else
update(d-dep[v]+2,dep[v]-2,x,id);
}
}
}
else{
scanf("%d",&v);
if(v==1){
printf("%d\n",query(dep[v],0));
continue;
}
int id=Belong[v];
printf("%d\n",query(dep[v]-1,id)+query(dep[v],0));
}
}
}