Vasya and a Tree CodeForces - 1076E
很好的思维
转化为对树上的深度差分
回朔的思想
对查询离线
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<string>
#include<queue>
#include<map>
#include<set>
#include<list>
#include<ctime>
#include<ctype.h>
#include<bitset>
#include<algorithm>
#include<numeric> //accumulate
#define endl "\n"
#define fi first
#define se second
#define FOR(i,s,t) for(int i=(s);i<=(t);++i)
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;
const int maxn=+; int n;
vector<int> g[maxn];
vector<pair<int,int> > q[maxn];
long long add[maxn];
long long ans[maxn];
void dfs(int u,int pa,int dep,long long sum)
{
for(auto i:q[u])
{
int l=dep,r=dep+i.fi+;
add[l]+=i.se;
if(r<=n)
add[r]-=i.se;
}
sum+=add[dep];
ans[u]=sum;
for(auto i:g[u])
{
if(i==pa)
continue;
dfs(i,u,dep+,sum);
}
for(auto i:q[u])
{
int l=dep,r=dep+i.fi+;
add[l]-=i.se;
if(r<=n)
add[r]+=i.se;
}
}
int main()
{ cin.tie();
cout.tie();
ios_base::sync_with_stdio(false);
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
cin>>n;
for(int i=;i<n;i++)
{
int x,y;
cin>>x>>y;
g[x].push_back(y);
g[y].push_back(x);
}
int m;
cin>>m;
for(int i=;i<m;i++)
{
int v,d,x;
cin>>v>>d>>x;
q[v].push_back({d,x});
}
dfs(,,,0ll);
for(int i=;i<=n;i++)
cout<<ans[i]<<' '; } /*
void read()
{ char c = getchar();
int x = 0;
for (; (c < 48 || c>57); c = getchar());
for (; c > 47 && c < 58; c = getchar())
{
x = (x << 1) + (x << 3) + c - 48;
}
return x;
}
*/
Vasya and a Tree CodeForces - 1076E的更多相关文章
- Vasya and a Tree CodeForces - 1076E(线段树+dfs)
I - Vasya and a Tree CodeForces - 1076E 其实参考完别人的思路,写完程序交上去,还是没理解啥意思..昨晚再仔细想了想.终于弄明白了(有可能不对 题意是有一棵树n个 ...
- Vasya and a Tree CodeForces - 1076E (线段树 + dfs)
题面 Vasya has a tree consisting of n vertices with root in vertex 1. At first all vertices has 0 writ ...
- Codeforces 1076 E - Vasya and a Tree
E - Vasya and a Tree 思路: dfs动态维护关于深度树状数组 返回时将当前节点的所有操作删除就能保证每次访问这个节点时只进行过根节点到当前节点这条路径上的操作 代码: #pragm ...
- CodeForces-1076E Vasya and a Tree
CodeForces - 1076E Problem Description: Vasya has a tree consisting of n vertices with root in verte ...
- Codeforces1076E. Vasya and a Tree(dfs+离线+动态维护前缀和)
题目链接:传送门 题目: E. Vasya and a Tree time limit per test seconds memory limit per test megabytes input s ...
- 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 ...
- Distance in Tree CodeForces - 161D
Distance in Tree CodeForces - 161D 题意:给一棵n个结点的树,任意两点之间的距离为1,现在有点u.v,且u与v的最短距离为k,求这样的点对(u,v)的个数((u,v) ...
- CF Edu54 E. Vasya and a Tree DFS+树状数组
Vasya and a Tree 题意: 给定一棵树,对树有3e5的操作,每次操作为,把树上某个节点的不超过d的子节点都加上值x; 思路: 多开一个vector记录每个点上的操作.dfs这颗树,同时以 ...
- Vasya and Beautiful Arrays CodeForces - 354C (数论,枚举)
Vasya and Beautiful Arrays CodeForces - 354C Vasya's got a birthday coming up and his mom decided to ...
随机推荐
- postman之存储测试结果
前言 在Jmeter的随笔中,我跟大家讲过利用Jmeter工具存储测试结果,那么,postman工具要该如何存储测试结果呢?下面一起来学习吧! 一:添加一个登录请求,填入接口参数点击send 二:点击 ...
- python 中列表 元组 字典 集合的区别
先看图片解释 (1)列表 什么是列表呢?我觉得列表就是我们日常生活中经常见到的清单.比如,统计过去一周我们买过的东西,把这些东西列出来,就是清单.由于我们买一种东西可能不止一次,所以清单中是允许有重复 ...
- k8s系列---stateful(有状态应用副本集)控制器
http://blog.itpub.net/28916011/viewspace-2215046/ 在应用程序中,可以分为有状态应用和无状态应用. 无状态的应用更关注于群体,任何一个成员都可以被取代. ...
- Centos 7 主要命令改动 service chkconfig iptables
1.service.chkconfig => systemctl seivice和chkconfig 是linux上的常用命令在centos7上被systemctl代替. CentOS 7 使用 ...
- C#制作Wincc组件进行配方管理
1,安装WinccV7.4并破解: 安装WinccV7.4SP1. 安装授权文件---根据提示 安装免狗驱动,根据提示 安装SImatic.net v13. 2,连接PLC, 首先在同一个局域网里面, ...
- MySQL - SQL语句优化方法
1.使用 show status 了解各种 SQL 的执行频率 mysql> show status like 'Com%'; 该命令可以查询 sql 命令的执行次数. 2.定位执行效率较低的 ...
- python打包生成可执行文件教程
python打包生成可执行文件教程 安装环境: 1.打开命令行窗口,输入命令:pip install pyinstaller ,等待下载结束.示例: 2.下载结束后,测试是否成功安装,输入命令:pyi ...
- java如何解决线程安全问题
方式一:同步代码块 synchroized(同步监视器的对象){需要被同步的代码} package threadtest; //使用同步代码块实现Runable接口的线程 public class R ...
- kali linux 渗透入门之基础准备-Burp Suite 代理设置
一:安装火狐浏览器-插件与设置中文 打开浏览器,复制粘贴这条url: https://addons.mozilla.org/en-US/firefox/addon/chinese-simplified ...
- 【WCF Restful】Post传参示范
1.传多个参数 接口定义:(ResponseFormat与RequestFormat分别将相应参数序列化.请求参数反序列化) [OperationContract] [WebInvoke(UriTem ...