1076E - Vasya and a Tree(图的遍历)
题意:给出一棵根节点为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(图的遍历)的更多相关文章
- Codeforces 1076E Vasya and a Tree(树状数组)或dfs
题意:给你一颗以1为根节点的树,初始所有节点的权值为0,然后有m个操作,每个操作将点x的所有距离不超过d的节点权值+1,问经过m次操作后每个节点权值是多少? 思路:如果是一个序列,就可以直接用树状数组 ...
- CF 1076E Vasya and a Tree(线段树+树剖)
传送门 解题思路 首先按照每个修改时\(x\)的深度\(+d\)从大到小排序,然后按照深度分层,一层一层的修改,修改的时候就直接暴力修改子树,然后每做完一层把答案都取下来,因为以后的所有修改的深度都小 ...
- codeforces 1076E Vasya and a Tree 【dfs+树状数组】
题目:戳这里 题意:给定有n个点的一棵树,顶点1为根.m次操作,每次都把以v为根,深度dep以内的子树中所有的顶点(包括v本身)加x.求出最后每个点的值为多少. 解题思路:考虑到每次都只对点及其子树操 ...
- Vasya and a Tree CodeForces - 1076E(线段树+dfs)
I - Vasya and a Tree CodeForces - 1076E 其实参考完别人的思路,写完程序交上去,还是没理解啥意思..昨晚再仔细想了想.终于弄明白了(有可能不对 题意是有一棵树n个 ...
- 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 ...
- CodeForces-1076E Vasya and a Tree
CodeForces - 1076E Problem Description: Vasya has a tree consisting of n vertices with root in verte ...
- 图的遍历(搜索)算法(深度优先算法DFS和广度优先算法BFS)
图的遍历的定义: 从图的某个顶点出发访问遍图中所有顶点,且每个顶点仅被访问一次.(连通图与非连通图) 深度优先遍历(DFS): 1.访问指定的起始顶点: 2.若当前访问的顶点的邻接顶点有未被访问的,则 ...
- C++编程练习(9)----“图的存储结构以及图的遍历“(邻接矩阵、深度优先遍历、广度优先遍历)
图的存储结构 1)邻接矩阵 用两个数组来表示图,一个一维数组存储图中顶点信息,一个二维数组(邻接矩阵)存储图中边或弧的信息. 2)邻接表 3)十字链表 4)邻接多重表 5)边集数组 本文只用代码实现用 ...
- Kruskal和prime算法的类实现,图的遍历BFS算法。
一.图的遍历 #include<iostream> #include<queue> #include<vector> using namespace std; in ...
随机推荐
- adb 的常见问题与处理办法两三
问题1:无法安装手机驱动, 解决方法:安装强大的豌豆荚,通常能都能解决问题 问题2: adb devices 时出现 adb devicesadb server is out of date. ki ...
- Java程序导出成.jar文件、生成.exe可执行文件及打包成可执行安装程序(可在无Java环境的计算机上运行)--以个人所得税计算器为例
Java程序导出成.jar文件.生成.exe可执行文件及打包成可执行安装程序 作者:凯鲁嘎吉 - 博客园 http://www.cnblogs.com/kailugaji/ 需要准备的软件: jdk, ...
- Nmap参考指南(Man Page)
Table of Contents 描述 译注 选项概要 目标说明 主机发现 端口扫描基础 端口扫描技术 端口说明和扫描顺序 服务和版本探测 操作系统探测 时间和性能 防火墙/IDS躲避和哄骗 输出 ...
- 部署tinyproxy代理服务
#安装依赖 yum install asciidoc #下载 wget https://github.com/tinyproxy/tinyproxy/releases/download/1.8.4/t ...
- linux学习笔记整理(一)
第二章 Linux基本操作 2.1 Linux网络相关概念和修改IP地址的方法2.2 关闭防火墙并设置开机开不启动2.3 临时和永久关闭Selinux2.4 设置系统光盘开机自动挂载2.5 配置本地 ...
- linux下安装jdk_mysql_tomcat_redis
目前搬我以前的笔记,每个人做笔记方式都不一样,看别人的风格,生成自己的风格 1.linux安装软件和redis学习 jdk --- java开发运行环境 Tomcat - WEB程序的服务器 Mysq ...
- WPF 样式(定义样式、引用样式、样式作用域、Trigger触发器)
1.定义 资源字典 <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presenta ...
- 【转】FFmpeg获取DirectShow设备数据(摄像头,录屏)
这两天研究了FFmpeg获取DirectShow设备数据的方法,在此简单记录一下以作备忘.本文所述的方法主要是对应Windows平台的. 1. 列设备 ffmpeg -list_devic ...
- 【vue】vue +element 搭建项目,vue-cli 如何打包上线
以自己的项目为例 第一步:手动修改config文件夹中的index.js文件中的build对象,将 assetsPublicPath 中的 “/” ,改为 “你实际的加载路径” 如图: 第二步:执行( ...
- 使用TTS实现Oracle跨版本迁移
TTS实现数据库迁移,具有速度快.支持跨平台和跨版本等优点.本文记录了用TTS从10g single迁移到11g RAC的过程. Source数据库版本和字符集设置: SQL> select * ...