浅谈树状数组与线段树: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的更多相关文章

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

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

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

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

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

  4. CodeForces-1076E Vasya and a Tree

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

  5. leetcode算法: Find Bottom Left Tree Value

    leetcode算法: Find Bottom Left Tree ValueGiven a binary tree, find the leftmost value in the last row ...

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

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

  7. Codeforces 1076 E - Vasya and a Tree

    E - Vasya and a Tree 思路: dfs动态维护关于深度树状数组 返回时将当前节点的所有操作删除就能保证每次访问这个节点时只进行过根节点到当前节点这条路径上的操作 代码: #pragm ...

  8. LeetCode第[98]题(Java):Validate Binary Search Tree(验证二叉搜索树)

    题目:验证二叉搜索树 难度:Medium 题目内容: Given a binary tree, determine if it is a valid binary search tree (BST). ...

  9. 二叉树系列 - 二叉搜索树 - [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 ...

随机推荐

  1. Maven上传本地jar

    1. 将Jar包安装到本地仓库 -- DgroupId和DartifactId构成了该jar包在pom.xml的坐标, 对应依赖的DgroupId和DartifactId    -- Dfile表示需 ...

  2. HDFS源码分析EditLog之获取编辑日志输入流

    在<HDFS源码分析之EditLogTailer>一文中,我们详细了解了编辑日志跟踪器EditLogTailer的实现,介绍了其内部编辑日志追踪线程EditLogTailerThread的 ...

  3. Appium python Uiautomator2 多进程问题

    appium更新uiautomator后可以获取tost了,大家都尝试,课程中也讲解了,但是这些跑的时候都在单机上,当我们多机并发的时候会出现一个端口问题,因为我们appium最后会调用uiautom ...

  4. 【转载】【selenium+Python WebDriver】之元素定位

    总结: 感谢: “煜妃”<Selenuim+Python之元素定位总结及实例说明> “Huilaojia123”<selenium WebDriver定位元素学习总结> “上海 ...

  5. mac上利用minikube搭建kubernetes(k8s)环境

    友情提示:对于初次接触k8s的同学,强烈建议先看看本文最后的参考文章. 环境: mac os(Mojave) 前提:先安装好kubectl (brew install kubectl) .docker ...

  6. iOS 逆向 - Class-dump 安装和使用方法

    1.下载安装包 http://stevenygard.com/projects/class-dump/,这里我下载的是 class-dump-3.5.dmp.然后把下载下来的 dmg 打开,复制文件里 ...

  7. Android Apk包下查看 sha1

    用keytool工具查看sha1,格式如下:keytool -printcert -file  Urovo.RSA文件路径(APK解压后在Meta-INF文件夹下)

  8. 九度OJ 1036:Old Bill (老比尔) (基础题)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:2691 解决:1432 题目描述: Among grandfather's papers a bill was found.     72 ...

  9. mysql insert返回主键

    使用mybatis的话,很方便. 使用useGeneratedKeys和keyProperty,keyProperty是插入的java对象的属性名,不是表的字段名. 这样,在插入该条记录之后,生成的主 ...

  10. cocos2d-js添加360广告联盟插屏(通过jsb反射机制)

    1.添加demo里的libs里的jar包 2.修改AndroidManifest.xml文件 添加权限: <uses-permission android:name="android. ...