Codeforces Round #169 (Div. 2) E. Little Girl and Problem on Trees(给出一棵树,对距离某个点d范围之内的所有点+x,查询某个点的值)

E. Little Girl and Problem on Trees
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard 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 ≤ nui ≠ 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 ≤ n1 ≤ x ≤ 1041 ≤ 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

题意:

一棵树有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));
        }
    }
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值