[CF1076E]Vasya and a Tree】的更多相关文章

我的做法: 给询问按$deep[v]+d$排序,每次做到某一深度的时候,先给这个深度所有点的值清0,然后直接改v的子树 官方做法比较妙妙: dfs,进入v的时候给$[deep[v],deep[v]+d]+=x$,出来的时候再减回来 日常忘开longlong,这回事变量开了输出没开 #pragma GCC optimize(3) #include<bits/stdc++.h> #define pa pair<ll,ll> #define CLR(a,x) memset(a,x,siz…
题目大意:给定一棵以$1$为根的树,$m$次操作,第$i$次为对以$v_i$为根的深度小于等于$d_i$的子树的所有节点权值加$x_i$.最后输出每个节点的值 题解:可以把操作离线,每次开始遍历到一个节点,把以它为根的操作加上,结束时把这个点的操作删去. 因为是$dfs$,所以一个点对同一深度的贡献是一定的,可以用树状数组区间加减,求前缀和.但是因为是$dfs$,完全可以把前缀和传入,就不需要树状数组了. 卡点:无 C++ Code: #include <cstdio> #include &l…
Vasya has a tree consisting of n n vertices with root in vertex 1 1 . At first all vertices has 0 0 written on it. Let d(i,j) d(i,j) be the distance between vertices i i and j j , i.e. number of edges in the shortest path from i i to j j . Also, let'…
I - Vasya and a Tree CodeForces - 1076E 其实参考完别人的思路,写完程序交上去,还是没理解啥意思..昨晚再仔细想了想.终于弄明白了(有可能不对 题意是有一棵树n个点,初始时候每个点权值都为0,m次修改,对v的叶子节点且距离小于d的都加上x 也就是v以下d层包括v自身都加上x 问最后每个点的权值 现在一想 用线段树来维护就是很自然的事了 但是要维护什么值呢 维护的就是某个深度上增加的值 先更新 后回溯取消更新 详见代码注释 #include <cstdio>…
题目链接:传送门 题目: E. Vasya and a Tree time limit per test seconds memory limit per test megabytes input standard input output standard output Vasya has a tree consisting of n vertices with root . At first all vertices has written on it. Let d(i,j) be the…
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 #de…
Vasya and a Tree 题意: 给定一棵树,对树有3e5的操作,每次操作为,把树上某个节点的不超过d的子节点都加上值x; 思路: 多开一个vector记录每个点上的操作.dfs这颗树,同时以深度开一个树状数组,踩到u节点的时候,给数组add(deep, x); add(min(maxn,deep + op[u][i].fi + 1), - op[u][i].se); 搜索下去,反回前得到这个节点的答案,并消去这个点的影响. //#pragma GCC optimize(3) //#pr…
题面 Vasya has a tree consisting of n vertices with root in vertex 1. At first all vertices has 0 written on it. Let d(i,j) be the distance between vertices i and j, i.e. number of edges in the shortest path from i to j. Also, let's denote k-subtree of…
CodeForces - 1076E Problem Description: Vasya has a tree consisting of n vertices with root in vertex 1. At first all vertices has 0 written on it. Let d(i,j) be the distance between vertices i and j, i.e. number of edges in the shortest path from i…
浅谈树状数组与线段树:https://www.cnblogs.com/AKMer/p/9946944.html 题目传送门:https://codeforces.com/problemset/problem/1076/E 因为询问只有一次,所以我们可以考虑怎样快速的找出会影响当前点的操作. 因为只有祖先结点上的操作可能会影响当前结点,所以我们在\(dfs\)的时候用树状数组动态维护深度差分值,然后单点询问当前深度应该增加多少就行了.如果本子树处理完了,那么就把差分去掉,以免影响其它子树. 时间复…