题意:给出一棵根节点为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. 盒子模型、网页自动居中、float浮动与清除、横向两列布局

    1.CSS包含:标准文档流,浮动,绝对定位 标准文档流的特点:从上到下,从左到右,输出文档内容 盒子模型:块级元素(div ui li dl dt p)与行级元素(span strong img in ...

  2. MSSQL coalesce系统函数简介

    转自:http://www.maomao365.com/?p=4390 一.coalesce函数简介 coalesce 系统函数,比ISNULL更强大,更方便的系统函数,coalesce可以接收多个参 ...

  3. JavaWeb入门笔记

    Java web笔记 一.HTTP协议 HTTP(超文本传输协议),它是一种主流B/S架构中应用的通信协议.具有以下特点: 1.无状态 服务端不会记录客户端每次提交的请求,服务器一旦相应客户端之后,就 ...

  4. Windows 的命令行安装Scoop程序管理工具

    传送门: # 官网 http://scoop.sh/ # github https://github.com/lukesampson/scoop window中快速安装: 必须使用powershell ...

  5. Unity Remote 无法连接

    前言 Unity Remote支持把手机的以下数据返回到Unity Editor中: 触摸输入 加速计 陀螺仪 摄像头 GPS 我的操作环境: Unity 5.3.6f1 在windows 下 And ...

  6. 我的BRF+自学教程(二):跟踪模式(trace mode)

    使用自开发程序来处理业务逻辑时,处理过程通常是个黑箱,业务顾问和业务用户不知道程序的具体运行方式,要依赖文档和频繁的沟通来确认实际情况. BRFplus可以通过配置的方式实现业务逻辑,使得业务人员把业 ...

  7. Nginx的configure各项中文说明

    –prefix=<path> – Nginx安装路径.如果没有指定,默认为 /usr/local/nginx. –sbin-path=<path> – Nginx可执行文件安装 ...

  8. Pandas 处理丢失数据

    处理丢失数据 import pandas as pd from pandas import Series, DataFrame import numpy as np 有两种丢失数据: 1. None ...

  9. C#反射の反射泛型

    C#反射の反射详解(点击跳转)C#反射の反射接口(点击跳转)C#反射反射泛型接口(点击跳转)C#反射の一个泛型反射实现的网络请求框架(点击跳转) 接上篇. 自定义一个泛型类(继承于接口) public ...

  10. centos7下安装docker(17.1docker监控---sysdig)

    sysdig是一个轻量级的系统监控工具,同时原生支持容器.通过sysdig我们可以近距离观察linux操作系统和容器的行为 Linux上有很多常用的监控工具,比如;strace,tcpdump,hto ...