题意:给出一棵根节点为1的树,执行m次修改操作,每次修改为a,b,c,表示a节点的子树中,距离a小于等于b的子节点的权值加上c,求m次操作后每个节点的权值

分析:用线段树维护每层节点的权值,然后dfs遍历这颗树,当前节点有操作时,把当前节点的深度到被修改的最大深度都加上c(实际上只有当前节点的子节点才加c),而回朔的时候再将这个区间减c,这样就避免了对非子节点的影响

AC代码(线段树的区间更新):

#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn=3e5+10;
ll f[maxn],nex[maxn*2],to[maxn*2],cnt,level[maxn],n,m;
ll ans[maxn],num[maxn*4],lazy[maxn*4];
vector<pair<ll,ll>>ve[maxn];
void add(int a,int b)
{
cnt++;
to[cnt]=b;
nex[cnt]=f[a];
f[a]=cnt;
}
void updata(int st,int en,int l,int r,int rt,ll x)
{
//cout<<l<<" "<<r<<endl;
if(st<=l&&en>=r)
{
num[rt]+=x*(r-l+1);
lazy[rt]+=x;
return ;
}
int mid=(l+r)/2;
if(lazy[rt])
{
lazy[rt*2]+=lazy[rt];
lazy[rt*2+1]+=lazy[rt];
num[rt*2]+=lazy[rt]*(mid-l+1);
num[rt*2+1]+=lazy[rt]*(r-mid);
lazy[rt]=0;
}
if(st<=mid)updata(st,en,l,mid,rt*2,x);
if(en>=mid+1)updata(st,en,mid+1,r,rt*2+1,x);
num[rt]=num[rt*2]+num[rt*2+1];
}
ll quer(int x,int l,int r,int rt)
{
int mid=(l+r)/2;
if(l==r)return num[rt];
if(lazy[rt])
{
lazy[rt*2]+=lazy[rt];
lazy[rt*2+1]+=lazy[rt];
num[rt*2]+=lazy[rt]*(mid-l+1);
num[rt*2+1]+=lazy[rt]*(r-mid);
lazy[rt]=0;
}
if(x<=(l+r)/2)return quer(x,l,(l+r)/2,rt*2);
else return quer(x,(l+r)/2+1,r,rt*2+1);
num[rt]=num[rt*2]+num[rt*2+1];
}
void getlevel(int x,int l)
{
level[x]=l;
for(int i=f[x]; i; i=nex[i])
{
int v=to[i];
if(!level[v])getlevel(v,l+1);
}
}
void dfs(int x,int pre)
{
for(int i=0; i<ve[x].size(); i++)
{
updata(level[x],level[x]+ve[x][i].first,1,n,1,ve[x][i].second);
}
ans[x]=quer(level[x],1,n,1);
for(int i=f[x]; i; i=nex[i])
{
int v=to[i];
if(v!=pre)
dfs(v,x);
}
for(int i=0; i<ve[x].size(); i++)
{
updata(level[x],level[x]+ve[x][i].first,1,n,1,-ve[x][i].second);
}
}
int main()
{
ios::sync_with_stdio(false); cin>>n;
for(int i=1; i<=n-1; i++)
{
int a,b;
cin>>a>>b;
add(a,b);
add(b,a);
}
getlevel(1,1);
cin>>m;
for(int i=1; i<=m; i++)
{
ll a,b,c;
cin>>a>>b>>c;
ve[a].push_back(make_pair(b,c));
} dfs(1,-1);
for(int i=1; i<=n; i++)
cout<<ans[i]<<" ";
cout<<endl;
return 0;
}

  

AC代码(树状数组的区间更新):

#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn=3e5+10;
ll f[maxn],nex[maxn*2],to[maxn*2],cnt,n,m;
ll ans[maxn],c1[maxn],c2[maxn];
vector<pair<int,ll>>ve[maxn];
void add1(int a,int b)
{
cnt++;
to[cnt]=b;
nex[cnt]=f[a];
f[a]=cnt;
}
void add(int x,ll y)
{
for(int i=x; i<=n; i+=(i&-i))c1[i]+=y,c2[i]+=y*x;
}
ll quer(int x)
{
ll res=0;
for(int i=x; i>0; i-=(i&-i))res+=((x+1)*c1[i]-c2[i]);
return res;
}
void in_add(int l,int r,ll x)
{
add(l,x);
add(r+1,-x);
}
void dfs(int x,int pre,int l)
{
for(int i=0; i<ve[x].size(); i++)
in_add(l,l+ve[x][i].first,ve[x][i].second);
ans[x]=quer(l)-quer(l-1);
for(int i=f[x]; i; i=nex[i])
{
int v=to[i];
if(v!=pre)
dfs(v,x,l+1);
}
for(int i=0; i<ve[x].size(); i++)
in_add(l,l+ve[x][i].first,-ve[x][i].second);
}
int main()
{
ios::sync_with_stdio(false);
cin>>n;
for(int i=1; i<=n-1; i++)
{
int a,b;
cin>>a>>b;
add1(a,b);
add1(b,a);
}
cin>>m;
for(int i=1; i<=m; i++)
{
ll a,b,c;
cin>>a>>b>>c;
ve[a].push_back(make_pair(b,c));
}
dfs(1,-1,1);
for(int i=1; i<=n; i++)
cout<<ans[i]<<" ";
cout<<endl;
return 0;
}

  

