线段树Codeforces Round #169 (Div. 2)(好题)

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 ≤ 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.

Sample test(s)
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

题意:有两种操作,一种是给距离节点v长度为d的节点加上x,另一种是询问v节点的值

思路:维护两颗线段树,一颗是根节点,维护给到根节点距离为d的节点区间加上x(他们的编号是层次遍历的编号),另一颗维护链上的区间加和(他们的编号是深搜的编号)

这样对于每次询问和加权,可以分成两部分(即包含根和不包含根)

#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
using namespace std;
const int maxn=100010;
typedef long long LL;
int  n,p,id;
int dep[maxn];//深度便利编号
int wid[maxn];//层次遍历编号
int ldep[maxn];//叶子节点深度
vector<int> g[maxn];


void init()
{
    id=0;
    for(int i=0;i<=n;i++)g[i].clear();
    memset(dep,0,sizeof(dep));
    memset(wid,0,sizeof(wid));
    memset(ldep,0,sizeof(ldep));
}
//对每个节点重新编号,包括深搜和宽搜的编号
void dfs(int u,int fa)
{
    ldep[u]=wid[u];
    dep[u]=id++;
    for(int i=0;i<g[u].size();i++)
    {
        int v=g[u][i];
        if(v==fa)continue;
        wid[v]=wid[u]+1;
        dfs(v,u);
        ldep[u]=max(ldep[u],ldep[v]);
    }
}


struct IntervalTree
{
    LL sum[maxn<<3];
    void build()
    {
        memset(sum,0,sizeof(sum));
    }

    void pushdown(int o,int l,int r)
    {
        if(l>=r)return;
        sum[o<<1]+=sum[o];
        sum[o<<1|1]+=sum[o];
        sum[o]=0;
    }
    void update(int o,int l,int r,int q1,int q2,int x)
    {
        if(q1<=l&&r<=q2)
        {
            sum[o]+=x;
            return;
        }
        pushdown(o,l,r);
        int mid=(l+r)>>1;
        if(q1<=mid)update(o<<1,l,mid,q1,q2,x);
        if(q2>mid)update(o<<1|1,mid+1,r,q1,q2,x);
    }
    LL query(int o,int l,int r,int pos)
    {
        if(l==r)return sum[o];
        pushdown(o,l,r);
        int mid=(l+r)>>1;
        if(pos<=mid)return query(o<<1,l,mid,pos);
        else return query(o<<1|1,mid+1,r,pos);
    }

}D,W;

int main()
{
    while(scanf("%d%d",&n,&p)!=EOF)
    {
        for(int i=1;i<n;i++)
        {
            int u,v;
            scanf("%d%d",&u,&v);
            g[u].push_back(v);
            g[v].push_back(u);
        }
        dfs(1,0);
        int op,v,x,d;
        LL root=0;
        while(p--)
        {
            scanf("%d%d",&op,&v);
            if(op==1)
            {
                if(v==1)cout<<root<<endl;
                else cout<<D.query(1,1,n,dep[v])+W.query(1,1,n,wid[v])<<endl;
            }
            else
            {
                scanf("%d%d",&x,&d);
                if(v==1)
                {
                    root+=x;
                    W.update(1,1,n,1,d,x);
                }
                else
                {
                    int L=d,R=min(ldep[v]-wid[v],d);
                    if(L==wid[v]){L--,root+=x;}
                    if(L<wid[v])//不经过根节点的时候
                        D.update(1,1, n,dep[v]-L,dep[v]+R,x);
                    else//经过根节点需要更新两个,分别是离根节点距离为tmp的,和离询问的节点距离为dep[v]-L,dep[v]+R的
                    {
                        root+=x;
                        int tmp=L-wid[v];
                        W.update(1,1,n,1,min(tmp,n),x);
                        L=wid[v]-tmp-1;
                        if(R>=-L)D.update(1,1,n,dep[v]-L,dep[v]+R,x);
                    }
                }
            }
        }
    }
    return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值