[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 ...
随机推荐
- QQ运动,新楛的马桶还在香,营销人不应摒弃。
QQ运动,都说新楛的马桶还香三天,为毛你这般明日黄花,为营销人所弃. QQ运动,一个差不多被遗忘的冷却地带,却圈粉无数,以性感.狂野.妖艳.线条.汗水等秀元素贯穿始终,狼友显露于此,爱美的女性也未曾缺 ...
- 微信小程序图片上传
uploadImage : function (){ wx.chooseImage({ count: 9, // 默认9 sizeType: ['original', 'compressed'], / ...
- ctf题目writeup(8)
2019.2.11 南京邮电的ctf平台: 地址http://ctf.nuptzj.cn/challenges# 他们好像搭新的平台了...我注册弄了好半天... 1. 签到题,打开网址: 查看一下页 ...
- Educational Codeforces Round 47 (Rated for Div. 2) :C. Annoying Present(等差求和)
题目链接:http://codeforces.com/contest/1009/problem/C 解题心得: 题意就是一个初始全为0长度为n的数列,m此操作,每次给你两个数x.d,你需要在数列中选一 ...
- [BZOJ1455]罗马游戏(左偏树)
用并查集和左偏树维护士兵的关系 Code #include <cstdio> #include <algorithm> #define N 1000010 using name ...
- JS 实现随机验证码功能
1.验证码 验证是网页常出现的一个验证点,所谓验证码类型有很多,下面代码只是实现一个简单的验证功能. <div> <input type = "text" id ...
- PHP.40-TP框架商城应用实例-后台15-商品属性与库存量1-不同商品(唯一属性、可选属性),属性类型
思路: 1.不同商品属于不同的类型,如:手机.服装.电脑等类型 2.不同的类型有不同的属性,其中分为唯一属性和可选属性,如服装:可选属性{尺寸:S,M,L……;颜色:白色,黑色……}唯一属性:材质 首 ...
- 修改mysql root密码的方法
方法1: 用SET PASSWORD命令 首先登录MySQL. 格式:mysql> set password for 用户名@localhost = password('新密码'); 例子:my ...
- Python: 列表的两种遍历方法
方法一:以列表中元素的下标进行访问 def traverse1(list1): for i in range(len(list1)): print(list1[i], end=' ') print() ...
- Python3: 对两个字符串进行匹配
Python里一共有三种字符串匹配方式,用于判断一个字符串是否包含另一个字符串.比如判断字符串“HelloWorld”中是否包含“World”: def stringCompare(str1, str ...