题面

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 vertex x — set of vertices y such that next two conditions are met:

题意

在一棵树上,进行m次操作,每次将结点v的子树上,且与v距离小于等于d的结点加上权值加上x.

所有操作结束之后,输出每一点的权值.

思路

在字树上,同一层结点,与树上根结点的距离都是相等的.

首先,我们预处理每个结点当了那些操作的根节点.

dfs遍历到这些节点时,把dep[u]~dep[u]+d的区间加上x

之后查询ans[u],ans[u]就等于dep[u]的权值

回溯时再减去x.

#include<iostream>
#include<algorithm>
#include<vector>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<ctime> #define fuck(x) cerr<<#x<<" = "<<x<<endl;
#define debug(a, x) cerr<<#a<<"["<<x<<"] = "<<a[x]<<endl;
#define lson l,mid,ls
#define rson mid+1,r,rs
#define ls (rt<<1)
#define rs ((rt<<1)|1)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int loveisblue = 486;
const int maxn = 300086;
const int maxm = 600086;
const int inf = 0x3f3f3f3f;
const ll Inf = 999999999999999999;
const int mod = 1000000007;
const double eps = 1e-6;
const double pi = acos(-1); int dep[maxn]; int Head[maxn],cnt;
struct edge{
int Next,v;
}e[maxm];
void add_edge(int u,int v){
e[cnt].Next=Head[u];
e[cnt].v=v;
Head[u]=cnt++;
} ll ans[maxn]; ll sum[maxn<<2],lazy[maxn<<2]; void push_down(int l,int r,int rt){
int mid = (l+r)>>1;
ll len1 = mid-l+1;
ll len2 = r-mid; sum[ls]+=len1*lazy[rt];
sum[rs]+=len2*lazy[rt];
lazy[ls]+=lazy[rt];
lazy[rs]+=lazy[rt];
lazy[rt]=0;
} void update(int l,int r,int rt,int L,int R,int val){
if(L<=l&&R>=r){
sum[rt]+=1ll*(r-l+1)*val;
lazy[rt]+=val;
return;
}
if(lazy[rt]){push_down(l,r,rt);}
int mid = (l+r)>>1;
if(L<=mid)update(lson,L,R,val);
if(R>mid){update(rson,L,R,val);}
} ll query(int l,int r,int rt,int pos){ if(l==r){
return sum[rt];
}
if(lazy[rt]){push_down(l,r,rt);}
int mid = (l+r)>>1;
if(pos<=mid){
return query(lson,pos);
}else{
return query(rson,pos);
} }
int mx=0;
struct node{
int d,x;
}; vector<node>vec[maxn];
void dfs(int u,int fa,int d){
dep[u]=d;
for(int k=Head[u];k!=-1;k=e[k].Next){
int v=e[k].v;
if(v==fa){ continue;}
dfs(e[k].v,u,d+1);
}
mx=max(mx,d);
} void dfs1(int u,int fa){
int siz =vec[u].size();
for(int i=0;i<siz;i++) {
update(1, mx, 1, dep[u], min(dep[u] + vec[u][i].d,mx),vec[u][i].x);
}
ans[u]+=query(1,mx,1,dep[u]);
for(int k=Head[u];k!=-1;k=e[k].Next){
int v=e[k].v;
if(v==fa){ continue;}
dfs1(e[k].v,u);
} for(int i=0;i<siz;i++) {
update(1, mx, 1, dep[u], min(dep[u] + vec[u][i].d,mx),-vec[u][i].x);
} } int main() {
ios::sync_with_stdio(true);
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
#endif memset(Head,-1,sizeof(Head));
int n;
scanf("%d",&n);
for(int i=1;i<n;i++){
int x,y;
scanf("%d%d",&x,&y);
add_edge(x,y);
add_edge(y,x);
} int m;
scanf("%d",&m);
dfs(1,0,1);
for(int i=1;i<=m;i++){
int v,d,x;
scanf("%d%d%d",&v,&d,&x);
vec[v].push_back({d,x});
} dfs1(1,0); for(int i=1;i<=n;i++){
printf("%lld ",ans[i]);
} return 0;
}

