CF1076E:Vasya and a Tree
浅谈树状数组与线段树:https://www.cnblogs.com/AKMer/p/9946944.html
题目传送门:https://codeforces.com/problemset/problem/1076/E
因为询问只有一次,所以我们可以考虑怎样快速的找出会影响当前点的操作。
因为只有祖先结点上的操作可能会影响当前结点,所以我们在\(dfs\)的时候用树状数组动态维护深度差分值,然后单点询问当前深度应该增加多少就行了。如果本子树处理完了,那么就把差分去掉,以免影响其它子树。
时间复杂度:\(O(mlogn)\)
空间复杂度:\(O(n)\)
代码如下:
#include <cstdio>
#include <vector>
using namespace std;
typedef long long ll;
#define low(i) ((i)&(-i))
const int maxn=3e5+5;
int n,m,tot;
ll val[maxn];
int dep[maxn];
int now[maxn],pre[maxn*2],son[maxn*2];
int read() {
int x=0,f=1;char ch=getchar();
for(;ch<'0'||ch>'9';ch=getchar())if(ch=='-')f=-1;
for(;ch>='0'&&ch<='9';ch=getchar())x=x*10+ch-'0';
return x*f;
}
struct query {
int v,d;
query() {}
query(int _v,int _d) {
v=_v,d=_d;
}
};
vector<query>s[maxn];
struct TreeArray {
ll c[maxn];
void add(int pos,int v) {
for(int i=pos;i<=n;i+=low(i))
c[i]+=v;
}
ll query(int pos) {
ll res=0;
for(int i=pos;i;i-=low(i))
res+=c[i];
return res;
}
}T;
void add(int a,int b) {
pre[++tot]=now[a];
now[a]=tot;son[tot]=b;
}
void dfs(int fa,int u) {
dep[u]=dep[fa]+1;
for(int p=now[u],v=son[p];p;p=pre[p],v=son[p])
if(v!=fa)dfs(u,v);
}
void make_ans(int fa,int u) {
vector<query>::iterator it;
for(it=s[u].begin();it!=s[u].end();it++) {
int l=dep[u],r=(*it).d+dep[u];r=min(r,n);
T.add(l,(*it).v);T.add(r+1,-(*it).v);
}val[u]=T.query(dep[u]);//进来的时候差分
for(int p=now[u],v=son[p];p;p=pre[p],v=son[p])
if(v!=fa)make_ans(u,v);
for(it=s[u].begin();it!=s[u].end();it++) {
int l=dep[u],r=(*it).d+dep[u];r=min(r,n);
T.add(l,-(*it).v);T.add(r+1,(*it).v);
}//出去的时候反差分
}
int main() {
n=read();
for(int i=1;i<n;i++) {
int a=read(),b=read();
add(a,b);add(b,a);
}dfs(0,1);m=read();
for(int i=1;i<=m;i++) {
int u=read(),d=read(),x=read();
s[u].push_back(query(x,d));
}
make_ans(0,1);
for(int i=1;i<=n;i++)
printf("%lld ",val[i]);
return 0;
}
CF1076E:Vasya and a Tree的更多相关文章
- 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 ...
- Codeforces1076E. Vasya and a Tree(dfs+离线+动态维护前缀和)
题目链接:传送门 题目: E. Vasya and a Tree time limit per test seconds memory limit per test megabytes input s ...
- CF Edu54 E. Vasya and a Tree DFS+树状数组
Vasya and a Tree 题意: 给定一棵树,对树有3e5的操作,每次操作为,把树上某个节点的不超过d的子节点都加上值x; 思路: 多开一个vector记录每个点上的操作.dfs这颗树,同时以 ...
- CodeForces-1076E Vasya and a Tree
CodeForces - 1076E Problem Description: Vasya has a tree consisting of n vertices with root in verte ...
- leetcode算法: Find Bottom Left Tree Value
leetcode算法: Find Bottom Left Tree ValueGiven a binary tree, find the leftmost value in the last row ...
- Vasya and a Tree CodeForces - 1076E(线段树+dfs)
I - Vasya and a Tree CodeForces - 1076E 其实参考完别人的思路,写完程序交上去,还是没理解啥意思..昨晚再仔细想了想.终于弄明白了(有可能不对 题意是有一棵树n个 ...
- Codeforces 1076 E - Vasya and a Tree
E - Vasya and a Tree 思路: dfs动态维护关于深度树状数组 返回时将当前节点的所有操作删除就能保证每次访问这个节点时只进行过根节点到当前节点这条路径上的操作 代码: #pragm ...
- LeetCode第[98]题(Java):Validate Binary Search Tree(验证二叉搜索树)
题目:验证二叉搜索树 难度:Medium 题目内容: Given a binary tree, determine if it is a valid binary search tree (BST). ...
- 二叉树系列 - 二叉搜索树 - [LeetCode] 中序遍历中利用 pre节点避免额外空间。题:Recover Binary Search Tree,Validate Binary Search Tree
二叉搜索树是常用的概念,它的定义如下: The left subtree of a node contains only nodes with keys less than the node's ke ...
随机推荐
- 移动应用开发测试工具Bugtags集成和使用教程【转载】
前段时间,有很多APP突然走红,最终却都是樱花一现.作为一个创业团队,突然爆红是非常难得的机会.然并卵,由于没有经过充分的测试,再加上用户的激增,APP闪退.服务器数据异常等问题就被暴露出来,用户的流 ...
- Spring配置错误记录
很多其它Spring问题因为发生时未记录而遗忘了~~~~~~~ 如今动动手 解决方式因为不是源头分析因而仅供參考.! ! 严重: Exception sending context destroyed ...
- 对Oracle的rownum生成时机的理解
在Oracle中,rownum和rowid是平时经常用到的.比如rownum经常用于分页查询,rowid用于排重或者快速定位到记录. 对rownum跟order by配合下的生成时机一直没有具体研究过 ...
- 在4x4的棋盘上摆满了黑白棋子,黑白两色的位置和数目随机其中左上角坐标为(1,1),右下角坐标为(4,4),现在依次有一些翻转操作,要对一些给定支点坐标为中心的上下左右四个棋子的颜色进行翻转,请计算出翻转后的棋盘颜色。
// ConsoleApplication10.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <iostream& ...
- eclipse maven 刷新报错
问题描述: An internal error occurred during: "Loading descriptor for cmbc_wms.".java.lang.Null ...
- Yii2 跨库orm实现
近期在对公司的Yii2项目进行子系统拆分,过度阶段难免会有一些跨库操作,原生语句还好,加下库名前缀就可以了,可是到了orm问题就来了,特别是用到model做查询的时候,现在来记录一下跳过的坑, 像下面 ...
- about gnu bash shell
1 定义字符串不需要引号 var=NONE echo $var ==>NONE 2 支持基本的整数计算 a=1 b=2 echo $((a+b)) ==>3 必须用$(()),双括号的形式 ...
- 通过主机名来获取一个ip对象
//通过名称(ip字符串or主机名)来获取一个ip对象. InetAddress ip = InetAddress.getByName("www.baidu.com");//jav ...
- subline 的常用命令
zsh 配置 编辑zsh 命令 vim .zshrc alias subl="/Applications/Sublime\ Text.app/Contents/SharedSupport/b ...
- Quartz Job scheduling 基础实现代码
Quartz 集成在 SpringBoot 中分为 config.task.utils.controller 和 MVC 的三层即 controller.service.dao 和 entity. c ...