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的更多相关文章

  1. Vasya and a Tree CodeForces - 1076E(线段树+dfs)

    I - Vasya and a Tree CodeForces - 1076E 其实参考完别人的思路,写完程序交上去,还是没理解啥意思..昨晚再仔细想了想.终于弄明白了(有可能不对 题意是有一棵树n个 ...

  2. 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 ...

  3. CodeForces-1076E Vasya and a Tree

    CodeForces - 1076E Problem Description: Vasya has a tree consisting of n vertices with root in verte ...

  4. codeforces 1065F Up and Down the Tree

    题目链接:codeforces 1065F Up and Down the Tree 题意:给出一棵树的节点数\(n\)以及一次移动的最大距离\(k\),现在有一个标记在根节点1处,每一次可以进行一下 ...

  5. Codeforces1076E. Vasya and a Tree(dfs+离线+动态维护前缀和)

    题目链接:传送门 题目: E. Vasya and a Tree time limit per test seconds memory limit per test megabytes input s ...

  6. 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 ...

  7. Codeforces 914H Ember and Storm's Tree Game 【DP】*

    Codeforces 914H Ember and Storm's Tree Game 题目链接 ORZ佬 果然出了一套自闭题 这题让你算出第一个人有必胜策略的方案数 然后我们就发现必胜的条件就是树上 ...

  8. CF Edu54 E. Vasya and a Tree DFS+树状数组

    Vasya and a Tree 题意: 给定一棵树,对树有3e5的操作,每次操作为,把树上某个节点的不超过d的子节点都加上值x; 思路: 多开一个vector记录每个点上的操作.dfs这颗树,同时以 ...

  9. Codeforces Round #499 (Div. 1) F. Tree

    Codeforces Round #499 (Div. 1) F. Tree 题目链接 \(\rm CodeForces\):https://codeforces.com/contest/1010/p ...

随机推荐

  1. windows下常用linux对应工具

    tail 可从http://files.cnblogs.com/hantianwei/tail.zip下载,解压后exe,如下: e:\>tail -fn 300 tool-slow.logus ...

  2. [WCF REST] 一个简单的REST服务实例

    Get:http://www.cnblogs.com/artech/archive/2012/02/04/wcf-rest-sample.html [01] 一个简单的REST服务实例 [02] We ...

  3. python简说(九)函数

    一.列表生成式 s =[1,2,3,4,5,6,7,8]for i in s: print(i+1)res = [ i+1 for i in s]res = [str(i) for i in s] 二 ...

  4. 【Python044--魔法方法:简单定制】

    一.简单定制 基本要求: -- 定制一个计时器的类 -- start和stop代表开始计时和停止计时 -- 假设计时器对象t1,print(t1)和直接调用t1均显示结果 -- 当计时器未启动或停止计 ...

  5. Oracle使用——oracle 忘记用户密码登录

    背景 有时候我们忘记了oracle登录的用户密码,甚至是用户名称都不确定,应该怎么登陆呢 操作系统 CentOS7 Oracle12c 操作步骤 使用sqlplus登录系统:sqlplus  /  a ...

  6. 《编写高质量代码:Web 前端开发修炼之道》 笔记与读后感

    编写高质量代码:Web 前端开发修炼之道/曹刘阳著. —北京:机械工业出版社,2010.5 第一版 涉及到的知识点: 1. CSS Sprites 在国内很多人叫css精灵,是一种网页图片应用处理方式 ...

  7. POJ 2400 Supervisor, Supervisee(KM二分图最大权值匹配)题解

    题意:n个老板n个员工,先给你n*n的数据,i行j列代表第i个老板第j喜欢的员工是谁,再给你n*n的数据,i行j列代表第i个员工第j喜欢的老板是谁,如果匹配到第k喜欢的人就会产生一个分数k-1.现在让 ...

  8. 【原理、注意点】Quartz的原理和需要注意的地方

    基本介绍和核心接口 1.quartz是完全基于java的可用于进行定时任务调度的开源框架,使用的时候需要引入: <dependency> <groupId>org.quartz ...

  9. C++笔记(2018/2/7)

    类class 类的名字就是用户自定义的类型的名字.可以像使用基本类型那样来使用它. 一个类所占用的内存空间的大小,等于所有成员变量的大小之和. 类之间可以用 "="进行赋值,但是不 ...

  10. Latex: 保持参考文献大小写

    参考: BibTeX loses capitals when creating .bbl file Latex: 保持参考文献大小写 在排版时,BibTeX会根据参考文献的格式将除了title中的第一 ...