1076E - Vasya and a Tree(图的遍历)的更多相关文章

  1. Codeforces 1076E Vasya and a Tree(树状数组)或dfs

    题意:给你一颗以1为根节点的树,初始所有节点的权值为0,然后有m个操作,每个操作将点x的所有距离不超过d的节点权值+1,问经过m次操作后每个节点权值是多少? 思路:如果是一个序列,就可以直接用树状数组 ...

  2. CF 1076E Vasya and a Tree(线段树+树剖)

    传送门 解题思路 首先按照每个修改时\(x\)的深度\(+d\)从大到小排序,然后按照深度分层,一层一层的修改,修改的时候就直接暴力修改子树,然后每做完一层把答案都取下来,因为以后的所有修改的深度都小 ...

  3. codeforces 1076E Vasya and a Tree 【dfs+树状数组】

    题目:戳这里 题意:给定有n个点的一棵树,顶点1为根.m次操作,每次都把以v为根,深度dep以内的子树中所有的顶点(包括v本身)加x.求出最后每个点的值为多少. 解题思路:考虑到每次都只对点及其子树操 ...

  4. Vasya and a Tree CodeForces - 1076E(线段树+dfs)

    I - Vasya and a Tree CodeForces - 1076E 其实参考完别人的思路,写完程序交上去,还是没理解啥意思..昨晚再仔细想了想.终于弄明白了(有可能不对 题意是有一棵树n个 ...

  5. Vasya and a Tree CodeForces - 1076E (线段树 + dfs)

    题面 Vasya has a tree consisting of n vertices with root in vertex 1. At first all vertices has 0 writ ...

  6. CodeForces-1076E Vasya and a Tree

    CodeForces - 1076E Problem Description: Vasya has a tree consisting of n vertices with root in verte ...

  7. 图的遍历(搜索)算法(深度优先算法DFS和广度优先算法BFS)

    图的遍历的定义: 从图的某个顶点出发访问遍图中所有顶点,且每个顶点仅被访问一次.(连通图与非连通图) 深度优先遍历(DFS): 1.访问指定的起始顶点: 2.若当前访问的顶点的邻接顶点有未被访问的,则 ...

  8. C++编程练习(9)----“图的存储结构以及图的遍历“(邻接矩阵、深度优先遍历、广度优先遍历)

    图的存储结构 1)邻接矩阵 用两个数组来表示图,一个一维数组存储图中顶点信息,一个二维数组(邻接矩阵)存储图中边或弧的信息. 2)邻接表 3)十字链表 4)邻接多重表 5)边集数组 本文只用代码实现用 ...

  9. Kruskal和prime算法的类实现,图的遍历BFS算法。

    一.图的遍历 #include<iostream> #include<queue> #include<vector> using namespace std; in ...

随机推荐

  1. [20181108]with temp as 建立临时表吗.txt

    [20181108]with temp as 建立临时表吗.txt --//链接:http://www.itpub.net/thread-2106304-1-1.html--//作者提到在dg上使用w ...

  2. python爬虫之天气预报网站--查看最近(15天)的天气信息(正则表达式)

    python爬虫之天气预报网站--查看最近(15天)的天气信息(正则表达式) 思路: 1.首先找到一个自己想要查看天气预报的网站,选择自己想查看的地方,查看天气(例:http://www.tianqi ...

  3. 测试TCP 和 UDP 端口的方法

    测试 TCP 端口: telnel IP PORT nc -vz IP PORT 测试 UDP 端口: nc -vuz IP PORT 其中 -u 表示使用 udp 协议来进行测试. -u, --ud ...

  4. Linux 忘记登录密码?破解系统登陆密码

    1.重启或者开启系统,在如下界面按e 进入救援系统: 2.在linux16 这一行末尾输入:rd.break,以rd.break 的方法重置密码 3.分别执行以下命令 mount -o remount ...

  5. LeetCode算法题-First Unique Character in a String(Java实现)

    这是悦乐书的第213次更新,第226篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第81题(顺位题号是387).给定一个字符串,找到它中的第一个非重复字符并返回它的索引. ...

  6. Java 7 for Absolute Beginners/Java 7基础教程--读后感

    1. 很适合部分初学者,因为书中对于JAVA许多特性都有涉猎,而且也是浅尝即止.如果读者没有受过专业的编程训练,那么在阅读此书时不会畏惧.因为书中没有充斥着大量的计算机理论知识,虽然作者依然覆盖了JA ...

  7. JSONCkecker(Java语言版本)

    // MIT License // // Copyright (c) 2016 Michel Kraemer // Copyright (c) 2005 JSON.org // // Permissi ...

  8. bootstrap的datepicker在选择日期后调用某个方法

    bootstrap的datepicker在选择日期后调用某个方法 2016-11-08 15:14 1311人阅读 评论(0) 收藏 举报 首先感谢网易LOFTER博主Ivy的博客,我才顿悟了问题所在 ...

  9. 11.redis_python

    # pip install redis import redis # 1.链接数据库 key--value client = redis.StrictRedis(host='127.0.0.1', p ...

  10. SQLite也可能出现死锁

    提到锁就不得不说到死锁的问题,而SQLite也可能出现死锁.下面举个例子:连接1:BEGIN (UNLOCKED)连接1:SELECT ... (SHARED)连接1:INSERT ... (RESE ...