[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 ...
随机推荐
- 富文本编辑器 summernote.js
1.引用js 可在 https://summernote.org/ 官网下载 ,并查看详细的API 引入:summernote.js 和 summernote-zh-CN.js 以及样式文件:su ...
- 【转载】在C#中主线程和子线程如何实现互相传递数据
引用:https://blog.csdn.net/shuaihj/article/details/41316731 一.不带参数创建Thread using System; using System. ...
- Lavavel5.5源代码 - 限流工具
app('redis')->connection('default')->throttle('key000') // 每60秒,只能有10个资源被获取,在3秒内获取不到锁抛出异常 -> ...
- Makefile中wildcard的介绍
在Makefile规则中,通配符会被自动展开.但在变量的定义和函数引用时,通配符将失效.这种情况下如果需要通配符有效,就需要使用函数“wildcard”,它的用法是:$(wildcard PATTER ...
- uva 540 - Team Queue(插队队列)
首发:https://mp.csdn.net/mdeditor/80294426 例题5-6 团体队列(Team Queue,UVa540) 有t个团队的人正在排一个长队.每次新来一个人时,如果他有队 ...
- Python3 利用pip安装BeautifulSoup4模块(Windows版)
一.找到Python3的安装文件夹 二.将路径复制 三.Windows10 打开Windows PowerShell(管理员).Windows 8.8.1.7使用cmd 切换到相应目录 四.此目录下的 ...
- go学习笔记-包处理
包处理 package是go管理代码的重要工具,用于组织 Go 源代码,提供了更好的可重用性与可读性. 可见性 变量或函数名的首字母大写时,其就是可导出的,小写时则是不可导出的. 函数和变量的可访问性 ...
- HyperLedger Fabric 1.4 区块链应用场景(3.1)
比特币是区块链应用最早的场景,随着比特币安全稳定运行多年以后,数字货币的场景应用遍地开花,各种山寨币泛滥,通过ICO(Initial Coin Offering 首次币发行)就能融到大量资金,上市后的 ...
- 11 TCP实现QQ聊天
1.客户端参考代码 #coding=utf-8 from socket import * # 创建socket tcpClientSocket = socket(AF_INET, SOCK_STREA ...
- PIC32MZ 通过USB在线升级 -- USB CDC bootloader
了解bootloader 的实现,请加QQ: 1273623966 (验证填 bootloader):欢迎咨询或定制bootloader:我的博客主页www.cnblogs.com/geekygeek ...