题目大意:

给一棵树root=1的树:

给一些操作:u  v 的路径所有节点的node + val;

最后m个询问:u 节点(包括u) sum%mod 是多少。

LCA + RMQ;

我们每次mark ,u , v, +=val;

fa=lca[u,v])-=val;

fa[fa]-=val

你会发现下次DFS一边的时候可以把所以答案更新出来。DFS 从下往上更新。

 #include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
#define N 100001
#define LN 20
#define mod 1000000007 vector<int>mp[N];
int mark[N];
int dp[LN][N];
int dep[N];
int ans[N],tmp[N]; int lca(int l,int r)
{
if (dep[l]<dep[r]) swap(l,r);
int dif=dep[l]-dep[r];
for (int i=;i<LN;i++)
if ((dif>>i)&) l=dp[i][l]; if (l==r) return l; for (int i=LN-;i>=;i--)
if (dp[i][l]!=dp[i][r]) l=dp[i][l],r=dp[i][r];
return dp[][l];
} void dfs(int u,int fa,int level)
{
dp[][u]=fa;
dep[u]=level;
for (int i=;i<mp[u].size();i++)
{
int v=mp[u][i];
if (v==fa) continue;
dfs(v,u,level+);
}
} int dfs2(int u,int fa=)
{
int sum=mark[u];
for (int i=;i<mp[u].size();i++)
{
int v=mp[u][i];
if (v==fa) continue;
dfs2(v,u);
sum+=tmp[v];
sum%=mod;
ans[u]+=ans[v];
ans[u]%=mod;
}
tmp[u]=sum;
ans[u]+=tmp[u];
ans[u]%=mod;
} int main()
{
int n,u,q;
scanf("%d",&n);
scanf("%d%d",&u,&q);
// for (int i=0;i<=n;i++) mp[i].clear();
for (int i=;i<n;i++)
{
int a,b;
scanf("%d%d",&a,&b);
mp[a].push_back(b);
mp[b].push_back(a);
}
dfs(,,);
for (int j=;j<LN;j++)
for (int i=;i<=n;i++)
dp[j][i]=dp[j-][dp[j-][i]];
while (u--)
{
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
mark[a]+=c;
mark[b]+=c;
mark[a]%=mod;
mark[b]%=mod;
int l=lca(a,b);
mark[l]-=c;
mark[l]%=mod;
mark[dp[][l]]-=c;
mark[dp[][l]]%=mod;
} dfs2();
while (q--)
{
int x;
scanf("%d",&x);
printf("%d\n",(ans[x]+mod)%mod);
} return ;
}

codechef FUN WITH TREES的更多相关文章

  1. Codechef Dynamic Trees and Queries

    Home » Practice(Hard) » Dynamic Trees and Queries Problem Code: ANUDTQSubmit https://www.codechef.co ...

  2. Codechef December Challenge 2014 Chef and Apple Trees 水题

    Chef and Apple Trees Chef loves to prepare delicious dishes. This time, Chef has decided to prepare ...

  3. codechef : TREDEG , Trees and Degrees

    其实有原题,生成树计数 然鹅这题里面是两道题, 50pts 可以用上面那题的做法直接过掉,另外 50pts 要推推式子,搞出 O n 的做法才行(毕竟多项式常数之大您是知道的) 虽说这道题里面是没有 ...

  4. 【CodeChef EDGEST】Edges in Spanning Trees(树链剖分+树上启发式合并)

    点此看题面 大致题意: 给你两棵\(n\)个点的树,对于第一棵树中的每条边\(e_1\),求存在多少条第二棵树中的边\(e_2\),使得第一棵树删掉\(e_1\)加上\(e_2\).第二棵树删掉\(e ...

  5. [C#] C# 知识回顾 - 表达式树 Expression Trees

    C# 知识回顾 - 表达式树 Expression Trees 目录 简介 Lambda 表达式创建表达式树 API 创建表达式树 解析表达式树 表达式树的永久性 编译表达式树 执行表达式树 修改表达 ...

  6. hdu2848 Visible Trees (容斥原理)

    题意: 给n*m个点(1 ≤ m, n ≤ 1e5),左下角的点为(1,1),右上角的点(n,m),一个人站在(0,0)看这些点.在一条直线上,只能看到最前面的一个点,后面的被档住看不到,求这个人能看 ...

  7. [LeetCode] Minimum Height Trees 最小高度树

    For a undirected graph with tree characteristics, we can choose any node as the root. The result gra ...

  8. [LeetCode] Unique Binary Search Trees 独一无二的二叉搜索树

    Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...

  9. [LeetCode] Unique Binary Search Trees II 独一无二的二叉搜索树之二

    Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For e ...

随机推荐

  1. Linux系统使用iftop查看带宽占用情况

    Linux系统下如果服务器带宽跑满了,查看跟哪个ip通信占用带宽比较多,可以通过iftop命令进行查询,使用方法如下: 1 安装方法[软件官网地址:http://www.ex-parrot.com/~ ...

  2. (C#)Xamarin.ios 发布到 App Store

    项目做到尾声了,IOS要发布,程序猿力Max来了. 不过就公司开发者账号就弄了一个月多,期间因为申请过D-U-N-S客服联系要公司资料时我们中途说取消了,后来再申请不知多少次了都没再回复... 给美国 ...

  3. 使用libsvm实现文本分类

    @Hcy(黄灿奕) 文本分类,首先它是分类问题,应该对应着分类过程的两个重要的步骤,一个是使用训练数据集训练分类器,另一个就是使用测试数据集来评价分类器的分类精度.然而,作为文本分类,它还具有文本这样 ...

  4. Web框架_MVC vs MVT

    MVC 大部分开发语言中都有MVC框架 MVC框架的核心思想是:解耦 降低各功能模块之间的耦合性,方便变更,更容易重构代码,最大程度上实现代码的重用 M表示model,主要用于对数据库层的封装 V表示 ...

  5. 通过JS加载XML文件,跨浏览器兼容

    引言 通过JS加载XML文件,跨多种浏览器兼容. 在Chrome中,没有load方法,需要特殊处理! 解决方案 部分代码 try //Internet Explorer { xmlDoc=new Ac ...

  6. python基础一 day5 知识点

    Unicode转化为gbk和utf-8 表现形式:str转化为bytes

  7. 将自己的数据制作成voc格式

    VOCdevkit2007文件下只保存VOC2007,VOC2007下只保存Annotations ImageSets JPEGImages. JPEGImages存放所有的图片数据(即训练测试验证的 ...

  8. 【转】关于编写WPF UserControl时提示The name 'InitializeComponent' does not exist in the current contextr的解决!

    1.打开.csproj(工程)文件. 2.找到<Import Project="$(MSBuildBinPath)/Microsoft.CSharp.targets" /&g ...

  9. STL源码分析-iterator(迭代器)

    1. GOF 迭代器设计模式 前面一篇文章有写到stl_list的实现,也实现了一下相应的iterator,但是后面觉得,实现具体容器之前有必要介绍一下iterator(迭代器) .那么迭代器是什么呢 ...

  10. [LOJ] 分块九题 1

    https://loj.ac/problem/6277 区间修改,单点查询. //Stay foolish,stay hungry,stay young,stay simple #include< ...