[CF1076E]Vasya and a Tree
题目大意:给定一棵以$1$为根的树,$m$次操作,第$i$次为对以$v_i$为根的深度小于等于$d_i$的子树的所有节点权值加$x_i$。最后输出每个节点的值
题解:可以把操作离线,每次开始遍历到一个节点,把以它为根的操作加上,结束时把这个点的操作删去。
因为是$dfs$,所以一个点对同一深度的贡献是一定的,可以用树状数组区间加减,求前缀和。但是因为是$dfs$,完全可以把前缀和传入,就不需要树状数组了。
卡点:无
C++ Code:
#include <cstdio>
#include <vector>
#include <cctype>
namespace __IO {
namespace R {
int x, ch;
inline int read() {
ch = getchar();
while (isspace(ch)) ch = getchar();
for (x = ch & 15, ch = getchar(); isdigit(ch); ch = getchar()) x = x * 10 + (ch & 15);
return x;
}
}
}
using __IO::R::read; #define maxn 300010 int head[maxn], cnt;
struct Edge {
int to, nxt;
} e[maxn << 1];
inline void add(int a, int b) {
e[++cnt] = (Edge) {b, head[a]}; head[a] = cnt;
} int n, m;
struct Modify {
int d, x;
inline Modify() {}
inline Modify(int __d, int __x) :d(__d), x(__x){}
};
std::vector<Modify> S[maxn]; long long pre[maxn], ans[maxn];
void dfs(int u, int fa = 0, int dep = 0, long long sum = 0) {
for (std::vector<Modify>::iterator it = S[u].begin(); it != S[u].end(); it++) {
pre[dep] += it -> x;
if (dep + it -> d + 1 <= n) pre[dep + it -> d + 1] -= it -> x;
}
sum += pre[dep];
ans[u] = sum;
for (int i = head[u]; i; i = e[i].nxt) {
int v = e[i].to;
if (v != fa) dfs(v, u, dep + 1, sum);
}
for (std::vector<Modify>::iterator it = S[u].begin(); it != S[u].end(); it++) {
pre[dep] -= it -> x;
if (dep + it -> d + 1 <= n) pre[dep + it -> d + 1] += it -> x;
}
}
int main() {
n = read();
for (int i = 1, a, b; i < n; i++) {
a = read(), b = read();
add(a, b);
add(b, a);
}
m = read();
for (int i = 1, v, d, x; i <= m; i++) {
v = read(), d = read(), x = read();
S[v].push_back(Modify(d, x));
}
dfs(1);
for (int i = 1; i <= n; i++) {
printf("%lld", ans[i]);
putchar(i == n ? '\n' : ' ');
}
return 0;
}
[CF1076E]Vasya and a Tree的更多相关文章
- cf1076E Vasya and a Tree (线段树)
我的做法: 给询问按$deep[v]+d$排序,每次做到某一深度的时候,先给这个深度所有点的值清0,然后直接改v的子树 官方做法比较妙妙: dfs,进入v的时候给$[deep[v],deep[v]+d ...
- CF1076E:Vasya and a Tree(DFS&差分)
Vasya has a tree consisting of n n vertices with root in vertex 1 1 . At first all vertices has 0 0 ...
- Vasya and a Tree CodeForces - 1076E(线段树+dfs)
I - Vasya and a Tree CodeForces - 1076E 其实参考完别人的思路,写完程序交上去,还是没理解啥意思..昨晚再仔细想了想.终于弄明白了(有可能不对 题意是有一棵树n个 ...
- Codeforces1076E. Vasya and a Tree(dfs+离线+动态维护前缀和)
题目链接:传送门 题目: E. Vasya and a Tree time limit per test seconds memory limit per test megabytes input s ...
- Codeforces 1076 E - Vasya and a Tree
E - Vasya and a Tree 思路: dfs动态维护关于深度树状数组 返回时将当前节点的所有操作删除就能保证每次访问这个节点时只进行过根节点到当前节点这条路径上的操作 代码: #pragm ...
- CF Edu54 E. Vasya and a Tree DFS+树状数组
Vasya and a Tree 题意: 给定一棵树,对树有3e5的操作,每次操作为,把树上某个节点的不超过d的子节点都加上值x; 思路: 多开一个vector记录每个点上的操作.dfs这颗树,同时以 ...
- 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 ...
- CF1076E:Vasya and a Tree
浅谈树状数组与线段树:https://www.cnblogs.com/AKMer/p/9946944.html 题目传送门:https://codeforces.com/problemset/prob ...
随机推荐
- PHP实现SMTP邮件的发送实例
当你还在纠结php内置的mail()函数不能发送邮件时,那么你现在很幸运,此时的这篇文章可以帮助到你! php利用smtp类来发邮件真是屡试不爽,我用过很久了,基本上没出过问题.本博客后台,当博主回复 ...
- css文本截字,超出文本省略号显示
一.单行文本截字 p { text-overflow: ellipsis;/*显示省略号代替裁剪的文本*/ white-space: nowrap;/*空白处理方式 不换行*/ overflow: h ...
- ant + jmeter 自动化接口测试环境部署
1.jdk下载安装 下载地址:http://www.oracle.com/technetwork/java/javase/downloads/index.html 2.jmeter下载 jmeter官 ...
- Failed to read candidate component class错误分析
org.springframework.beans.factory.BeanDefinitionStoreException: Failed to read candidate component c ...
- go学习笔记-函数
函数 定义 格式 func function_name( [parameter list] ) [return_types] { 函数体 } 解析 func:函数由 func 开始声明 functio ...
- B -- POJ 1208 The Blocks Problem
参考:https://blog.csdn.net/yxz8102/article/details/53098575 https://www.cnblogs.com/tanjuntao/p/867892 ...
- Leetcode 简略题解 - 共567题
Leetcode 简略题解 - 共567题 写在开头:我作为一个老实人,一向非常反感骗赞.收智商税两种行为.前几天看到不止两三位用户说自己辛苦写了干货,结果收藏数是点赞数的三倍有余,感觉自己的 ...
- vs编译报错 BLOCK_TYPE_IS_VALID(pHead->nBlockUse)
1.重复释放内存导致,new delete和malloc free两个组合分配的堆空间都不能重复释放两次: 2.用delete或者free释放栈空间导致内存空间被破坏(栈空间内存的头部有系统写入的一些 ...
- CentOS 7 安装Nginx并实现域名转发
CentOS 7 条件 教程中的步骤需要root用户权限. 一.添加Nginx到YUM源 添加CentOS 7 Nginx yum资源库,打开终端,使用以下命令: sudo rpm -Uvh http ...
- Android4.0系统以上程序不出现菜单键的问题解决
去掉targetSdkVersion 或改为targetSdkVersion =13或更小.. 不改targetSdkVersion的办法:在onCreate() 里setContentView()之 ...