Vasya and a Tree CodeForces - 1076E (线段树 + dfs)的更多相关文章

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

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

  2. S - Query on a tree HDU - 3804 线段树+dfs序

    S - Query on a tree HDU - 3804   离散化+权值线段树 题目大意:给你一棵树,让你求这棵树上询问的点到根节点直接最大小于等于val的长度. 这个题目和之前写的那个给你一棵 ...

  3. Z - New Year Tree CodeForces - 620E 线段树 区间种类 bitset

    Z - New Year Tree CodeForces - 620E 这个题目还没有写,先想想思路,我觉得这个题目应该可以用bitset, 首先这个肯定是用dfs序把这个树转化成线段树,也就是二叉树 ...

  4. Alyona and a tree CodeForces - 739B (线段树合并)

    大意: 给定有根树, 每个点$x$有权值$a_x$, 对于每个点$x$, 求出$x$子树内所有点$y$, 需要满足$dist(x,y)<=a_y$. 刚开始想错了, 直接打线段树合并了..... ...

  5. Vasya and a Tree CodeForces - 1076E

    很好的思维 转化为对树上的深度差分 回朔的思想 对查询离线 #include<iostream> #include<cstdio> #include<cmath> ...

  6. Bash and a Tough Math Puzzle CodeForces 914D 线段树+gcd数论

    Bash and a Tough Math Puzzle CodeForces 914D 线段树+gcd数论 题意 给你一段数,然后小明去猜某一区间内的gcd,这里不一定是准确值,如果在这个区间内改变 ...

  7. 2016湖南省赛 I Tree Intersection(线段树合并,树链剖分)

    2016湖南省赛 I Tree Intersection(线段树合并,树链剖分) 传送门:https://ac.nowcoder.com/acm/contest/1112/I 题意: 给你一个n个结点 ...

  8. CF620E New Year Tree 状压+线段树(+dfs序?)

    借用学长的活:60种颜色是突破口(我咋不知道QAQ) 好像这几道都是线段树+dfs序??于是你可以把60种颜色压进一个long long 里,然后向上合并的时候与一下(太妙了~) 所以记得开long ...

  9. HDU 5692 线段树+dfs序

    Snacks Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Sub ...

随机推荐

  1. oracle如何回收空间?

    ALTER TABLE 名称 DEALLOCATE UNUSED [KEEP 4[M|K]] 1.当空间分配过大时,可以使用本命令 2.如果没有加KEEP,回收到水线 3.如果水线<MINEXT ...

  2. Hibernate中的Session对象 标签: hibernatesession 2017-01-22 22:10 238人阅读 评论(

    Hibernate中的Session 大家在看hibernate视频的时候一定都发现了,每次要操作数据库,总是要新建一个session对象,Hibernate在对资料库进行操作之前,必须先取得Sess ...

  3. 中国境内PE\VC\投资公司名单

    中国境内PE\VC\投资公司名单 1.青云创投 2.高盛 3.红杉资本 4.鼎晖创投 5.枫丹国际 6.派杰投资银行 7.凯雷投资 8.长安私人资本 9.格林雷斯 10.汉能资本 11.启明创投 12 ...

  4. spider csdn blog part II

    继续上次的笔记, 继续完善csdn博文的提取. 发现了非常好的模块. html2docx 结果展示: 运行之后, 直接生成docx文档. 截个图如下: 结果已经基本满意了!!! 在编写过程中的一些感想 ...

  5. ajax封装最后形态

    function obj1(obj) { obj.t = new Date().getTime(); var res = []; for (var key in obj) //url中不能出现中文 r ...

  6. Java中清空session的方法

    session.removeAttribute("sessionname")是清除SESSION里的某个属性. session.invalidate()是让SESSION失效. 或 ...

  7. this 、静态变量

    /*作者:qingfeng日期:2017/2/18功能:this,静态变量(类变量)*/class Demo3_2{    public static void main(String args[]) ...

  8. QT 获取系统时间

    1.导入QTime #include <QTime> 2.定义QTime 对象接受当前时间 QTime t=QTime::currentTime(); t就是系统时间. 3.将t转化为st ...

  9. 阿里云POLARDB如何助力轻松筹打造5亿用户信赖的大病筹款平台?

    轻松筹首创了“大病救助”模式,帮助了众多病患在第一时间解決了医疗资金等问题,为了从源头解决了医疗资金问题.而在轻松筹这样全球5.5亿用户信赖的大病筹款平台的背后,是日益增长的各种数据.面对这样数据量所 ...

  10. Mac OSX原生读写NTFS功能开启方法

    macOX系统内建的NTFS支持默认只能读不能写 原生读写NTFS,需要自行终端命令手动开启 1. 插上磁盘 此时Mac桌面应该会显示出插入的磁盘,但是当你想把文件拖入磁盘的时候,发现是不能拖进去的, ...