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 ...
随机推荐
- 01-jsp与javabean
<%@page import="java.util.Date"%><%@ page language="java" contentType=& ...
- 2009-04-19 22:40 SQL SERVER游标的讲解
游标和游标的优点 在数据库中,游标是一个十分重要的概念.游标提供了一种对从表中检索出的数据进行操作的灵活手段,就本质而言,游标实际上是一种能从包括多条数据记录的结 果集中每次提取一条记录的机制.游标总 ...
- jquery 获取 outerHtml
在开发过程中,jQuery.html() 是获取当前节点下的html代码,并不包括当前节点本身的代码,然后我们有时候确须要.找遍jQuery api文档也没有不论什么方法能够拿到. 看到有的人通过pa ...
- python学习(六)元组学习
元组就是列表的一种,不过元组具有不可变性,而且是用圆括号访问的. 索引(下表索引或者键索引都是用的中括号) #!/usr/bin/python # 这节来学习元组, tuple, 基本上就像一个不可以 ...
- HTML经典标签用法
1.marquee属性的使用说明 <marquee> ... </marquee>移动属性的设置 ,这种移动不仅仅局限于文字,也可以应用于图片,表格等等 鼠标属性 onMo ...
- 给定一颗完全二叉树,给每一层添加上next的指针,从左边指向右边
给你机会发出声音,但是不给你机会证明高层的决定是错的 RT: 时间复杂度O(n) 空间复杂度O(1) 原理就是有指针指向父节点和当前的节点,左孩子必指向右孩子,右孩子必指向父节点的下一个节点的左孩子 ...
- WPF实现ScrollViewer滚动到指定控件处
在前端 UI 开发中,有时,我们会遇到这样的需求:在一个 ScrollViewer 中有很多内容,而我们需要实现在执行某个操作后能够定位到其中指定的控件处:这很像在 HTML 页面中点击一个链接后定位 ...
- leetcode笔记:Pow(x, n)
一. 题目描写叙述 Implement pow(x, n). 二. 题目分析 实现pow(x, n).即求x的n次幂. 最easy想到的方法就是用递归直接求n个x的乘积,这里须要依据n的值,推断结果是 ...
- Spring IOC源代码具体解释之容器依赖注入
Spring IOC源代码具体解释之容器依赖注入 上一篇博客中介绍了IOC容器的初始化.通过源代码分析大致了解了IOC容器初始化的一些知识.先简单回想下上篇的内容 加载bean定义文件的过程.这个过程 ...
- android菜鸟学习笔记4----android项目结构
src: 应用程序源代码存放目录 gen: 自动生成的目录,目录中存放所有由Android开发工具自动生成的文件. 目录中最重要的就是R.java文件. 这个文件由Android开发工具自动产生的.A ...