浅谈树状数组与线段树: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. Nginx性能测试

    环境:Centos 7.0  Nginx 1.6.2 测试工具:siege 3.0.7 配置1: I3-3110M 4G 测试1(100用户,1000请求): 平均响应:0.06s 并发数:59.19 ...

  2. 【Atheros】禁用CSMA之后pktgen发包一分钟后无法发送的问题

    无线网络中各个节点不断地广播信标帧,收到某节点的信标帧之后才知道这个节点存在,知道它的网络配置是怎么样的,才能知道应该怎么和它通信. 那么问题来了,禁用了CSMA之后,发送节点全力发送,那么它会永远占 ...

  3. c# 枚举返回字符串操作

    //内部类public static class EnumHelper { public static string GetDescription(Enum value) { if (value == ...

  4. ZookeeperclientAPI之创建会话(六)

    Zookeeper对外提供了一套Java的clientAPI. 本篇博客主要讲一下创建会话. 创建项目 首选,创建一个基于maven管理的简单javaproject.在pom文件里引入zookeepe ...

  5. 基于ajax和Form实现的注册

    注册 urls.py from django.contrib import admin from django.urls import path, re_path from app01 import ...

  6. JDBC超时原理与设置

    抄录自网上,因为担心以后找不到,因此抄录之.感谢分享的大神! 英文原版:http://www.cubrid.org/blog/dev-platform/understanding-jdbc-inter ...

  7. [LeetCode] 698. Partition to K Equal Sum Subsets

    Problem Given an array of integers nums and a positive integer k, find whether it's possible to divi ...

  8. ARP之windows下的ARP命令

    ARP之windows下的ARP命令 arp -a 查看当前电脑上的ARP映射表.可以看到当前的ARP的映射关系是动态的还是静态的. arp -s w.x.y.z aa-bb-cc-dd-ee-ff ...

  9. TP框架控制器和对应方法创建

    控制器和对应方法创建 控制器是MVC模式中的核心,TP默认有一个控制器:   Index控制器里面有一个操作方法:Index   我们在访问http://localhost:8080/Thinkphp ...

  10. css3线性渐变兼容

    火狐浏览器: background:-moz-linear-gradient(top, red, rgba(0, 0, 255, 0.5)); 谷歌: .l6{background: -webkit- ...