题目链接:http://codeforces.com/problemset/problem/276/E
E. Little Girl and Problem on Trees
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
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.
Input
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 and vi. 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.
Output
For each request to print the node value print an integer — the reply to the request.
Examples
input
3 6
1 2
1 3
0 3 1 2
0 2 3 1
0 1 5 2
1 1
1 2
1 3
output
9
9
6
input
6 11
1 2
2 5
5 4
1 6
1 3
0 3 1 3
0 3 4 5
0 2 1 4
0 1 5 5
0 4 6 2
1 1
1 2
1 3
1 4
1 5
1 6
output
11
17
11
16
17
11
题意:
除了结点1,其他结点的度之多为2
有两种操作,操作0:将距离v的距离<=d的结点的值加x;
操作1:求结点v的值。
分析:
更新结点操作时,有两种情况发生…一个是在所在的线段内更新…一个是上界超过了线段…更新到其他的线段上去…考虑到一段一段更新会超时,,,建树时,除了将每个线段建成一个树,还需将结点1与其所有子节点(只需找到线段最长的一段)建一颗树。这样若更新时发生第二种情况,则分两段更新,先求出其他线段该更新的长度,利用tre[0]更新, 然后再看该点所在线段还需不需要更新。
源代码:
#include<cstdio>
#include<cstring>
#include<cmath>
#include<vector>
#include<algorithm>
using namespace std;
const int N=100005;
vector<int>vec[N];
struct Node
{
int num;//哪一棵线段树
int dis;//距离节点1的距离,也为该点在树中的编号
} node[N];
int ma;
vector<int>tre[N];
int rm[N];
void dfs(int cur,int pre,int num,int deep)
{
node[cur].num=num;
node[cur].dis=deep;
tre[num].push_back(0);
for(int i=0; i<vec[cur].size(); i++)
{
if(vec[cur][i]!=pre)
{
dfs(vec[cur][i],cur,num,deep+1);
}
}
ma=max(ma,deep);
}
void pushdown(int M,int o)
{
if(tre[M][o])
{
tre[M][o<<1]+=tre[M][o];
tre[M][o<<1|1]+=tre[M][o];
tre[M][o]=0;
}
}
void update(int M,int o,int l,int r,int x,int y,int c)
{
if(x<=l&&y>=r)
{
tre[M][o]+=c;
return;
}
pushdown(M,o);
int mid=(l+r)/2;
if(x<=mid)
update(M,o<<1,l,mid,x,y,c);
if(y>mid)
update(M,o<<1|1,mid+1,r,x,y,c);
}
int query(int M,int o,int l,int r,int p)
{
if (l==r) return tre[M][o];
pushdown(M,o);
int mid=(l+r)/2;
if (p<=mid) return query(M,o<<1,l,mid,p);
if (p>mid) return query(M,o<<1|1,mid+1,r,p);
}
int main()
{
int n,q;
while(~scanf("%d%d",&n,&q))
{
int u,v;
for(int i=0; i<N; i++)
{
vec[i].clear();
tre[i].clear();
}
for(int i=1; i<n; i++)
{
scanf("%d%d",&u,&v);
vec[u].push_back(v);
vec[v].push_back(u);
}
ma=0;
int cnt=0;
node[1].dis=0;
for(int i=0; i<vec[1].size(); i++)
{
dfs(vec[1][i],1,++cnt,1);
}
int x=(ma<<2);
while(x--)
tre[0].push_back(0);
for(int i=1; i<=cnt; i++)
{
rm[i]=tre[i].size();
x=(rm[i]<<2);
while(x--)
tre[i].push_back(0);
}
while(q--)
{
int op,v;
scanf("%d%d",&op,&v);
if(op==0)
{
int x,d;
scanf("%d%d",&x,&d);
int l;
if(node[v].dis<=d)
{
l=d-node[v].dis+1;
update(0,1,0,ma,0,min(ma,l-1),x);
}
else l=node[v].dis-d;
if(v==1)
continue;
int r=min(rm[node[v].num],node[v].dis+d);
if(l>r)
continue;
update(node[v].num,1,1,rm[node[v].num],l,r,x);
}
else
{
int ans=0;
ans+=query(0,1,0,ma,node[v].dis);
if(v!=1)
{
ans+=query(node[v].num,1,1,rm[node[v].num],node[v].dis);
}
printf("%d\n",ans);
}
}
}
return 0;
}