Codeforces 1076 E - Vasya and a Tree
思路:
dfs动态维护关于深度树状数组
返回时将当前节点的所有操作删除就能保证每次访问这个节点时只进行过根节点到当前节点这条路径上的操作
代码:
#pragma GCC optimize(2)
#pragma GCC optimize(3)
#pragma GCC optimize(4)
#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pi acos(-1.0)
#define LL long long
//#define mp make_pair
#define pb push_back
#define ls rt<<1, l, m
#define rs rt<<1|1, m+1, r
#define ULL unsigned LL
#define pll pair<LL, LL>
#define pli pair<LL, int>
#define pii pair<int, int>
#define piii pair<pii, int>
#define mem(a, b) memset(a, b, sizeof(a))
#define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define fopen freopen("in.txt", "r", stdin);freopen("out.txt", "w", stout);
//head const int N = 3e5 + ;
vector<int> g[N];
vector<pii> que[N];
LL bit[N], ans[N];
int n;
void add(int x, int d) {
while(x <= n) bit[x] += d, x += x&-x;
}
LL sum(int x) {
LL ans = ;
while(x) ans += bit[x], x -= x&-x;
return ans;
} void dfs(int u, int o, int deep) {
for (pii t : que[u]) {
int d = t.fi;
int x = t.se;
int l = deep;
int r = min(deep+d, n);
add(l, x);
add(r+, -x);
}
ans[u] = sum(deep);
for (int v : g[u]) {
if(v != o) dfs(v, u, deep+);
}
for (pii t : que[u]) {
int d = t.fi;
int x = t.se;
int l = deep;
int r = min(deep+d, n);
add(l, -x);
add(r+, +x);
}
}
int main() {
int u, m, v, d, x;
scanf("%d", &n);
for (int i = ; i < n; i++) {
scanf("%d %d", &u, &v);
g[u].pb(v);
g[v].pb(u);
}
scanf("%d", &m);
for (int i = ; i <= m; i++) {
scanf("%d %d %d", &v, &d, &x);
que[v].pb({d, x});
}
dfs(, , );
for (int i = ; i <= n; i++) printf("%lld%c", ans[i], " \n"[i==n]);
return ;
}
Codeforces 1076 E - Vasya and a Tree的更多相关文章
- 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 ...
- codeforces 1065F Up and Down the Tree
题目链接:codeforces 1065F Up and Down the Tree 题意:给出一棵树的节点数\(n\)以及一次移动的最大距离\(k\),现在有一个标记在根节点1处,每一次可以进行一下 ...
- Codeforces1076E. Vasya and a Tree(dfs+离线+动态维护前缀和)
题目链接:传送门 题目: E. Vasya and a Tree time limit per test seconds memory limit per test megabytes input s ...
- 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 ...
- Codeforces 914H Ember and Storm's Tree Game 【DP】*
Codeforces 914H Ember and Storm's Tree Game 题目链接 ORZ佬 果然出了一套自闭题 这题让你算出第一个人有必胜策略的方案数 然后我们就发现必胜的条件就是树上 ...
- CF Edu54 E. Vasya and a Tree DFS+树状数组
Vasya and a Tree 题意: 给定一棵树,对树有3e5的操作,每次操作为,把树上某个节点的不超过d的子节点都加上值x; 思路: 多开一个vector记录每个点上的操作.dfs这颗树,同时以 ...
- Codeforces Round #499 (Div. 1) F. Tree
Codeforces Round #499 (Div. 1) F. Tree 题目链接 \(\rm CodeForces\):https://codeforces.com/contest/1010/p ...
随机推荐
- Strom学习笔记2:Storm Maven Project-StromStack工程
1:IntelliJ新建Maven工程
- linux centos6.8搭建 jdk 环境
1. 上官网下载jdk1.8的包 http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html ...
- ssh-keygen 不是内部或外部命令
如何处理呢? 1.找到Git/usr/bin目录下的ssh-keygen.exe(如果找不到,可以在计算机全局搜索) 2.属性-->高级系统设置-->环境变量-->系统变量,找到Pa ...
- linux上的文件服务
主要的文件服务vsftp.Samba.NFS对比 服务器名称 用户客户端平台 使用范围 服务端口 VSFTP Windows/linux/unix/macOS等 发布网站,文件共享 Tcp/21 Sa ...
- opencv学习之路(1)、示例程序
一.介绍 工欲善其事必先利其器,首先当然是配置环境安装软件啦. 我安装的vs2012+opencv2.48以及opencv3.0.具体安装步骤按照浅墨大神的博客进行即可:http://blog.cs ...
- 无旋转Treap简介
无旋转Treap是一个神奇的数据结构,能够支持插入,删除,查询k大,查询某个数的排名,查询前驱后继,支持各种区间操作和持久化.基于旋转的Treap无法实现区间反转等操作,但是无旋Treap可以轻易地支 ...
- 在Ubuntu上更新Chrome到最新的版本
本操作只限于已经安装了Chrome的Ubuntu系统: Step 1: sudo apt-get update Step 2: sudo apt-get install google-chrome-s ...
- topcoder srm 693 div1 -3
1.给出一个$n$个顶点的无向带权图.其中顶点$i,i+1$之间存在边,$i,i+2$之间存在边.而且仅有这些边.现在删掉其中的一些边,剩下的边满足图仍然是2联通的情况下使得权值和最小? 思路:其实就 ...
- SVM学习笔记2-拉格朗日对偶
下面我们抛开1中的问题.介绍拉格朗日对偶.这一篇中的东西都是一些结论,没有证明. 假设我们有这样的问题:$min_{w}$ $f(w)$,使得满足:(1)$g_{i}(w)\leq 0,1\leq i ...
- Web Service平台有三种元素构成:SOAP、WSDL、UDDI。区别和联系
Web Service平台有三种元素构成:SOAP.WSDL.UDDI.一个消费者可以在UDDI注册表查找服务,取得服务的WSDL描述,然后通过SOAP来调用服务.SOAP.WSDL.UDDI的区别如 ...